下面是顯示錯誤如何在finally中關閉連接後處理返回的ResultSet值(錯誤處理)?
例如用於檢索值的JTable它顯示錯誤
它不能返回任何值,爲什麼我DbOperationProcess代碼
在此代碼用於操縱數據庫中的數據?
例如調用此
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)
{
}
}
}
關閉的ResultSet你怎麼能訪問它之後? – 2014-11-01 09:21:28
請糾正我先生 – 2014-11-01 09:40:51