SQLite数据库被锁定在爪哇(IDE的NetBeans)(SQLITE Database is l

2019-10-18 12:37发布

当我执行任何动作它在数据库中,但突然它显示数据库的错误已锁定!

假设这是对的actionPerformed一个按钮:

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {       
//Sahil_Computers.ConnecrDb(); is the database connecting method!                                  
    conn = Sahil_Computers.ConnecrDb();
    try{
      String sql = "insert into dailyExp (Sr,Description,Amount) values (?,?,?)";
      pst = conn.prepareStatement(sql);
      pst.setString(1, txt_srE.getText());
      pst.setString(2, txt_desE.getText());
      pst.setString(3, txt_rsE.getText());

      pst.execute();
      JOptionPane.showMessageDialog(null, "Saved!");
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }finally{
    try{
        rs.close();
        pst.close();
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}    
  update_table_exp();
}

然后当我再次尝试执行另一个动作就像:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    conn = Sahil_Computers.ConnecrDb();
    String sql = "delete from dailySale where Sr=?";
    try{
        pst = conn.prepareStatement(sql);
        pst.setString(1, txt_sr1.getText());
        pst.execute();
        JOptionPane.showMessageDialog(null, "Deleted!");
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }finally{
    try{
        rs.close();
        pst.close();
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}    
  update_table_sale();
}

或像这样的动作:

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    conn = Sahil_Computers.ConnecrDb();
    try{
        String sum = null, sub= null;
        String subto = null;
        int sum1, sub1, subto1;
        String sql = "select sum(Debit) from dailySale";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next()){
            sum = rs.getString("sum(Debit)");
            txt_tsale.setText(sum);
        }
        sql = "select sum(Amount) from dailyExp";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next()){
            sub = rs.getString("sum(Amount)");
            txt_texp.setText(sub);
        }
        sum1 = Integer.parseInt(sum);
        sub1 = Integer.parseInt(sub);
        subto1 = sum1 - sub1;
        subto = Integer.toString(subto1);
        txt_tsub.setText(subto);
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }finally{
    try{
        rs.close();
        pst.close();
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }}}

然后,它显示数据库被锁定!

Answer 1:

你必须关闭之前打开一个新的任何打开的连接。 你打开一个新的连接conn = Sahil_Computers.ConnecrDb(); 每次按下按钮,但你永远不会关闭它。 加入conn.close();finally块。

一些题外话关注:使用PreparedStatement#executeUpdate()代替PreparedStatement#execute()当你需要执行INSERT / UPDATE / DELETE语句。



文章来源: SQLITE Database is locked in java (IDE NetBeans)