Created
February 10, 2013 21:29
-
-
Save mchirico/4751124 to your computer and use it in GitHub Desktop.
Example using the sqlite-jdbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.sysadmin; | |
/* | |
* https://bitbucket.org/xerial/sqlite-jdbc/overview | |
* | |
* | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class SQLite { | |
private Connection connection = null; | |
private Statement statement; | |
private ResultSet rs; | |
public SQLite(String File) { | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
connection = DriverManager.getConnection("jdbc:sqlite:" + File); | |
// example of how you would do it in memory | |
// Connection connection = | |
// DriverManager.getConnection("jdbc:sqlite::memory:"); | |
statement = connection.createStatement(); | |
statement.setQueryTimeout(30); | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public SQLite(String File, int TimeOut) { | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
connection = DriverManager.getConnection("jdbc:sqlite:" + File); | |
// example of how you would do it in memory | |
// Connection connection = | |
// DriverManager.getConnection("jdbc:sqlite::memory:"); | |
statement = connection.createStatement(); | |
statement.setQueryTimeout(TimeOut); | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public void executeUpdate(String s) { | |
try { | |
statement.executeUpdate(s); | |
/* | |
* statement.executeUpdate("drop table if exists person"); statement | |
* .executeUpdate("create table person (id integer, name string)"); | |
* statement.executeUpdate("insert into person values(1, 'leo')"); | |
* statement.executeUpdate("insert into person values(2, 'yui')"); | |
*/ | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public void select(String s) { | |
try { | |
rs = statement.executeQuery(s); | |
while (rs.next()) { | |
// read the result set | |
ResultSetMetaData rsmd = rs.getMetaData(); | |
int colCount = rsmd.getColumnCount(); | |
// System.out.println("colCount:" + colCount); | |
String sep = ""; | |
for (int i = 1; i < colCount + 1; ++i) { | |
System.out.print(sep + rs.getString(i)); | |
sep = ","; | |
} | |
System.out.println(); | |
} | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public void close() { | |
try { | |
if (connection != null) | |
connection.close(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
public static void main(String[] args) { | |
SQLite sp = new SQLite("/tmp/sqlitejava.db"); | |
sp.executeUpdate("drop table if exists person"); | |
sp.executeUpdate("create table person (id int, name string,timeEnter DATE)"); | |
sp.executeUpdate("DROP TRIGGER IF EXISTS insert_person_timeEnter"); | |
sp.executeUpdate("CREATE TRIGGER insert_person_timeEnter AFTER INSERT ON person " | |
+ "BEGIN " | |
+ "UPDATE person SET timeEnter = DATETIME('NOW') WHERE rowid = new.rowid; " | |
+ "END;"); | |
for (int i = 0; i < 5; ++i) | |
sp.executeUpdate("insert into person (id,name) values (" + i | |
+ ",'temp" + i + "')"); | |
sp.executeUpdate("insert into person (id,name) values (200,'sue')"); | |
sp.select("select * from person"); | |
sp.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment