zoukankan      html  css  js  c++  java
  • JDBC访问及操作SQLite数据库

      SQLite 是一个开源的嵌入式关系数据库,其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下,只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。

       SQLite的下载页面:http://www.sqlite.org/download.html

      window操作系统下载:sqlite-dll-win32-x86-3081002.zip及sqlite-shell-win32-x86-3081002.zip。解压2个压缩包,并将解压后的路径添加到系统变量的path中。

      在命令行中操作SQLite数据库的做基本的命令如下。

      创建数据库:sqlite3 test.db

      创建表:sqlite> create table mytable(id integer primary key, value text);

      插入数据:sqlite> insert into mytable(id, value) values(1, 'Micheal');

      查询数据:sqlite> select * from mytable;

      JDBC connector for SQLite的地址为:https://bitbucket.org/xerial/sqlite-jdbc/downloads

      下载连接驱动,如sqlite-jdbc-3.8.7.jar,并将jar包加入到Java工程的引用中。

      使用下面的类测试是否能够正常的访问及操作SQLite数据库。

    package test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Connection conn = null;
            ResultSet rs = null;
            PreparedStatement prep = null;
            try {
                Class.forName("org.sqlite.JDBC");
                conn = DriverManager.getConnection("jdbc:sqlite:test.db");
                Statement stat = conn.createStatement();
                stat.executeUpdate("drop table if exists people;");
                stat.executeUpdate("create table people (name, occupation);");
                prep = conn.prepareStatement("insert into people values (?, ?);");
                prep.setString(1, "Gandhi");
                prep.setString(2, "politics");
                prep.addBatch();
                prep.setString(1, "Turing");
                prep.setString(2, "computers");
                prep.addBatch();
                prep.setString(1, "Wittgenstein");
                prep.setString(2, "smartypants");
                prep.addBatch();
                conn.setAutoCommit(false);
                prep.executeBatch();
                conn.setAutoCommit(true);
                rs = stat.executeQuery("select * from people;");
                while (rs.next()) {
                    System.out.println("name = " + rs.getString("name"));
                    System.out.println("job = " + rs.getString("occupation"));
                }
                //分页时,可以使用ResultSet来完成分页rs.absolute(100),也可以sql语句中完成分页select ... limit 100,10;
                //下面是释放数据库连接的过程,使用数据库连接池时不应该释放连接,而是将连接重新放到连接池中。
                //以代理的方式生成Connection的对象,调用Connection的close方法时将Connection加入到线程池中。线程池中加入的是Connection的代理对象即可。
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } finally {
                        if (prep != null) {
                            try {
                                prep.close();
                            } finally {
                                if (conn != null) {
                                    conn.close();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

     我们可以在工程的根目录下发现一个名为test.db的文件,这就是刚创建的数据库文件。我们也可以指定test.db为其他的路径下的SQLite数据库文件。

      下面是一些SQLite的其他常用的命令:

      设置格式化查询结果: sqlite> .mode column  

                 sqlite> .header on 

      修改表结构,增加列:sqlite> alter table mytable add column email text not null '' collate nocase;

      创建视图: sqlite> create view nameview as select * from mytable;

      创建索引:sqlite> create index test_idx on mytable(value);

      格式化输出数据到 CSV 格式:sqlite >.output [filename.csv ]  

                    sqlite >.separator ,  

                    sqlite > select * from test;  

                    sqlite >.output stdout

      从 CSV 文件导入数据到表中:sqlite >create table newtable ( id integer primary key, value text );  

                    sqlite >.import [filename.csv ] newtable

      

  • 相关阅读:
    Vue:不同页面之间的传递参数(二)---query
    Vue:不同页面之间的传递参数--params
    Vue:将px转化为rem,适配移动端
    CSS变量:声明全局变量,让编写更快捷 --root
    前端:viewport-fit解决iphoneX的“刘海”问题
    css小技巧---:not()
    分析Array.apply(null, { length: 5 })
    对比jquery获取属性的方法props、attr、data
    在浏览器里点击input输入框输入,会展示默认的历史下拉菜单
    记录下 Markdown 语法
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/4609161.html
Copyright © 2011-2022 走看看