Sqlite_JDBC

写一个Java连接到 Sqlite的代码


public class SqliteJDBC{

    public static void main(String[] args) {

        Connection connection =null;

        Statement statement =null;

        try {

            Class.forName("org.sqlite.JDBC");

            //连接到一个现有的数据库。

            // 如果数据库不存在,将创建一个数据库,最后返回数据库对象

            connection = DriverManager.getConnection("jdbc:sqlite:D://test.db");

            statement = connection.createStatement();

            // 在数据库中创建一个表

            statement.executeUpdate("drop table if exists PERSON");

            statement.executeUpdate("create table PERSON (id integer, name string)");

            // 在表中创建记录

            statement.executeUpdate("insert into PERSON values(1, 'tree')");

            statement.executeUpdate("insert into PERSON values(2, 'cindy')");

            statement.executeUpdate("insert into PERSON values(3, 'jack')");

            // 在表中获取记录并显示出来

            ResultSet resultSet = statement.executeQuery("select * from PERSON");

            while (resultSet.next()) {

                System.out.println("id = " + resultSet.getInt("id"));

                System.out.println("name = " + resultSet.getString("name"));

            }

            resultSet.close();

            // 更新表中原有的记录

            statement.executeUpdate("update PERSON set name = 'tree2' where id = 3");

            // 删除表中的一条记录

            statement.executeUpdate("delete from PERSON where id = 3");

            statement.close();

            connection.close();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

        }

        System.out.println("Successfully!");

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本人的环境为Myeclipse10、MySQL5.7.15 本文包括:简介JDBC编程步骤打通数据库程序详解—Dr...
    廖少少阅读 4,005评论 7 39
  • JDBC简介 SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC。JDBC...
    奋斗的老王阅读 1,550评论 0 51
  • 本节介绍Statement接口及其子类PreparedStatement和CallableStatement。 它...
    zlb阅读 1,194评论 0 0
  • 不知道从什么时候起 喜欢上了一个人静静坐在窗边的案台 看着窗外的雨以各种方式跌落在大地上 看着雨下人儿的千般模样 ...
    有只柠檬阅读 365评论 0 2
  • 第一天:06-15 07:30(福州)福州长乐国际机场飞-10:25(成都)双流机场T2降厦门航空MF8435. ...
    虞筱xie阅读 259评论 0 0