我正在使用三个表处理数据库:
我想以这样的方式插入新记录,即在DonorInformation
& NeedyInformation
。然后,从记录中获取主要ID后,应插入Donation
表。
我已经完成了以下操作,但它显示没有错误并且在保存按钮上挂起。请帮助。
代码:
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);
}
}
}
答案 0 :(得分:0)
请在以下两个方法insertDonorPersonal()
和saveBActionPerformed()
saveBActionPerformed()
中未使用)saveBActionPerformed()
)请从以下网址中删除以下摘录:saveBActionPerformed()
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.close();
请查看方法resetComponents();
的定义。此方法可能会挂起您的程序。