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();

猜你喜欢

转载自blog.csdn.net/fantalee/article/details/80624731
今日推荐