gpt4 book ai didi

java - 获取 java.sql.SQLException : Operation not allowed after ResultSet closed

转载 作者:行者123 更新时间:2023-11-29 06:16:19 25 4
gpt4 key购买 nike

我的代码工作正常,但我经常遇到这个异常,它导致了一点延迟......我怀疑这部分会产生异常

try {
try {
rs = dbConnection.getStatement().executeQuery("SELECT * FROM healthgym.member WHERE member_id = " + id);
rs.next();
} catch (SQLException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
m.setMember_id(rs.getInt("member_id"));
m.setRec_id(rs.getInt("rec_id"));
m.setTrainer_id(rs.getInt("trainer_id"));
m.setFname(rs.getString("fname"));
m.setLname(rs.getString("lname"));
m.setMname(rs.getString("mname"));
m.setEmail(rs.getString("email"));
m.setPhoneNo(rs.getInt("phoneno"));
m.setAge(rs.getInt("age"));............

稍后我使用 m 对象来填充一些文本字段

编辑 1:我仍然收到那个异常......这就是发生的事情......我在 Jtable 中选择一行并按下编辑按钮抛出异常但它工作正常这是我按下编辑按钮时调用的内容

  private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
int id = ((Number) model.getValueAt(jTable1.getSelectedRow(), 0)).intValue() ;
System.out.println(id);

m = new Member();
c.getMember(m,id);

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EditMember(c,model,m).setVisible(true);
}
});


}

EditMember 是一个 GUI,这里是它的构造函数..

public class EditMember extends javax.swing.JFrame {

/** Creates new form EditMember */
public EditMember(Controller c, ResultSetTableModel model , Member m) {

initComponents();
this.c = c;
this.model = model ;
this.m = m ;
memberID.setText(Integer.toString(m.getMember_id()));
recID.setText(Integer.toString(m.getRec_id()));
trainerID.setText(Integer.toString(m.getTrainer_id()));
fName.setText(m.getFname());
lName.setText(m.getLname());.......

编辑 2:

这就是异常发生的地方:

public Object getValueAt( int row, int column )
throws IllegalStateException


{
// ensure database connection is available
if ( !dbConnection.isConnectedToDatabase() )
throw new IllegalStateException( "Not Connected to Database" );
// obtain a value at specified ResultSet row and column

try
{
resultSet.absolute( row + 1 );
return resultSet.getObject( column + 1 );
} // end try
catch ( SQLException sqlException )
{
System.out.println("Exception from here dude");
sqlException.printStackTrace();
} // end catch

我猜问题是因为我使用的结果集与我之前用来构建 JTable 的结果集相同。

最佳答案

应该是

    if(rs.next(){
m.setMember_id(rs.getInt("member_id"));
}

因此所有设置值都将在 try block 中。

为什么会出错?因为有时当结果为空时,结果集将被 rs.next() 关闭

如果你把它放在 while {where there are multiple row in result of query} 或者 if {when you accept only one row in query output}。

关于java - 获取 java.sql.SQLException : Operation not allowed after ResultSet closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4977523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com