If you are looking to connect to your databases like SQL Server, Oracle, DB2, Postgres, MongoDB etc., or SaaS apps like Salesforce, Eloqua, Oracle Sales Cloud, Oracle Service Cloud etc., using a JDBC driver from your Node.js application, this is the place to get started.
Have Node.js installed on your machine
Note: If you are downloading for use on Linux/Unix, the downloaded jar file is not the driver, but an installer.
java -jar PROGRESS_DATADIRECT_JDBC_INSTALL.jar
npm i jdbc
var
JDBC = require(
'jdbc'
);
var
jinst = require(
'jdbc/lib/jinst'
);
var
asyncjs = require(
'async'
);
if
(!jinst.isJvmCreated()) {
jinst.addOption(
"-Xrs"
);
jinst.setupClasspath([
'./drivers/sforce.jar'
]);
}
var
config = {
// SparkSQL configuration to your server
url:
'jdbc:datadirect:sforce://login.salesforce.com;DatabaseName=default;SecurityToken=stoken'
,
drivername:
'com.ddtek.jdbc.sforce.SForceDriver'
,
minpoolsize: 1,
maxpoolsize: 100,
user:
'saiteja09@gmail.com'
,
password:
'password'
,
properties: {}
};
var
sforcesqldb =
new
JDBC(config);
//initialize
sforcesqldb.initialize(
function
(err) {
if
(err) {
console.log(err);
}
});
sforcesqldb.reserve(
function
(err, connObj) {
if
(connObj) {
console.log(
"Using connection: "
+ connObj.uuid);
var
conn = connObj.conn;
// Query the database.
asyncjs.series([
function
(callback) {
// Select statement example.
conn.createStatement(
function
(err, statement) {
if
(err) {
callback(err);
}
else
{
statement.setFetchSize(100,
function
(err) {
if
(err) {
callback(err);
}
else
{
//Execute a query
statement.executeQuery(
"SELECT * FROM SFORCE.Account;"
,
function
(err, resultset) {
if
(err) {
callback(err)
}
else
{
resultset.toObjArray(
function
(err, results) {
//Printing number of records
if
(results.length > 0) {
console.log(
"Record count: "
+ results.length);
console.log(results);
}
callback(
null
, resultset);
});
}
});
}
});
}
});
},
],
function
(err, results) {
// Results can also be processed here.
// Release the connection back to the pool.
sforcesqldb.release(connObj,
function
(err) {
if
(err) {
console.log(err.message);
}
});
});
}
});
You can use similar steps with any of DataDirect JDBC suite of drivers available for Relational databases like SQL Server, MySQL, PostgreSQL etc., Big Data sources like Hive, Spark etc., SaaS sources like Salesforce, Eloqua, Oracle Sales Cloud etc., and NoSQL Data sources like MongoDB. Feel free to try any of our drivers with your Node.js apps for connecting to your datasource of your choice.