java版 sqlite数据库锁定,SQLITE数据库被锁定在Java(IDE NetBeans)中

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:

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!

解决方案

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 finally blocks.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值