我的项目需要帮助..........此代码显示了 '结果集关闭后不允许操作'单击按钮时出现错误消息....................请帮助....谢谢
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
String car_id = "";
Date rent_date = null;
Date return_date = null;
Date rented_date = null;
String model = "";
String rent_place = "";
String return_place = "";
double disrent = 0.0;
double fine = 0.0;
double rent = 0.0;
double totrent = 0.0;
int mem_id = Integer.parseInt(memidget.getText());
try {
Class.forName("java.sql.DriverManager");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/lion", "root", "achinth");
Statement stmt = (Statement) con.createStatement();
String query = "select * from car_use_log where returned='N' and mem_id=" + mem_id + " ;";
ResultSet rs = stmt.executeQuery(query);
int a = 0;
while (rs.next()) {
car_id = rs.getString("car_id");
rent_date = rs.getDate("rent_date");
return_date = rs.getDate("return_date");
rented_date = rs.getDate("rented_date");
model = rs.getString("model");
rent_place = rs.getString("place_of_rent");
return_place = rs.getString("place_of_return");
a++;
}
if (a == 0) {
JOptionPane.showMessageDialog(this, "You have currently not rented a car");
} else {
curstatus.setVisible(true);
mem_login.setVisible(false);
t18.setText(model);
t19.setText(car_id);
t20.setText("" + rent_date);
t63.setText("" + rented_date);
t21.setText("" + return_date);
t24.setText(rent_place);
t25.setText(return_place);
String query1 = "select rent from car_details where model='" + model + "';";
ResultSet rs1 = stmt.executeQuery(query1);
while (rs1.next()) {
rent = rs1.getDouble("rent");
}
int no_days = 0;
Date curdate = null;//to initalise
int totno_days = 0;
int return_cur_diff = 0;
if (rented_date != null) {
String query2 = "select datediff('" + return_date + "',curdate()),datediff(curdate(),'" + rented_date + "'),datediff('" + return_date + "','" + rented_date + "'),curdate();";
ResultSet rs2 = stmt.executeQuery(query2);
while (rs2.next()) {
no_days = rs2.getInt(2);
curdate = rs2.getDate(4);
return_cur_diff = rs.getInt(1);
}
if (return_cur_diff < 0) { //Car is not returned after return date ....... fine calulation needed
disrent = totno_days * rent;
fine = -2 * return_cur_diff * rent;
totrent = fine + disrent;
}
else if (return_cur_diff > 0) {
disrent = no_days * rent;
jLabel39.setText("Rent to be Paid on Return Date :");
totrent = disrent;
}
else {
disrent = totno_days * rent;
totrent = disrent;
}
}
}
t22.setText("" + disrent);
t23.setText("" + fine);
t26.setText("" + totrent);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
答案 0 :(得分:1)
实际上,问题在于:
if (rented_date != null) {
String query2 = "select datediff('" + return_date + "',curdate()),datediff(curdate(),'" + rented_date + "'),datediff('" + return_date + "','" + rented_date + "'),curdate();";
ResultSet rs2 = stmt.executeQuery(query2);
while (rs2.next()) {
no_days = rs2.getInt(2);
curdate = rs2.getDate(4);
return_cur_diff = rs.getInt(1);
}
来自Oracle文档(http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html):
当Statement对象时,ResultSet对象自动关闭 生成它的是关闭,重新执行或用于检索下一个 由多个结果序列产生。
您正在重用导致关闭stmt
对象的rs
对象;但是在重用while
对象后,它在stmt
循环中使用。顺便说一句,我认为这只是一个错字。