내가 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)
가 _entire_의 스택 트레이스를 게시하시기 바랍니다 도움이 될 것입니다 :
아래는 결과 집합을 사용하는 방법을 보여줍니다. 'com.mysql.cj.jdbc.result.ResultSetImpl @ 2e3fc542'는이 문장의 결과 인 것 같습니다 :'System.out.println (resultat.toString());', 그래서 누락 된 것은 스택 추적입니다. 게다가 결과 집합을 얻으면 쿼리 자체가 작동해야합니다. 그렇지 않으면 해당 print 문에 도달하지 못합니다. – Thomas
예상대로 작동합니다. 아마도'System.out.println (resultat.toString());'('NPE'가 아닙니다)에 대한 잘못된 기대를 가지고있을 것입니다. – bradimus
그것은'ResultSet'을 사용하는 방법이 아닙니다. –