sqlite的Java应用-入门

前端之家收集整理的这篇文章主要介绍了sqlite的Java应用-入门前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.应用Demo

Java项目需导入jar包sqlitejdbc-v056.jar

import java.sql.*;

public class Test {
  public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection 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);");
    PreparedStatement 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);

    ResultSet rs = stat.executeQuery("select * from people;");
    while (rs.next()) {
      System.out.println("name = " + rs.getString("name"));
      System.out.println("job = " + rs.getString("occupation"));
    }
    rs.close();
    conn.close();
  }
}

运行结果,在项目根路径生成test.db数据库文件


2. sqlite自增ID自段使用方法:

INTEGER PRIMARY KEY AUTOINCREMENT

CREATE TABLE user (
 id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(100) NOT NULL,date DATE 
);
原文链接:https://www.f2er.com/sqlite/202312.html

猜你在找的Sqlite相关文章