would publish it. One of the problems some people must solve is data
access to sources without a jdbc driver. I used Phobos to connect to a
Visual Foxpro database. The same technique should be able to be used
for an Access database or other data. The first example just shows a
driver connection. The second example shows a pooled connection using
Apache Commons. The module code for the pooling part is in a previous
blog. This means you can have a pooled data connection from Phobos to
just about any data source. This would not be possible with most Java
application servers, but since we are using our pooling objects provided
by Apache, we can accomplish a much greater variety of data access from
Phobos. Phobos is versatile and flexible.
Driver Example:
library.common.define(controller, "test", function() {
this.Test = function() {
this.onRequest = function() {
// Set up our connection by specifying the driver
java.sql.Class.forName="sun.jdbc.odbc.JdbcOdbcDriver";
// We are using the java odbc bridge with a VFP database
// You need a DSN set up on your machine and for VFP you
// only need to specify the DSN name. User & Pass are blank.
var con = new
java.sql.DriverManager.getConnection("jdbc:odbc:javafox","","");
// create a statement
stmt = con.createStatement();
stmt.setMaxRows(1);
// create the result set by executing the query
// You can specify properties for locking etc if you wish.
rs = stmt.executeQuery("select * from some table");
//Loop through the result set and make a json object
var rsJSON = "{rs: [";
var numcols = rs.getMetaData().getColumnCount();
while (rs.next()) { //Loop through record set
rsJSON += "{"; //resultet row
for (i=1; i<=numcols;i++) {
rsJSON += rs.getMetaData().getColumnName(i) +
": ";
rsJSON += rs.getString(i) + ", ";
} // end for
rsJSON += "},"; //end result set row
} //End while through result set
rsJSON += "]}" model={rs:
rsJSON}
con.close(); //clean up
// render a view with the data
library.view.render("test.ejs");
// Close the connection.
con.close();
}; // End on request function
};
});
Pooled Example:
response.setStatus(200);
response.setContentType("text/html");
writer = response.getWriter();
writer.println("<html><head><title>Phobos</title></head><body>");
var conn = null;
var stmt = null;
var rset = null;
// custom module for pooled connection - start pool (normally done in
startup)
module.vfpDB.startConnectionPool();
writer.println("Pool Started<br>");
writer.println("Creating connection.<br>");
conn =
java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:oConn");
writer.println("Creating statement.<br>");
var stmt = conn.createStatement();
writer.println("Executing statement.<br>");
rset = stmt.executeQuery('select * from some table);
writer.println("Results:<br>");
var numcols = rset.getMetaData().getColumnCount();
while (rset.next()) {
for(i=1;i<=numcols;i++) {
writer.println("<br>" + rset.getString(i));
}
}
writer.println("Closing Pool.<br>");
module.vfpDB.closeConnectionPool();
writer.println("</body></html>");
writer.flush();
0 comments:
Post a Comment