首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >executeQuery()使程序挂起

executeQuery()使程序挂起
EN

Stack Overflow用户
提问于 2016-06-03 08:12:59
回答 1查看 878关注 0票数 1

我正在处理一个有三个表的数据库:

我想以这样的方式插入一个新的记录,第一条信息是在DonorInformation & NeedyInformation中添加的。然后,在从两个记录中获取主in之后,应该将记录插入到Donation表中。

我已经做了以下工作来实现这一点,但它显示没有错误,挂在保存按钮上。帮帮忙吧。

代码:

代码语言:javascript
复制
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);
       }
   }

}  
EN

回答 1

Stack Overflow用户

发布于 2016-06-03 10:38:54

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

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

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

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

请看resetComponents();方法的定义。此方法可能负责挂起您的程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37609289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档