JDBC TUTORIAL

Connect to any database or SaaS applications using JDBC drivers from Node.js

Updated: 26 Feb 2021

Introduction

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.

Pre-requisites

Have Node.js installed on your machine

Download and Install DataDirect JDBC driver

  1. Go to our website and download the JDBC driver for the datasource you are trying to connect.

    Note: If you are downloading for use on Linux/Unix, the downloaded jar file is not the driver, but an installer.

     

  2. After you have finished downloading the jar file, run the package to install the JDBC driver.
    1. For Windows, run the installer .exe file to install the JDBC driver
    2. For Unix/Linux, run the following command to install the driver

      java -jar PROGRESS_DATADIRECT_JDBC_INSTALL.jar

Install Node.js JDBC package ​

  1. In your Node.js, you need to install node-jdbc package to be able to use JDBC drivers.
  2. To install the package, run
    npm i jdbc


  3. Copy the DataDirect JDBC driver from /install_dir/Progress/JDBC_XX/lib to your project library. In this case I installed Salesforce driver on my machine and I copied the sforce.jar, which is the Salesforce JDBC driver.

Connect to Salesforce from Node.js

  1. Following is a sample code on how you can connect to Salesforce using Progress DataDirect JDBC driver and query a table. This is just a starter code, you can modify this as per your needs.

    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);
              }
            });
          });
        }
      });
  2. Notice the method setupClasspath, where you would have to give a path to the DataDirect Salesforce JDBC driver. When you run the above code, it should print the count of records and the data in your table to console.

    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.


Connect any application to any data source anywhere

Explore all DataDirect Connectors

Need additional help with your product?

Get Customer Support