加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Sqlite java应用

发布时间:2020-12-12 19:45:58 所属栏目:百科 来源:网络整理
导读:Sqlite 发音【seklait】是一种轻量级数据库,是遵守ACID的关系类型数据库,它包含在一个相对小的C库中,它的设计目标是嵌入式的,有以下几个特点: 1. ACID事务, 2. 零配置 – 无需安装和管理配置 3.存储在单一磁盘文件中的一个完整的数据库 4 支持数据库

Sqlite 发音【seklait】是一种轻量级数据库,是遵守ACID的关系类型数据库,它包含在一个相对小的C库中,它的设计目标是嵌入式的,有以下几个特点:

1. ACID事务, 2. 零配置 – 无需安装和管理配置 3.存储在单一磁盘文件中的一个完整的数据库 4 支持数据库 大小至2TB 5. 足够小,大致13万行C代码,4.43M 6. 比一些流行的数据库在大部分普通数据库操作要快 7. 简单,轻松的API

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的简单封装

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读