jdbc

Node Module JDBC wrapper

node-jdbc

JDBC Wrapper for node.js

0.0.13

Support for adding multiple JARs to the classpath has been added. Use the libs array as shown in the initialize section below. As of release 0.0.9, the minimum version of node.js has been increased to v0.10. If you need to use this with node.js v0.8, use version 0.0.8 of node-jdbc.

Please visit node-jdbc for information on installing with npm.

var jdbc = new ( require('jdbc') );
 
var config = {
  libpath: __dirname + 'path/to/jdbc.jar',
  libs: [__dirname + 'path/to/other/jars.jar'],
  drivername: 'com.java.driverclass',
  url: 'url/to/database',
  // optionally   
  user: 'user',
  password: 'secret',
};
 
jdbc.initialize(config, function(errres) {
  if (err) {
    console.log(err);
  }
});
var genericQueryHandler = function(errresults) {
  if (err) {
    console.log(err);
  } else if (results) {
    console.log(results);
  }
  
  jdbc.close(function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log("Connection closed successfully!");
    }
  });
 
};
 
jdbc.open(function(errconn) {
  if (conn) {
    // SELECT statements are called with executeQuery 
    jdbc.executeQuery("SELECT * FROM table", genericQueryHandler);
 
    // Table modifying statements (UPDATE/INSERT/DELETE/etc) are called with executeUpdate 
    jdbc.executeUpdate("UPDATE table SET column = value", genericQueryHandler);
 
    // Use non-generic callbacks to handle queries individually and/or to nest queries 
    jdbc.executeUpdate("INSERT INTO table VALUES (value)", function(errresults) {
      
      if(results > some_arbitrary_value) {
        jdbc.executeQuery("SELECT * FROM table where column = value", genericQueryHandler);
      }
    
    });
  }
});
 
 
  • see above example for config object
  • callback(error)
  • opens a new connection
  • callback(error)
  • closes any existing connection
  • callback(error)
  • SELECT commands.
  • callback(error, rset)
  • table modifying commands (INSERT, UPDATE, DELETE, etc).
  • callback(error, num_rows) where @num_rows is the number of rows modified