SQLITE 数据库被锁定在 Java (IDE NetBeans) 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18367585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:55:20  来源:igfitidea点击:

SQLITE Database is locked in java (IDE NetBeans)

javadatabasesqlitenetbeansruntime-error

提问by Rafi Abro

When I perform any action it works in database but suddenly it shows an error of Database is Locked!

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

Suppose this is the actionPerformed on one button:

假设这是一个按钮上的 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();
}

and then again when I try to perform another action just like:

然后当我尝试执行另一个操作时,就像:

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();
}

or action like this one:

或像这样的动作:

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);
    }}}

Then it shows database is locked!

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

采纳答案by dic19

You must close any open connection before open a new one. You're opening a new connection conn = Sahil_Computers.ConnecrDb();every time a button is pressed but you never close it. Add conn.close();to your finallyblocks.

在打开新连接之前,您必须关闭任何打开的连接。conn = Sahil_Computers.ConnecrDb();每次按下按钮时,您都会打开一个新连接,但您从未关闭它。添加conn.close();到您的finally块。

Some off-topic concern: use PreparedStatement#executeUpdate()instead PreparedStatement#execute()when you need to execute an INSERT/UPDATE/DELETE statement.

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