Sunday, July 26, 2009

Mac OS X: Installing and using SQLITE with Rhino JavaScript

1. Download latest jar from here.
2. Move the jar to ~/Library/Java/Extensions/
3. Start using it in JavaScript as follows:


importPackage(java.sql);
java.lang.Class.forName('org.sqlite.JDBC');

try {
var sDB = 'test.db';
var oConnection = DriverManager.getConnection('jdbc:sqlite:' + sDB);
var oStatement = oConnection.createStatement();

oStatement.executeUpdate('create table t(id smallint, value varchar(25))');

oStatement.executeUpdate('insert into t values(1, "hello")');
oStatement.executeUpdate('insert into t values(2, "world")');

var oResultSet = oStatement.executeQuery('select * from t');
while (oResultSet.next()) {
print(oResultSet.getString('id') + ' – ' + oResultSet.getString('value'));
}

oResultSet.close();
oStatement.close();
oConnection.close();
} catch (e) {
print(e);
}

No comments:

Post a Comment