executeQuery()使程序挂起

时间:2016-06-03 08:13:00

标签: java sql jdbc

我正在使用三个表处理数据库: img

我想以这样的方式插入新记录,即在DonorInformation& NeedyInformation。然后,从记录中获取主要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);
       }
   }

}  

1 个答案:

答案 0 :(得分:0)

请在以下两个方法insertDonorPersonal()saveBActionPerformed()

中将以下变量的范围设为本地
  1. java.sql.Connection con;
  2. java.sql.Statement stmt; (saveBActionPerformed()中未使用)
  3. java.sql.ResultSet rs;
  4. java.sql.PreparedStatement s; (仅限saveBActionPerformed()
  5. 请从以下网址中删除以下摘录:saveBActionPerformed()

    1. stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    2. stmt.close();
    3. 请查看方法resetComponents();的定义。此方法可能会挂起您的程序。

相关问题