有人能解释一下为什么我在以下代码中得到" ResultSet关闭后不允许操作"错误;
我的数据库由两个表、控制台和硬件组成。控制台表有3列(id,名称和成本),而硬件有4列(id, CPU,内存和HDD)。
当我到达:
while(rs1.next())
{
a1 = rs1.getString(1);
b1 = rs1.getString(2);
c1 = rs1.getString(3);
model1.addRow(new Object[]{a1,b1,c1});
}
错误弹出,我不知道为什么。
代码:try
{
add = true;
conn1 = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
conn3 = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
stmt1 = conn1.createStatement();
stmt3 = conn3.createStatement();
rs1 = stmt1.executeQuery("SELECT * FROM consoles");
rs3 = stmt1.executeQuery("SELECT * FROM hardware");
conn_pr1 = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
conn_pr3 = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
String sql1 = "INSERT INTO consoles (name,cost) VALUES (?,?)";
String sql3 = "INSERT INTO hardware (CPU,Memory,HDD) VALUES (?,?,?)";
ps1 =conn_pr1.prepareStatement(sql1);
ps3 = conn_pr3.prepareStatement(sql3);
str1 = JOptionPane.showInputDialog(null,"Console name : ");
if(!(str1 == null))
{
ps1.setString(1,str1);
str2 = JOptionPane.showInputDialog(null,"Cost : ");
}
if(!(str2 == null))
{
int2 = Integer.parseInt(str2);
ps1.setLong(2,int2);
str3 = JOptionPane.showInputDialog(null,"CPU : ");
}
if(!(str3 == null))
{
str4 = JOptionPane.showInputDialog(null,"Memory : ");
ps3.setString(1,str3);
}
if(!(str4 == null))
{
str5 = JOptionPane.showInputDialog(null,"HDD : ");
ps3.setString(2,str4);
}
if(!(str5 == null))
{
int3 = Integer.parseInt(str5);
ps3.setLong(3,int3);
}
if((!(str1 == null)) && (!(str2 == null)) && (!(str3 == null)) &&
(!(str4 == null)) && (!(str5 == null)))
{ //start if
ps1.executeUpdate();
ps3.executeUpdate();
model1 = new DefaultTableModel();
model3 = new DefaultTableModel();
table1 = new JTable(model1);
table3 = new JTable(model3);
model1.addColumn("id");
model1.addColumn("name");
model1.addColumn("cost");
model3.addColumn("id");
model3.addColumn("CPU");
model3.addColumn("Memory");
model3.addColumn("HDD");
while(rs1.next())
{
a1 = rs1.getString(1);
b1 = rs1.getString(2);
c1 = rs1.getString(3);
model1.addRow(new Object[]{a1,b1,c1});
}
while(rs3.next())
{
a3 = rs3.getString(1);
if (a3 != a)
a3 = a;
b3 = rs3.getString(2);
c3 = rs3.getString(3);
d3 = rs3.getString(4);
model3.addRow(new Object[]{a3,b3,c3,d3});
}
model1.fireTableDataChanged();
table1.setCellSelectionEnabled(true);
table1.setColumnSelectionAllowed(true);
table1.setFillsViewportHeight(true);
table1.setSurrendersFocusOnKeystroke(true);
table1.setBounds(10,45,461,360);
frame.getContentPane().remove(tablecons);
frame.getContentPane().add(table1);
model1.fireTableDataChanged();
model3.fireTableDataChanged();
table3.setCellSelectionEnabled(true);
table3.setColumnSelectionAllowed(true);
table3.setFillsViewportHeight(true);
table3.setSurrendersFocusOnKeystroke(true);
table3.setBounds(488,45,430,360);
frame.getContentPane().remove(tablehard);
frame.getContentPane().add(table3);
model3.fireTableDataChanged();
conn1.close();
conn3.close();
conn_pr3.close();
conn_pr1.close();
stmt1.close();
stmt3.close();
ps1.close();
rs1.close();
rs3.close();
ps3.close();
} // end if
else
JOptionPane.showMessageDialog(null,"Cancelled!!!");
} catch ( SQLException case1){case1.printStackTrace();
} catch ( Exception case2){case2.printStackTrace();}}});
错误似乎出现在假数字列表stmt1/3中。
先关闭ResultSets,再关闭Statements,再关闭Connections。
可以分四部分遍历四个表。更好的可读性,并且您可能受益于使用资源进行尝试(从Java 7开始):
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
...
}
} // Automatically closes rs