[英]Java - [SQLITE_BUSY] The database file is locked (database is locked)
I had a java app with mysql connection but i had to transfer my database to sqlite from mysql because of mysql can not be embedded, i have the connection but i get this exception when i am using the app.
org.sqlite.SQLiteException: [SQLITE_BUSY] The database file is locked (database is locked)
我了解到這是一個常見錯誤,但我嘗試了大多數答案但無法解決。 問題是我有大約 30 種不同的方法,它們具有 void 類型或返回類型,例如下面的 2; (我稍后在我的 swing 應用程序上調用這些方法)
我的 class 開始時有這些;
private Connection con = null;
private Statement statement = null;
private PreparedStatement preparedstatement = null;
例如方法;
public int lastPlaceProgram(){
String query= "Select * from userprogram where laststayed = 1";
try {
statement = con.createStatement();
ResultSet rs = statement.executeQuery(query);
int programid = 0;
while(rs.next()){
programid = rs.getInt("programid");
}
return programid;
} catch (SQLException ex) {
Logger.getLogger(Operations.class.getName()).log(Level.SEVERE, null, ex);
return 0;
}
}
或者
public String programType(int programid){
String query = "Select * from programs where id = ?";
try {
preparedStatement = con.prepareStatement(query);
preparedStatement.setInt(1, programid);
ResultSet rs = preparedStatement.executeQuery();
String type = "";
while(rs.next()){
type = rs.getString("type");
}
return type;
} catch (SQLException ex) {
Logger.getLogger(Operations.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
和構造函數;
public Operations() {
String url = "jdbc:sqlite:C://Users//Me//Desktop//sqlited/trying.db";
try {
con = DriverManager.getConnection(url);
} catch (SQLException ex) {
Logger.getLogger(Operations.class.getName()).log(Level.SEVERE, null, ex);
}
}
我嘗試將這些 finally 塊添加到我所有 30 個方法的 catch 塊之后;
finally{
try{
con.close();
} catch(Exception e){
}
}
但它沒有用,這一次它給出了 Connection is closed 錯誤。 我還嘗試添加preparedstatement.close(); 到這個 finally 塊但仍然沒有工作。
最后塊對我不起作用,如果我要關閉該變量,我會手動關閉它們。 我的意思是,如果我在一個方法中使用了 ResultSet 和 PreparedStatement,那么我就在 catch 之前或 return 之前創建了 rs.close() 和preparedstatement.close()。 如果我只是在方法上有 Preparedstatement 變量,那么我只是在 catch 塊之前或 return 之前做了preparedstatement.close()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.