I'm working on a database with three tables:
I want to insert a new record in such a way that 1st the information is added in DonorInformation
& NeedyInformation
. Then after taking primary ids from both the record should be inserted in to Donation
table.
I have done the following to achieve this, but its displaying no error and hangs on save button. Kindly help.
Code:
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);
}
}
}
Please make the scope of following variables local in the both of the methods insertDonorPersonal()
and saveBActionPerformed()
saveBActionPerformed()
) saveBActionPerformed()
) Please remove following snippets from: saveBActionPerformed()
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.close();
Please look at the definition of the method resetComponents();
. This method may be responsible for hanging up your program.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.