Java with SQLite

Supporting Java SE version 7 and up
djfalkenstein

Java with SQLite

Postby djfalkenstein » Wed Aug 31, 2011 7:03 am

I am chugging along with my projects at breakneck speeds, but I've hit a wall. The SQLite adapter for Java (I am using the Xerial variant).

I don't think I am using it correctly as I have package registration errors thrown from my code.

Does anyone have a quick-start guide or boilerplate for using Java to query SQLite with respect to Phidgets?

Most appreciated.

anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Re: Java with SQLite

Postby anewsome » Wed Aug 31, 2011 5:42 pm

Hi David. My program also uses SQLite. It's working nicely. The sample code on their site got me started.

I simply compiled the code and it showed me everything I knew to get started. Here's the code:

Code: Select all

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();
  }
}

djfalkenstein

Re: Java with SQLite

Postby djfalkenstein » Thu Sep 01, 2011 6:49 am

Thank you!


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest