Sqlite java应用

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值