Fork me on GitHub Fork me on GitHub

Database


<%
var db = new Database("jdbc:h2:tmp/demo", "admin", "admin");
 
db.query("INSERT INTO demo VALUES('WSO2','59 Flower Rd'), ('WSO2 UK', 'Emsworth, Hampshire, UK');");
 
var result = db.query("SELECT * FROM demo");
 
print(result);
%>

Overview

The Database Object is used to connect to a relational database through jaggery and provides most of the default functionality and manipulation through the below members/functions. Jaggery out of the box support H2 database engine, and with little configuration supports MySql and Oracle.
In-order to connect to other database engines, you need to place the connector libs (MySql lib, Oracle lib) into Jaggery runtime. you can copy these files to {JAGGERY_HOME}/carbon/repository/components/lib/


Operations

Operation Returns Description
Database(connectionUrl,
username, password[, config])
Database
var db = new Database("jdbc:h2:demo", "admin", "admin");
Returns Database object for database communication

var config = {
		defaultAutoCommit: boolean,
		defaultReadOnly: boolean,
		defaultTransactionIsolation: "String",
		defaultCatalog: "String",
		maxActive: number,
		maxIdle: number,
		minIdle: number,
		initialSize: number,
		maxWait: number,
		testOnBorrow: boolean,
		testOnReturn: boolean,
		testWhileIdle: boolean,
		validationQuery: "String",
		accessToUnderlyingConnectionAllowed: boolean,
		logAbandoned: boolean,
		connectionProperties: "String",
		initSQL: "String",
		validationInterval: number,
		maxAge: number,
		suspectTimeout: number
}
var db = new Database("jdbc:h2:demo", "admin", "admin", config);
Returns Database object for database communication with optional configurations
query(query, callback)String
db.query("SELECT * FROM myTable", function(results) {})

Query result will be returned to the callback

commit()None
db.commit()
SQL transaction commit
rollback()None
db.rollback()
SQL transaction rollback
close()None
db.close()
SQL connection close