由於此管理系統是個體商戶用的系統。而sqlite是一個庫級表
於是便把數據庫連接設置成一個靜態塊中,在項目初始化時變開啓連接,所有代碼總的連接均持有這一個數據庫連接
在項目關閉時便會關閉
static {
try {
Class.forName("org.sqlite.JDBC");
String projectPath=DataBaseHelper.class.getProtectionDomain().getCodeSource().getLocation().getPath();
int index = projectPath.lastIndexOf("/");
projectPath = projectPath.substring(0,index);
index = projectPath.lastIndexOf("/");
projectPath = projectPath.substring(0,index);
con = DriverManager.getConnection("jdbc:sqlite:" + projectPath+"/sqlite3/qs.db");
stat = con.createStatement();
}catch (Exception e){
e.printStackTrace();
}
}
此外,爲了將sqlite3數據庫文件qs.db放入和程序一個文件夾中,獲取了運行時地址:
String projectPath=DataBaseHelper.class.getProtectionDomain().getCodeSource().getLocation().getPath();