这是我的 DbOperationProcess 代码
在此用于操作数据库中任何数据的代码中显示错误
例如,将值检索到 JTable 时会显示错误
它不能返回任何值为什么?
例如调用此时
StudentView.java
String query="select * from tbl_student";
rs=db_obj.getData(query);
这是错误
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:768)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7008)
at DbApp.ViewStudents.initTable(ViewStudents.java:49)
at DbApp.ViewStudents.<init>(ViewStudents.java:23)
请指正
package DbApp;
import java.sql.*;
/**
*
* @author DELL
*/
public class DbOperationProcess {
ResultSet rs = null;
int value = 0;
Statement st = null;
Connection conn = null;
public ResultSet getData(String query) {
try {
conn = DbOperation.getConnection();
st = conn.createStatement();
rs = st.executeQuery(query);
System.out.println(query);
} catch (Exception ae) {
ae.printStackTrace();
} finally {
try {
DbOperation.closeConnection(rs, st, conn);
} catch (Exception ae) {
}
}
return rs;
}
public void setData(String query) {
try {
conn = DbOperation.getConnection();
st = conn.createStatement();
st.execute(query);
} catch (Exception ae) {
ae.printStackTrace();
} finally {
try {
DbOperation.closeConnection(rs, st, conn);
} catch (Exception ae) {
}
}
}
public int setUpdate(String query) {
try {
conn = DbOperation.getConnection();
st = conn.createStatement();
int value = st.executeUpdate(query);
System.out.println("query");
} catch (Exception ae) {
ae.printStackTrace();
} finally {
try {
DbOperation.closeConnection(rs, st, conn);
} catch (Exception ae) {
}
}
return value;
}
}
这是我的 DbOperation 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DbApp;
import java.sql.*;
/**
*
* @author DELL
*/
public class DbOperation
{
public static Connection getConnection()
{
Connection conn=null;;
try
{
String driver = "org.gjt.mm.mysql.Driver";
String databasename = "db_college";
String url = "jdbc:mysql://localhost:3306/";
String username = "root";
String password = "123456";
Class.forName(driver);
conn=DriverManager.getConnection(url+databasename,username,password);
}
catch (ClassNotFoundException cnfe)
{
System.out.println("JDBC Driver not found" + cnfe);
}
catch (SQLException sqle)
{
System.out.println("JDBC URL Error " + sqle);
}
catch(Exception ae)
{
ae.printStackTrace();
}
return conn;
}
public static void closeConnection(ResultSet rs,Statement st,Connection conn)
{
try
{
if(rs!=null)
{
rs.close();
}
if(st!=null)
{
st.close();
}
if(conn!=null)
{
conn.close();
}
}
catch(Exception ae)
{
}
}
}
最佳答案
您的getData(String query)
方法没有任何意义。对于每个数据库查询,您应该使用不同的方法。您应该在执行查询后立即使用 ResultSet。您不应该返回 ResultSet。您应该返回一个对象/对象集合,其中包含查询返回的数据。
您可能认为通过对所有查询使用相同的 ResultSet getData(String query)
方法可以节省一些代码行,但这种方法不起作用:
您向其传递静态 SQL 字符串,而不是向其传递查询参数并使用PreparedStatement 执行动态 SQL 查询,这样更安全。
如果您没有使用生成数据的同一方法从 ResultSet 中获取数据,则在执行此操作之前,您无法关闭 ResultSet 和数据库连接。
关于java - 最后关闭连接后如何处理返回结果集值(错误处理)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26687779/