SQLite JDBC Demo

    技术2024-11-11  20

    package cz.sqlite;

    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) {  try {   Class.forName("org.sqlite.JDBC");   Connection conn = DriverManager     .getConnection("jdbc:sqlite:g:/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 limit 1 offset 1;");   while (rs.next()) {    System.out.println("name = " + rs.getString("name"));    System.out.println("job = " + rs.getString("occupation"));   }   rs.close();   conn.close();  } catch (Exception e) {   e.printStackTrace();  } }}

     

      

    最新回复(0)