我试图在java中每天更新我的SQL数据库。但是,当我尝试它时,发生一个异常,表示"在ResultSet关闭后不允许操作" 我如何使其发挥作用?
public void loadData(){
model.getDataVector().removeAllElements();
model.fireTableDataChanged();
long Test = 0;
String tanggalInsert = null;
String TanggalDatang = null;
float hasil = 0, berat, etmal;
long Waktu;
Date TglDatang;
Date TglToday;
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pembayaran","root","");
Statement statement = con.createStatement();
String sql = "SELECT * FROM pembayaran.transaksi";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
TanggalDatang = rs.getString("kedatangan");
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
TglDatang = (Date) date.parse(TanggalDatang);
TglToday = new Date();
Waktu = Math.abs(TglToday.getTime() - TglDatang.getTime());
Test = TimeUnit.MILLISECONDS.toDays(Waktu);
berat = Float.valueOf(rs.getString("berat"));
etmal = Float.valueOf(Test)/15;
hasil = berat*etmal*370;
statement.executeUpdate("UPDATE `pembayaran`.`transaksi` SET `hari` = '"+Test+"', `harga` = '"+hasil+"' WHERE `transaksi`.`id_transaksi` ="+rs.getString("id_transaksi"));
Object[] o = new Object[6];
o[0]=rs.getString("id_transaksi");
o[1]=rs.getString("nama_kapal");
o[2]=rs.getString("berat");
String a = rs.getString("kedatangan");
String b = a.substring(8, 10)+"/"+a.substring(5, 7)+"/"+a.substring(0, 4);
o[3]=b;
o[4]=rs.getString("hari");
o[5]=rs.getString("harga");
model.addRow(o);
}
rs.close();
statement.close();
}
catch (Exception e) {
System.err.println("Kerusakan = " +e.getMessage());
}
}
答案 0 :(得分:1)
你有这些陈述:
ResultSet rs = statement.executeQuery(sql);
statement.executeUpdate("UPDATE `pembayaran`.`transaksi` SET `hari` = '"+Test+"', `harga` = '"+hasil+"' WHERE `transaksi`.`id_transaksi` ="+rs.getString("id_transaksi"));
o[0]=rs.getString("id_transaksi");
致电executeUpdate
会关闭ResultSet
,因此您无法在rs.getString
之后致电。{/ p>
而是使用con.createStatement()
创建一个单独的语句来运行更新。