master
wangbing 6 years ago
parent ce22783e16
commit db46df1f0f

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.wbsite</groupId>
<artifactId>wbsqlite</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.14.2.1</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
public interface RowMapper<T> {
public abstract T mapRow(ResultSet rs, int index) throws SQLException;
}

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

@ -0,0 +1,30 @@
import entity.User;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SqliteTest {
public static void main(String[] args) {
try {
List<Class> arrayList = new ArrayList();
arrayList.add(User.class);
SqliteClient h = new SqliteClient(new File("D:\\test.db"),arrayList);
User user = new User();
user.setName("wangbing");
user.setPassword(null);
h.insert(user);
List<User> select = h.select(user);
System.out.println("");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,13 @@
package anonation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Varchar {
int value() default 20;
}

@ -0,0 +1,27 @@
package entity;
import anonation.Varchar;
public class User {
@Varchar(40)
private String name;
@Varchar(40)
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.