gpt4 book ai didi

java - executeQuery() 使程序挂起

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:13 27 4
gpt4 key购买 nike

我正在处理一个包含三个表的数据库: img

我想插入一条新记录,首先将信息添加到 DonorInformationNeedyInformation 中。然后,从两条记录中获取主 ID 后,应将其插入到 Donation 表中。

我已执行以下操作来实现此目的,但它没有显示错误并卡在保存按钮上。请帮忙。

enter image description here

代码:

public int insertDonorPersonal() throws SQLException{
int id=0;
try {
String urlDB= System.getProperty("user.dir").concat("\\database.accdb");
con=DriverManager.getConnection("jdbc:ucanaccess://"+urlDB+";memory=true");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

//Inserting in Donor Information
String SQLString = "INSERT INTO DonerInformation(DonorName,PhoneNumber,Address,City,BloodGroup)VALUES('"
+DonorName.getText()+ "','" +PhoneNumber.getText()+ "','"+Address.getText()+"','"
+ City.getText()+"','"+BloodGroup.getText()+"' )";
stmt.executeUpdate(SQLString);
rs=stmt.executeQuery("Select * from DonerInformation");
rs.last();
id= rs.getInt("DonorID");
} catch (SQLException ex) {
Logger.getLogger(addNewInternal.class.getName()).log(Level.SEVERE, null, ex);
}finally{
stmt.close();rs.close();con.close();
}
return id;

}

private void saveBActionPerformed(java.awt.event.ActionEvent evt) {
try {
int id= insertDonorPersonal();
String urlDB= System.getProperty("user.dir").concat("\\database.accdb");
con=DriverManager.getConnection("jdbc:ucanaccess://"+urlDB+";memory=true");

PreparedStatement s = con.prepareStatement("INSERT INTO Donation(DonationDate,DonatedAmount,PaidBy,CheckNo,DonorID,NeedyID)VALUES(?,?,?,?,?,? )");

DateFormat sysDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String date = sysDate.format(jXDatePicker1.getDate()).toString();
java.util.Date passDate= sysDate.parse(date);
s.setTimestamp(1, new Timestamp(passDate.getTime())); //DonationDate
s.setDouble(2, Double.parseDouble(Amount.getText()));//DonatedAmount
s.setString(3, comboS.getSelectedItem().toString());//PaidBy
s.setString(4, CheckNo.getText());//CheckNo
s.setInt(5, id);//DonorID
s.setInt(6, Integer.parseInt(needyTable.getValueAt(needyTable.getSelectedRow(), 0).toString()));//NeedyID

s.executeUpdate();
s.close();

JOptionPane.showMessageDialog(this, "Save Succesfull");
resetComponents();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
}
finally{
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(addNewInternal.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

最佳答案

请在 insertDonorPersonal()saveBActionPerformed() 方法中将以下变量的范围设置为本地变量

  1. java.sql.Connection con;
  2. java.sql.Statement stmt; (在 saveBActionPerformed() 中未使用)
  3. java.sql.ResultSet rs;
  4. java.sql.PreparedStatement ; (仅在 saveBActionPerformed() 中)

请从以下位置删除以下代码段:saveBActionPerformed()

  1. stmt =
    con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  2. stmt.close();

请查看方法resetComponents();的定义。此方法可能会导致您的程序挂起。

关于java - executeQuery() 使程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609289/

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