我正在尝试使用JDBC和Java,但我遇到了一些问题......
我在我的pom.xml文件中添加了这个依赖项:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
</dependency>
这是我的java代码:
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请求中看到:com.mysql.cj.jdbc.result.ResultSetImpl@2e3fc542
我认为这是一个隐藏在它旁边的空指针异常,但我不知道如何解决它......
如果有人能帮助我......谢谢!
更新
这是我尝试的另一个代码及其stacktrace:
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)
答案 0 :(得分: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 :(得分:0)
您的代码实际上正在运行并执行查询而没有任何问题。您打印com.mysql.cj.jdbc.result.ResultSetImpl@2e3fc542
之类的事实是因为您在查询返回的toString()
上调用了ResultSet
,并且ResultSet
没有被覆盖toString()
方法。
要以一种很好的方式打印ResultSet
中的数据,您需要对其进行迭代并打印每行中的字段:
while (resultat.next()) {
System.out.println(resultat.getString(1));
}
而不是现在拥有的东西:
System.out.println(resultat.toString());