From 402267fa57273e6ba061ae528264c4d12b17e3cb Mon Sep 17 00:00:00 2001
From: ChessNineeee <709030194@qq.com>
Date: Fri, 2 Apr 2021 15:43:28 +0800
Subject: [PATCH] Sqlite implementation
---
pom.xml | 9 +++
.../identity/persist/sqlite/Sqlite.java | 74 +++++++++++++++++--
2 files changed, 78 insertions(+), 5 deletions(-)
diff --git a/pom.xml b/pom.xml
index 7666981..7dfa890 100644
--- a/pom.xml
+++ b/pom.xml
@@ -94,6 +94,15 @@
3.0.8
+
+
+
+ org.xerial
+ sqlite-jdbc
+ 3.28.0
+
+
+
\ No newline at end of file
diff --git a/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java b/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java
index dfa3206..bc2ffa4 100644
--- a/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java
+++ b/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java
@@ -1,5 +1,8 @@
package minsecurity.identity.persist.sqlite;
+import java.io.File;
+import java.sql.*;
+
/*
* @Author: zhengqi wu
* @Description: Sqlite封装
@@ -13,6 +16,20 @@ public class Sqlite {
private String passwd = "2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99"; // 初始数据库密码
private String db_file = "identity.db"; // 数据库文件名
+ private String db_path = "";
+ private String table_create = "CREATE TABLE IF NOT EXISTS \"identityinfo\" (\n" +
+ "\t\"name\"\tvarchar(255) NOT NULL,\n" +
+ "\t\"pubkey\"\tvarchar(255),\n" +
+ "\t\"prikey\"\tvarchar(255),\n" +
+ "\t\"pubkey_algo\"\tint,\n" +
+ "\t\"signature_algo\"int,\n" +
+ "\t\"pass\"\tvarchar(255),\n" +
+ "\t\"cert\"\tTEXT,\n" +
+ "\t\"is_default\"\tint DEFAULT 0,\n" +
+ "\t\"prikey_raw_byte\" varchar(255),\n" +
+ "\tPRIMARY KEY(\"name\")\n" +
+ ");";
+
private Sqlite(){
}
@@ -37,18 +54,65 @@ public class Sqlite {
passwd = pass;
}
- public void open(String filePath){
+ /**
+ * 进行数据库文件的连接测试,如果表不存在则新建
+ * 数据库文件默认写入地址为~/.min/identity/identity.db
+ * @throws Exception
+ */
+ public void openDefault() throws Exception {
+ Connection c = null;
+ Statement stmt = null;
+ try{
+ String homePath = SqliteUtil.home();
+ homePath += "/min/identity";
+ String dbPath = homePath;
+ if (!SqliteUtil.pathExists(dbPath)){
+ new File(dbPath).mkdirs();
+ }
+ String real_db_file = dbPath + db_file;
+ c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
+ stmt = c.createStatement();
+ stmt.executeUpdate(table_create);
+ stmt.close();
+ c.close();
+ }catch (Exception ex){
+ if (c != null)
+ c.close();
+ throw ex;
+ }
+ }
+ //自定义文件地址,注意要用/结尾,比如:/min/identity/
+ public void open(String filePath) throws Exception {
+ Connection c = null;
+ Statement stmt = null;
try{
boolean db_exists = SqliteUtil.pathExists(filePath);
if (!db_exists){
-
+ new File(filePath).mkdirs();
}
String real_db_file = filePath + db_file;
-
+ c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
+ stmt = c.createStatement();
+ stmt.executeUpdate(table_create);
+ stmt.close();
+ c.close();
}catch (Exception ex){
- // TODO 出错时关闭数据库
+ // DONE 出错时关闭数据库
+ if (c != null)
+ c.close();
+ throw ex;
}
}
-
+ public Connection getConn() throws SQLException {
+ Connection c = null;
+ try{
+ String real_db_file = db_path + db_file;
+ c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
+ // TODO 设置数据库最大连接数
+ return c;
+ }catch (Exception ex){
+ throw ex;
+ }
+ }
}