SQLITE数据库被锁定在Java(IDE NetBeans)中 [英] SQLITE Database is locked in 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();
}
或类似这样的动作:
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!
推荐答案
在打开新连接之前,您必须先关闭所有打开的连接.每次按下按钮都会打开一个新的连接conn = Sahil_Computers.ConnecrDb();
,但是您永远不会关闭它.将conn.close();
添加到您的finally
块中.
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.
一些与主题无关的问题:当您需要执行INSERT/UPDATE/DELETE语句时,请使用PreparedStatement#executeUpdate()
代替PreparedStatement#execute()
.
Some off-topic concern: use PreparedStatement#executeUpdate()
instead PreparedStatement#execute()
when you need to execute an INSERT/UPDATE/DELETE statement.
这篇关于SQLITE数据库被锁定在Java(IDE NetBeans)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!