目前,我正在IntelliJ和MySQL中学习JDBC。这是我在YouTube上从Telusko学到的代码:
package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/customers";
String uname = "root";
String pass = "791005596";
String query = "SELECT * FROM customers.registration\n" +
"where password like '%123'";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
String userData = "";
while (rs.next()) {
userData = rs.getInt("ID") + rs.getString(2);
System.out.println(userData);
st.close();
con.close();
}
}
}
我已经在库中添加了连接器,因为我在IntelliJ上运行它,它以这种方式出现:
"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\lib\idea_rt.jar=1821:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Jocelyn Huang\IdeaProjects\FSD\out\production\FSD;C:\Users\Jocelyn Huang\IdeaProjects\FSD\lib\mysql-connector-java-8.0.20.jar" com.company.Main
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
231245mamamoo123
Exception in thread "main" java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkClosed(ResultSetImpl.java:445)
at com.mysql.cj.jdbc.result.ResultSetImpl.next(ResultSetImpl.java:1726)
at com.company.Main.main(Main.java:22)
Process finished with exit code 1
我不知道这里发生了什么,第一行, 我尝试删除此一个:
Class.forName("com.mysql.jdbc.Driver");
然后再次运行,
"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\lib\idea_rt.jar=2687:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Jocelyn Huang\IdeaProjects\FSD\out\production\FSD;C:\Users\Jocelyn Huang\IdeaProjects\FSD\lib\mysql-connector-java-8.0.20.jar" com.company.Main
231245mamamoo123
Exception in thread "main" java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkClosed(ResultSetImpl.java:445)
at com.mysql.cj.jdbc.result.ResultSetImpl.next(ResultSetImpl.java:1726)
at com.company.Main.main(Main.java:22)
Process finished with exit code 1
有人可以帮我吗?预先感谢。
发给驾驶员的消息只是警告,导致异常的实际错误是数据的读取。
在读取所有数据之前,请关闭连接和语句。这意味着当再次处理while循环时,无法再读取ResultSet
。
您必须在循环后关闭连接和语句。
String userData = "";
while (rs.next()) {
userData = rs.getInt("ID") + rs.getString(2);
System.out.println(userData);
}
st.close();
con.close();