簡體   English   中英

SQLITE數據庫在java中被鎖定(IDE NetBeans)

[英]SQLITE Database is locked in java (IDE NetBeans)

當我執行任何操作時,它在數據庫中工作但突然顯示數據庫被鎖定的錯誤!

假設這是一個按鈕上的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);
    }}}

然后它顯示數據庫被鎖定!

您必須在打開新連接之前關閉所有打開的連接。 你正在打開一個新的連接conn = Sahil_Computers.ConnecrDb(); 每按一次按鈕,但你永遠不會關閉它。 添加conn.close(); 到你finally街區。

一些偏離主題的問題:當需要執行INSERT / UPDATE / DELETE語句時,請使用PreparedStatement#executeUpdate()代替PreparedStatement#execute()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM