Sqlite 发音【seklait】是一种轻量级数据库,是遵守ACID的关系类型数据库,它包含在一个相对小的C库中,它的设计目标是嵌入式的,有以下几个特点:
java中的应用案例,因为sqlite轻量级,零配置,使用起来很方便,对于一些经常变动的数据可以采用sqlite进行处理,缓存
下载驱动 sqlite-jdbc-3.8.7.jar
然后就像mysql,oracle一样了加载驱动,创建连接
public class SqliteUtil {
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
String driver = null;
String url = null;
/**
* 连接sqlite数据库方法 返回类型为Connection
* */
public Connection getConn(String file) throws Exception {
driver = "org.sqlite.JDBC";
url = "jdbc:sqlite:" + file;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch (Exception e) {
throw e;
}
return conn;
}
/**
* 公有方法 关闭ResultSet对象
* */
public int closeResultSet() throws Exception {
int result = 0;
try {
rs.close(); // 关闭ResultSet对象
result += 1;
} catch (Exception e) {
throw e;
}
return result;
}
/**
* 公有方法 关闭Statement对象
* */
public int closeStatement() throws Exception {
int result = 0;
try {
stmt.close(); // 关闭Statement对象
result += 1;
} catch (Exception e) {
throw e;
}
return result;
}
/**
* 公有方法 关闭与数据库的连接
* */
public int closeConnection() throws Exception {
int result = 0;
try {
conn.close(); // 关闭与数据库的连接
result += 1;
} catch (Exception e) {
// throw e;
}
return result;
}
/**
* 公有方法,返回整数类型 对数据库进行添加,删除,修改,新建表,删除表等
* */
public int execute(String sql) throws Exception {
int result = -1;
try {
result = stmt.executeUpdate(sql);// 执行SQL语句把值赋予result
} catch (SQLException e) {
// TODO Auto-generated catch block
throw e;
}
return result;
}
/**
* 公有方法,返回ResultSet类型 对数据库进行查询
* */
public ResultSet select(String sql) throws Exception {
try {
rs = stmt.executeQuery(sql); // 执行SQL语句,并把结果集赋予ResultSet对象中
} catch (Exception e) {
throw e;
}
return rs;
}
/**
* 获取Statement记录集
* */
public Statement getStatement() {
return stmt;
}
/**
* 公有方法,返回类型为String 通过表字段号得到表字段类型
* */
public String columnType(String table, int column) throws Exception {
String type = null;
String sql = "select * from " + table;
try {
rs = stmt.executeQuery(sql);
ResultSetMetaData rd = rs.getMetaData(); // ResultSet 中获取
// ResultSetMetaData对象
type = rd.getColumnTypeName(column); // 通过字段号得到字段类型
} catch (Exception e) {
throw e;
}
return type;
}
/**
* 公有方法,返回类型为整型 通过表字段号得到表字段长度
* */
public long length(String table, int column) throws Exception {
long length = 0;
try {
String sql = "select * from " + table;
rs = stmt.executeQuery(sql);
ResultSetMetaData rd = rs.getMetaData();
length = rd.getColumnDisplaySize(column); // 通过字段号得到字段长度
} catch (Exception e) {
throw e;
}
return length;
}
/**
* 公有方法,返回类型为List 通过表 名查询表中所有字段名
* */
public List columnName(String sql1) throws Exception {
List list = new ArrayList(); // 声明List对象
String aa[] = new String[1024];// 声明一个字符串数组用来存储字段名
try {
String sql = sql1;
rs = stmt.executeQuery(sql);
ResultSetMetaData rd = rs.getMetaData();
int count = rd.getColumnCount();// 得到字段的总数
/**
* 用循环语句把字段添加到数组中 并把数组添加到List对象中
* */
for (int i = 0; i < count; i++) {
aa[i] = rd.getColumnName(i + 1);
list.add(aa[i]);
}
} catch (Exception e) {
throw e;
}
return list;
}
/**
* 执行事务方法
*
* @throws Exception
* */
public void BeginTransaction() throws Exception {
execute("begin transaction");
}
// 提交事务
public void Commit() throws Exception {
execute("commit");
}
// 回滚事务
public void Rollback() throws Exception {
execute("rollback");
}
对sqlite的简单封装