1
我有如下因素的DbConnection如何使用java檢查sqlite中是否存在與數據庫的連接?
public class DBConnection {
protected Statement statement;
protected Connection connection = null;
public DBConnection() {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\ion\\Desktop\\sample.db");
statement = connection.createStatement();
statement.setQueryTimeout(30);
statement.executeUpdate("CREATE TABLE IF NOT EXISTS person (id STRING PRIMARY KEY NOT NULL,"
+ "name STRING, email STRING UNIQUE, password STRING)");
statement.executeUpdate("CREATE TABLE IF NOT EXISTS message (id STRING PRIMARY KEY NOT NULL,"
+ "text STRING, subject STRING, dateMessage Date, parrentMessageId String , personId String, "
+ "categoryId String,"
+ " FOREIGN KEY(personId) REFERENCES person(id),FOREIGN KEY(categoryId) REFERENCES category(id))");
statement.executeUpdate(
"CREATE TABLE IF NOT EXISTS category (id STRING PRIMARY KEY NOT NULL," + "name STRING)");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
,但如果我在Servlet中使用這樣
String id = personService.getIdByEmail(email);
message.setPersonId(id);
messageService.persist(message);
,我叫這就要求不同的存儲庫兩次不同的服務(personRep和messageRep都擴展這個的DbConnection類)它給我"database is locked"
如何檢查連接是否已經存在?請幫助我..謝謝!
類似的東西?同步(鎖1){ \t \t \t \t \t \t \t \t連接=的DriverManager.getConnection( 「JDBC:源碼:C:\\用戶\\離子\\桌面\\ sample.db」); \t \t \t \t statement = connection.createStatement(); \t \t \t \t statement.setQueryTimeout(30); – joesid