java操作sqlite数据库

第一步 添加依赖

maven依赖:

<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
  <version>3.7.2</version>
</dependency>

第二步 编写代码

  • 加载驱动
  • 获取连接
  • 查询数据
  • 关闭语句和连接
// 加载驱动
Class.forName("org.sqlite.JDBC");

Connection connection = null;

try {
    String questionContent = null;
    String options = null;
    String answers = null;

    // 获取连接
    connection = DriverManager.getConnection("jdbc:sqlite:/Users/root/Downloads/test.db");

    Statement statement = connection.createStatement();

    // 查询数据
    ResultSet resultSet = statement.executeQuery("select * from answers");
    while (resultSet.next()){
        questionContent = resultSet.getString("question_content");
        options = resultSet.getString("options");
        answers = resultSet.getString("answers");

        System.out.println("questionContent  " + questionContent);
        System.out.println("options  " + options);
        System.out.println("answers  " + answers);

    }
    // 关闭语句和连接
    statement.close();
    connection.close();
} catch (
        SQLException e) {
    e.printStackTrace();
}
上一篇:Centos7下升级自带SQLite3


下一篇:SQL学习8.2-SQLite和MySQL的区别