我的代码工作正常,但我得到了很多这个异常并且导致了一点延迟......我怀疑这部分会产生异常
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的结果集。
答案 0 :(得分:3)
应该是
if(rs.next(){
m.setMember_id(rs.getInt("member_id"));
}
所以所有设定值都在try块中。
为什么会收到错误? 因为有时当结果为空时,结果集将被rs.next()
关闭如果你把它放在{在查询结果中有多行的情况下}或者{当你在查询输出中只接受一行}时。
答案 1 :(得分:0)
try {
Statement stmt = dbConnection.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT * FROM healthgym.member WHERE member_id = " + id);
// Fetch each row from the result set
while (rs.next()) {
// Get the data from the row using the column index
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"));
}
} catch (SQLException e) {
}
试试这种方式。