swing 項目中使用 sqlite3

由於此管理系統是個體商戶用的系統。而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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章