jrunscript+RhinoでSqlite3に接続(JDBC)

foo/data.sqlite3 がまだ作られてない状態で

$ sqlite3 foo/data.sqlite3

Sqlite3のコンソールで操作:

create table people (id ,name);
insert into people values (1, 'Athos');
insert into people values (2, 'Aramis');
insert into people values (3, 'Porthos');
insert into people values (4, 'd''Artagnan');

sample.js:

var url = "jdbc:sqlite:foo/data.sqlite3";
var conn = java.sql.DriverManager.getConnection(url);
var stmt = conn.prepareStatement("select * from people");
var rs = stmt.executeQuery();

while(rs.next()){
  var id = rs.getInt(1);
  var name = rs.getString(2);
  println("id=" + id + ", name=" + name);
}

rs.close();
stmt.close();
conn.close();

Sqlite/JDBC接続用のドライバ(jar)を用意して次のように実行

$ jrunscript -classpath lib/sqlite-jdbc-3.7.15-M1.jar sample.js 

結果:

id=1, name=Athos
id=2, name=Aramis
id=3, name=Porthos
id=4, name=d'Artagnan



環境:

$ java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1~0.12.10.1)
OpenJDK Server VM (build 24.51-b03, mixed mode)
$ jrunscript -q
Language ECMAScript 1.7 implemention "Rhino" Rhino 1.7 release 3 2012 05 18



関連: