美文网首页
Sqlite_JDBC

Sqlite_JDBC

作者: Tree27th | 来源:发表于2019-03-21 14:30 被阅读0次

写一个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!");

    }

}

相关文章

  • Sqlite_JDBC

    写一个Java连接到 Sqlite的代码 public class SqliteJDBC{ public st...

网友评论

      本文标题:Sqlite_JDBC

      本文链接:https://www.haomeiwen.com/subject/okbqvqtx.html