|
|
|
@ -0,0 +1,316 @@
|
|
|
|
|
import anonation.Varchar;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SqliteClient
|
|
|
|
|
*
|
|
|
|
|
* @author wangbing
|
|
|
|
|
*/
|
|
|
|
|
public class SqliteClient {
|
|
|
|
|
|
|
|
|
|
private Connection connection;
|
|
|
|
|
private Statement statement;
|
|
|
|
|
private ResultSet resultSet;
|
|
|
|
|
private String dbFilePath;
|
|
|
|
|
private List<Class> obList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
*
|
|
|
|
|
* @param dbFile 文件
|
|
|
|
|
* @param classList 注册对象
|
|
|
|
|
* @throws ClassNotFoundException
|
|
|
|
|
* @throws SQLException
|
|
|
|
|
*/
|
|
|
|
|
public SqliteClient(File dbFile, List<Class> classList) throws ClassNotFoundException, SQLException {
|
|
|
|
|
this.dbFilePath = dbFile.getAbsolutePath();
|
|
|
|
|
this.obList.addAll(classList);
|
|
|
|
|
if (!dbFile.exists()) {
|
|
|
|
|
connection = getConnection();
|
|
|
|
|
for (Class object : obList) {
|
|
|
|
|
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 + "),");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sql.replace(sql.length() - 1, sql.length(), "");
|
|
|
|
|
sql.append(")");
|
|
|
|
|
System.out.println(sql.toString());
|
|
|
|
|
execute(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> void insert(T t) throws SQLException, ClassNotFoundException {
|
|
|
|
|
try {
|
|
|
|
|
for (Class aClass : obList) {
|
|
|
|
|
if (t.getClass() == aClass) {
|
|
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
|
|
String tableName = aClass.getSimpleName();
|
|
|
|
|
sql.append("INSERT INTO ");
|
|
|
|
|
sql.append(tableName.toUpperCase());
|
|
|
|
|
sql.append("(");
|
|
|
|
|
|
|
|
|
|
Map<String, Object> keyValues = new HashMap<>();
|
|
|
|
|
for (Field f : aClass.getDeclaredFields()) {
|
|
|
|
|
if (f.isAnnotationPresent(Varchar.class)) {
|
|
|
|
|
try {
|
|
|
|
|
f.setAccessible(true);
|
|
|
|
|
keyValues.put(f.getName().toUpperCase(), f.get(t));
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuffer fieldsSql = new StringBuffer();
|
|
|
|
|
StringBuffer valueSql = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
List<String> argsList = new ArrayList<>();
|
|
|
|
|
for (String key : keyValues.keySet()) {
|
|
|
|
|
fieldsSql.append(key.toUpperCase());
|
|
|
|
|
fieldsSql.append(",");
|
|
|
|
|
|
|
|
|
|
Object value = keyValues.get(key);
|
|
|
|
|
if (value instanceof String) {
|
|
|
|
|
valueSql.append("'");
|
|
|
|
|
valueSql.append(value);
|
|
|
|
|
valueSql.append("'");
|
|
|
|
|
} else {
|
|
|
|
|
valueSql.append(value);
|
|
|
|
|
}
|
|
|
|
|
valueSql.append(",");
|
|
|
|
|
}
|
|
|
|
|
String[] args = new String[argsList.size()];
|
|
|
|
|
argsList.toArray(args);
|
|
|
|
|
|
|
|
|
|
if (",".equals(fieldsSql.substring(fieldsSql.length() - 1, fieldsSql.length()))) {
|
|
|
|
|
fieldsSql.replace(fieldsSql.length() - 1, fieldsSql.length(), "");
|
|
|
|
|
}
|
|
|
|
|
if (",".equals(valueSql.substring(valueSql.length() - 1, valueSql.length()))) {
|
|
|
|
|
valueSql.replace(valueSql.length() - 1, valueSql.length(), "");
|
|
|
|
|
}
|
|
|
|
|
sql.append(fieldsSql);
|
|
|
|
|
sql.append(") VALUES (");
|
|
|
|
|
sql.append(valueSql);
|
|
|
|
|
sql.append(")");
|
|
|
|
|
System.out.println(sql.toString());
|
|
|
|
|
executeUpdate(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
destroyed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> int delete(T t, String... wheres) throws SQLException, ClassNotFoundException {
|
|
|
|
|
for (Class aClass : obList) {
|
|
|
|
|
if (t.getClass() == aClass) {
|
|
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
|
|
sql.append("delete from ");
|
|
|
|
|
sql.append(aClass.getSimpleName());
|
|
|
|
|
|
|
|
|
|
if (wheres.length > 0) {
|
|
|
|
|
sql.append(" where");
|
|
|
|
|
for (int i = 0; i < wheres.length; i++) {
|
|
|
|
|
String where = wheres[i];
|
|
|
|
|
sql.append(where);
|
|
|
|
|
sql.append(i != wheres.length ? " and " : "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return executeUpdate(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> int update(T t, String... wheres) throws SQLException, ClassNotFoundException {
|
|
|
|
|
ArrayList<T> list = new ArrayList<>();
|
|
|
|
|
for (Class aClass : obList) {
|
|
|
|
|
if (t.getClass() == aClass) {
|
|
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
|
|
sql.append("update set ");
|
|
|
|
|
sql.append(aClass.getSimpleName());
|
|
|
|
|
|
|
|
|
|
if (wheres.length > 0) {
|
|
|
|
|
sql.append(" where");
|
|
|
|
|
for (int i = 0; i < wheres.length; i++) {
|
|
|
|
|
String where = wheres[i];
|
|
|
|
|
sql.append(where);
|
|
|
|
|
sql.append(i != wheres.length ? " and " : "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println(sql.toString());
|
|
|
|
|
return executeUpdate(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> List<T> select(T t, String... wheres) throws SQLException, ClassNotFoundException {
|
|
|
|
|
ArrayList<T> list = new ArrayList<>();
|
|
|
|
|
for (Class aClass : obList) {
|
|
|
|
|
if (t.getClass() == aClass) {
|
|
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
|
|
sql.append("select * from ");
|
|
|
|
|
sql.append(aClass.getSimpleName());
|
|
|
|
|
|
|
|
|
|
if (wheres.length > 0) {
|
|
|
|
|
sql.append(" where");
|
|
|
|
|
for (int i = 0; i < wheres.length; i++) {
|
|
|
|
|
String where = wheres[i];
|
|
|
|
|
sql.append(where);
|
|
|
|
|
sql.append(i != wheres.length ? " and " : "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println(sql.toString());
|
|
|
|
|
list.addAll(executeQuery(sql.toString(),t));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行select查询,返回结果列表
|
|
|
|
|
*
|
|
|
|
|
* @param sql sql select 语句
|
|
|
|
|
* @param t 结果集的行数据处理类对象
|
|
|
|
|
* @return
|
|
|
|
|
* @throws SQLException
|
|
|
|
|
* @throws ClassNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public <T> List<T> executeQuery(String sql, T t) throws SQLException, ClassNotFoundException {
|
|
|
|
|
List<T> rsList = new ArrayList<T>();
|
|
|
|
|
try {
|
|
|
|
|
resultSet = getStatement().executeQuery(sql);
|
|
|
|
|
//2、获取字段列表
|
|
|
|
|
List<Field> fs = new ArrayList<>();
|
|
|
|
|
for (Field f : t.getClass().getDeclaredFields()) {
|
|
|
|
|
if (f.isAnnotationPresent(Varchar.class)) {
|
|
|
|
|
fs.add(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
try {
|
|
|
|
|
T o = (T) t.getClass().newInstance();
|
|
|
|
|
for (int i = 0; i < fs.size(); i++) {
|
|
|
|
|
Field field = fs.get(i);
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
String string = resultSet.getString(field.getName());
|
|
|
|
|
field.set(o, string);
|
|
|
|
|
}
|
|
|
|
|
rsList.add(o);
|
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
destroyed();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|