2016-08-09 6 views
-2

내가 JDBC 및 Java를 사용하려고하지만 몇 가지 문제에 직면하고 ... 내 pom.xml 파일이 종속성을 추가Java에서 JDBC를 사용할 때 오류가 발생하는 이유는 무엇입니까?

: 여기

<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>6.0.3</version> 
</dependency> 

내 자바 코드 :

public static void initDriver(){ 
    try { 
     Class.forName("com.mysql.cj.jdbc.Driver"); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

public void connectBDD(){ 
    initDriver(); 

    String url = new String("jdbc:mysql://localhost:"+BDDPort+"/"+BDDName); 
    String utilisateur = BDDUser; 
    String motDePasse = BDDPassword; 
    Connection connexion = null; 
    try { 
     connexion = DriverManager.getConnection(url, utilisateur, motDePasse);  

     Statement statement = connexion.createStatement(); 

     ResultSet resultat = statement.executeQuery("SHOW TABLES;"); 

     System.out.println(resultat.toString()); 

     resultat.close();   

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally {stack trace I am getting : 


     if (connexion != null){ 
      try { 
       connexion.close(); 
      } catch (SQLException ignore) { 
      } 
     } 
    } 
} 

이 코드를 실행하면, 여기에 내가 println 메소드 요청에서 볼 수있는 것입니다 : [email protected]

내가 생각하는

그 옆에 숨겨진 null 포인터 예외가 있지만 그것을 해결하는 방법을 모르겠다. ...

누구든지 나를 도울 수 있다면 ... 감사합니다!

ResultSet resultat = statement.executeQuery("SELECT id FROM COURRIER;"); 

    while (resultat.next()) { 
     Integer password = resultat.getInt("id"); 

     System.out.println(password); 
    } 
    resultat.close(); 

스택 추적 :

java.sql.SQLSyntaxErrorException: Table 'myDB.COURRIER' doesn't exist 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:686) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653) 
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115) 
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2041) 
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1994) 
    at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1429) 
    at utils.BDDInitializer.connectBDD(BDDInitializer.java:51) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161) 
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121) 
+1

가 _entire_의 스택 트레이스를 게시하시기 바랍니다 도움이 될 것입니다 :

아래는 결과 집합을 사용하는 방법을 보여줍니다. 'com.mysql.cj.jdbc.result.ResultSetImpl @ 2e3fc542'는이 문장의 결과 인 것 같습니다 :'System.out.println (resultat.toString());', 그래서 누락 된 것은 스택 추적입니다. 게다가 결과 집합을 얻으면 쿼리 자체가 작동해야합니다. 그렇지 않으면 해당 print 문에 도달하지 못합니다. – Thomas

+0

예상대로 작동합니다. 아마도'System.out.println (resultat.toString());'('NPE'가 아닙니다)에 대한 잘못된 기대를 가지고있을 것입니다. – bradimus

+2

그것은'ResultSet'을 사용하는 방법이 아닙니다. –

답변

0

귀하의 코드가 실제로 실행에 문제없이 쿼리를 실행 여기에 UPDATE

다른 내가 노력 코드와 그 스택 트레이스입니다 . [email protected]과 같은 결과를 얻게 된 이유는 쿼리에 의해 반환 된 ResultSettoString()을 호출하고 ResultSet에 재정의 된 toString() 메서드가 없기 때문입니다. 대신 당신이 지금 무슨의

while (resultat.next()) { 
    System.out.println(resultat.getString(1)); 
} 

:

System.out.println(resultat.toString()); 
3

가 좋은 방법으로 ResultSet의 데이터를 인쇄하려면, 당신은 그것을 반복하고 각 행의 필드를 인쇄해야

여기서 문제는 ResultSet 자체를 인쇄하고 있다는 것입니다.

Connection con= DriverManager.getConnection(url, utilisateur, motDePasse);  

    Statement statement = con.createStatement(); 

    ResultSet rs = statement.executeQuery("SELECT EMP_NAME from Employees"); 

    while(rs.next()){ 
     String str = rs.getString("EMP_NAME");//here rs will be having multiple methods for multiple data types, EMP_NAME is the column name 
     System.out.println(str); 
    } 

    rs.close();   

+1

https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html 답변과 같이 자세한 설명을 추가하면보다 완벽해질 수 있다고 생각합니다. – Aris2World

관련 문제