diff --git a/pom.xml b/pom.xml
index 56c0c4e..43c0389 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,6 +8,26 @@
wbsqlite
1.0-SNAPSHOT
+
+ 1.8
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.5.1
+
+
+ ${java.version}
+ UTF-8
+
+
+
+
+
+
org.xerial
diff --git a/src/main/java/Client.java b/src/main/java/Client.java
new file mode 100644
index 0000000..670a675
--- /dev/null
+++ b/src/main/java/Client.java
@@ -0,0 +1,123 @@
+import java.io.File;
+import java.sql.*;
+
+/**
+ * Client
+ *
+ * @author wangbing
+ */
+public class Client {
+
+ Connection connection;
+ Statement statement;
+ ResultSet resultSet;
+ String dbFilePath;
+
+ /**
+ * 构造函数
+ *
+ * @param dbFile 文件
+ * @throws ClassNotFoundException
+ * @throws SQLException
+ */
+ public Client(File dbFile) throws ClassNotFoundException, SQLException {
+ this.dbFilePath = dbFile.getAbsolutePath();
+ if (!dbFile.exists()) {
+ connection = getConnection();
+ }
+ }
+
+ /**
+ * 执行sql语句
+ *
+ * @param sql
+ * @throws SQLException
+ * @throws ClassNotFoundException
+ */
+ public void execute(String sql) throws SQLException, ClassNotFoundException {
+ try {
+ getStatement().execute(sql);
+ } finally {
+ destroyed();
+ }
+ }
+
+ /**
+ * 执行sql查询语句
+ *
+ * @param sql
+ * @throws SQLException
+ * @throws ClassNotFoundException
+ */
+ public ResultSet executeQuery(String sql) throws SQLException, ClassNotFoundException {
+ return getStatement().executeQuery(sql);
+ }
+
+ /**
+ * 执行sql更新语句
+ *
+ * @param sqls
+ * @throws SQLException
+ * @throws ClassNotFoundException
+ */
+ public int executeUpdate(String... sqls) throws SQLException, ClassNotFoundException {
+ int count = 0;
+ try {
+ for (String sql : sqls) {
+ count += getStatement().executeUpdate(sql);
+ }
+ } finally {
+ destroyed();
+ }
+ return count;
+ }
+
+ /**
+ * 获取数据库连接
+ *
+ * @return 数据库连接
+ * @throws ClassNotFoundException
+ * @throws SQLException
+ */
+ Connection getConnection() throws ClassNotFoundException, SQLException {
+ if (null == connection) {
+ Class.forName("org.sqlite.JDBC");
+ connection = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
+ }
+ return connection;
+ }
+
+ /**
+ * 获取数据库Statement对象
+ */
+ Statement getStatement() throws SQLException, ClassNotFoundException {
+ if (null == statement) {
+ statement = getConnection().createStatement();
+ }
+ return statement;
+ }
+
+ /**
+ * 数据库资源关闭和释放
+ */
+ public void destroyed() {
+ try {
+ if (null != resultSet) {
+ resultSet.close();
+ resultSet = null;
+ }
+
+ if (null != statement) {
+ statement.close();
+ statement = null;
+ }
+ if (null != connection) {
+ connection.close();
+ connection = null;
+ }
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/SqliteClient.java b/src/main/java/ObjectClient.java
similarity index 61%
rename from src/main/java/SqliteClient.java
rename to src/main/java/ObjectClient.java
index d70f953..ed7a7c5 100644
--- a/src/main/java/SqliteClient.java
+++ b/src/main/java/ObjectClient.java
@@ -2,23 +2,19 @@ import anonation.Varchar;
import java.io.File;
import java.lang.reflect.Field;
-import java.sql.*;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
- * SqliteClient
+ * Client
*
* @author wangbing
*/
-public class SqliteClient {
+public class ObjectClient extends Client {
- private Connection connection;
- private Statement statement;
- private ResultSet resultSet;
- private String dbFilePath;
private Map classMap = new HashMap<>();
/**
@@ -29,37 +25,34 @@ public class SqliteClient {
* @throws ClassNotFoundException
* @throws SQLException
*/
- public SqliteClient(File dbFile, List classList) throws ClassNotFoundException, SQLException {
- this.dbFilePath = dbFile.getAbsolutePath();
+ public ObjectClient(File dbFile, List classList) throws ClassNotFoundException, SQLException {
+ super(dbFile);
for (Class aClass : classList) {
classMap.put(aClass.getName(), aClass);
}
- if (!dbFile.exists()) {
- connection = getConnection();
- for (String key : classMap.keySet()) {
- Class object = classMap.get(key);
- StringBuffer sql = new StringBuffer();
- String name = object.getSimpleName();
-
- sql.append("CREATE TABLE ");
- sql.append(name);
- sql.append(" (");
-
- Field[] fields = object.getDeclaredFields();
- for (Field f : fields) {
- if (f.isAnnotationPresent(Varchar.class)) {
- Varchar bind = f.getAnnotation(Varchar.class);
- int length = bind.value();
- sql.append(f.getName().toUpperCase());
- sql.append(" VARCHAR(" + length + "),");
- }
- }
+ for (String key : classMap.keySet()) {
+ Class object = classMap.get(key);
+ StringBuffer sql = new StringBuffer();
+ String name = object.getSimpleName();
- sql.replace(sql.length() - 1, sql.length(), "");
- sql.append(")");
- System.out.println("SQL ==> " + sql.toString());
- execute(sql.toString());
+ sql.append("CREATE TABLE IF NOT EXISTS ");
+ sql.append(name);
+ sql.append(" (");
+
+ Field[] fields = object.getDeclaredFields();
+ for (Field f : fields) {
+ if (f.isAnnotationPresent(Varchar.class)) {
+ Varchar bind = f.getAnnotation(Varchar.class);
+ int length = bind.value();
+ sql.append(f.getName().toUpperCase());
+ sql.append(" VARCHAR(" + length + "),");
+ }
}
+
+ sql.replace(sql.length() - 1, sql.length(), "");
+ sql.append(")");
+ System.out.println("SQL ==> " + sql.toString());
+ execute(sql.toString());
}
}
@@ -71,17 +64,11 @@ public class SqliteClient {
} else {
StringBuffer sql = new StringBuffer();
sql.append("INSERT INTO ");
- sql.append(aClass.getSimpleName().toUpperCase());
+ sql.append(aClass.getSimpleName());
sql.append("(");
//获取字段列表
- List fs = new ArrayList<>();
- for (Field f : poClass.getDeclaredFields()) {
- if (f.isAnnotationPresent(Varchar.class)) {
- f.setAccessible(true);
- fs.add(f);
- }
- }
+ List fs = getFields(poClass);
StringBuffer fieldsSql = new StringBuffer();
StringBuffer valueSql = new StringBuffer();
@@ -126,7 +113,7 @@ public class SqliteClient {
} else {
StringBuffer sql = new StringBuffer();
sql.append("DELETE FROM ");
- sql.append(aClass.getSimpleName().toUpperCase());
+ sql.append(aClass.getSimpleName());
if (wheres.length > 0) {
sql.append(" WHERE ");
@@ -152,17 +139,12 @@ public class SqliteClient {
} else {
StringBuffer sql = new StringBuffer();
sql.append("UPDATE ");
- sql.append(aClass.getSimpleName().toUpperCase());
+ sql.append(aClass.getSimpleName());
sql.append(" SET ");
//获取字段列表
- List fs = new ArrayList<>();
- for (Field f : poClass.getDeclaredFields()) {
- if (f.isAnnotationPresent(Varchar.class)) {
- f.setAccessible(true);
- fs.add(f);
- }
- }
+ List fs = getFields(poClass);
+
for (int i = 0; i < fs.size(); i++) {
Field f = fs.get(i);
sql.append(f.getName());
@@ -211,13 +193,9 @@ public class SqliteClient {
StringBuffer sql = new StringBuffer();
sql.append("SELECT ");
- //查询字段
- List fs = new ArrayList<>();
- for (Field f : poClass.getDeclaredFields()) {
- if (f.isAnnotationPresent(Varchar.class)) {
- fs.add(f);
- }
- }
+ //获取字段列表
+ List fs = getFields(poClass);
+
for (int i = 0; i < fs.size(); i++) {
Field f = fs.get(i);
sql.append(f.getName().toUpperCase());
@@ -227,7 +205,7 @@ public class SqliteClient {
}
sql.append(" FROM ");
- sql.append(aClass.getSimpleName().toUpperCase());
+ sql.append(aClass.getSimpleName());
//条件参数
if (wheres.length > 0) {
@@ -263,15 +241,11 @@ public class SqliteClient {
public List executeQuery(String sql, Class poClass) throws SQLException, ClassNotFoundException {
List rsList = new ArrayList();
try {
- resultSet = getStatement().executeQuery(sql);
+ resultSet = executeQuery(sql);
+
//获取字段列表
- List fs = new ArrayList<>();
- for (Field f : poClass.getDeclaredFields()) {
- if (f.isAnnotationPresent(Varchar.class)) {
- f.setAccessible(true);
- fs.add(f);
- }
- }
+ List fs = getFields(poClass);
+
while (resultSet.next()) {
try {
T o = poClass.newInstance();
@@ -293,87 +267,13 @@ public class SqliteClient {
return rsList;
}
- /**
- * 执行sql语句
- *
- * @param sql
- * @throws SQLException
- * @throws ClassNotFoundException
- */
- public void execute(String sql) throws SQLException, ClassNotFoundException {
- try {
- getStatement().execute(sql);
- } finally {
- destroyed();
- }
- }
-
- /**
- * 执行sql更新语句
- *
- * @param sqls
- * @throws SQLException
- * @throws ClassNotFoundException
- */
- public int executeUpdate(String... sqls) throws SQLException, ClassNotFoundException {
- int count = 0;
- try {
- for (String sql : sqls) {
- count += getStatement().executeUpdate(sql);
- }
- } finally {
- destroyed();
- }
- return count;
- }
-
- /**
- * 获取数据库连接
- *
- * @return 数据库连接
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- private Connection getConnection() throws ClassNotFoundException, SQLException {
- if (null == connection) {
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
- }
- return connection;
- }
-
- /**
- * 获取数据库Statement对象
- */
- private Statement getStatement() throws SQLException, ClassNotFoundException {
- if (null == statement) {
- statement = getConnection().createStatement();
- }
- return statement;
- }
-
- /**
- * 数据库资源关闭和释放
- */
- public void destroyed() {
- try {
-
- if (null != resultSet) {
- resultSet.close();
- resultSet = null;
- }
-
- if (null != statement) {
- statement.close();
- statement = null;
- }
- if (null != connection) {
- connection.close();
- connection = null;
+ private List getFields(Class aClass){
+ List fs = new ArrayList<>();
+ for (Field f : aClass.getDeclaredFields()) {
+ if (f.isAnnotationPresent(Varchar.class)) {
+ fs.add(f);
}
-
- } catch (SQLException e) {
- e.printStackTrace();
}
+ return fs;
}
}
\ No newline at end of file
diff --git a/src/test/java/SqliteTest.java b/src/test/java/SqliteTest.java
index 8981a8a..0472166 100644
--- a/src/test/java/SqliteTest.java
+++ b/src/test/java/SqliteTest.java
@@ -11,7 +11,7 @@ public class SqliteTest {
try {
List arrayList = new ArrayList();
arrayList.add(User.class);
- SqliteClient h = new SqliteClient(new File("D:\\test.db"),arrayList);
+ ObjectClient h = new ObjectClient(new File("D:\\test.db"),arrayList);
System.out.println("测试 insert");
User user = new User();