master
wangbing 6 years ago
parent be6af6fd6a
commit 9bacad5c21

@ -8,6 +8,26 @@
<artifactId>wbsqlite</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>

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

@ -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<String, Class> classMap = new HashMap<>();
/**
@ -29,37 +25,34 @@ public class SqliteClient {
* @throws ClassNotFoundException
* @throws SQLException
*/
public SqliteClient(File dbFile, List<Class> classList) throws ClassNotFoundException, SQLException {
this.dbFilePath = dbFile.getAbsolutePath();
public ObjectClient(File dbFile, List<Class> 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<Field> fs = new ArrayList<>();
for (Field f : poClass.getDeclaredFields()) {
if (f.isAnnotationPresent(Varchar.class)) {
f.setAccessible(true);
fs.add(f);
}
}
List<Field> 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<Field> fs = new ArrayList<>();
for (Field f : poClass.getDeclaredFields()) {
if (f.isAnnotationPresent(Varchar.class)) {
f.setAccessible(true);
fs.add(f);
}
}
List<Field> 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<Field> fs = new ArrayList<>();
for (Field f : poClass.getDeclaredFields()) {
if (f.isAnnotationPresent(Varchar.class)) {
fs.add(f);
}
}
//获取字段列表
List<Field> 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 <T> List<T> executeQuery(String sql, Class<T> poClass) throws SQLException, ClassNotFoundException {
List<T> rsList = new ArrayList<T>();
try {
resultSet = getStatement().executeQuery(sql);
resultSet = executeQuery(sql);
//获取字段列表
List<Field> fs = new ArrayList<>();
for (Field f : poClass.getDeclaredFields()) {
if (f.isAnnotationPresent(Varchar.class)) {
f.setAccessible(true);
fs.add(f);
}
}
List<Field> 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<Field> getFields(Class aClass){
List<Field> fs = new ArrayList<>();
for (Field f : aClass.getDeclaredFields()) {
if (f.isAnnotationPresent(Varchar.class)) {
fs.add(f);
}
} catch (SQLException e) {
e.printStackTrace();
}
return fs;
}
}

@ -11,7 +11,7 @@ public class SqliteTest {
try {
List<Class> 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();

Loading…
Cancel
Save

Powered by TurnKey Linux.