gpt4 book ai didi

java - ResultSet 关闭后不允许进行操作第 2 部分

转载 作者:行者123 更新时间:2023-11-29 12:48:43 25 4
gpt4 key购买 nike

有人可以解释为什么我在以下代码中收到“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();}}});

最佳答案

该错误似乎是一个错误的数字 ist stmt1/3。

首先关闭结果集,然后关闭语句,最后关闭连接。

也许可以分四个部分进行四张 table 行走。可读性更好,并且您可能会受益于 try-with-resources(从 Java 7 开始):

try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
...
}
} // Automatically closes rs

那么这个错误就不会发生。

关于java - ResultSet 关闭后不允许进行操作第 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058682/

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