Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Moya Richards
Moya Richards

Posted on • Edited on

     

How to connect Node.js to IBM DB2 database - windows

It is good to connect with the right people. For months now I thought that we had to pay over 30k for the license to use DB2 with node. Today I contacted the right person at IMB. I sent a message to the main person on the node team through LinkedIn, he messaged me back right away and told me about the right tools to use. In less than 10 minutes, guess who connected node to DB2? Me.

I swear I have been googling this for months. Maybe I was typing in the wrong search terms because I wasn't finding what I needed, I kept seeing information about ibm-db.


So if you come across this issue and you want to use IBM DB2 with node, what you need is one out of the following node modules. By the way, none of them have licensing fees:

Choose one of the following (in order of preference):

Note: Theodbc option is most flexible and works outside of the i-series environment, on operating systems such as Windows and Linux.

  • odbc

  • idb-pconnector

  • idb-connector

Note: These package only installs on IBM i systems.


Connect Node to a DB2 Database

First of all, you will need a connection string

*If you have used php in the past *, your connection string might look like this:

$dbconn=odbc_connect('usrProd','username1','password1',SQL_CURSOR_FORWARD_ONLY)
Enter fullscreen modeExit fullscreen mode

** But in node your connection string is like this**

const cn = "DRIVER=IBM i Access ODBC Driver;SERVER=host;UID=user;PWD=password;DATABASE=dbname"

If you already have a DNS setup on your system, you can shorten the connection syntax like so, by providing the DSN information and the username and password

const cn = "DSN=datasourceName;UID=username;PWD=password";


You can learn more about how to install the odbc driver and setup the DSN by visitingIBM/ibmi-oss-examples on github


Sample code to connect Node to DB2

constodbc=require("odbc");constcn="DSN=usrProd;UID=username1;PWD=password1";odbc.connect(cn,(error,connection)=>{connection.query("SELECT * FROM QIWS.QCUSTCDT FETCH FIRST 6 ROWS ONLY",(error,result)=>{if(error){throwerror;}console.log(result);});});
Enter fullscreen modeExit fullscreen mode

% post moyarich/how-to-connect-node-js-to-ibm-db2-database-16m6 %}

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
bimalkjha profile image
Bimal Kumar Jha
A Software Developer working on C, C++, Node, Python, Java and other open source drivers.
  • Location
    Bangalore
  • Work
    Senior Software Engineer at Rocket Software
  • Joined
• Edited on• Edited

To connect a Db2 for z/OS, Db2 for AS400, Db2 for LUW or Db2 on Cloud/Dashdb or bigsql server using node.js application, you just need to install ibm_db package.

npm install ibm_db

and you are ready to go. You can install ibm_db on MacOS, Windows, Linux, zLinux, AIX, Sun, Sunamd, Linuxppc and z/OS platforms.
Check it for more info:github.com/ibmdb/node-ibm_db

CollapseExpand
 
bskimball profile image
Brian Kimball
  • Joined

What about the DB2Connect licensing mentioned in the readme? I think that was the point of using the odbc connection instead

CollapseExpand
 
michelc profile image
Michel
French developper, from QB Compiler 2, Visual Basic and C# for work. Also like testing Ruby/Sinatra and JavaScript/Node.
  • Location
    France
  • Work
    LeMOT (Wordle en français) : https://www.solitaire-play.com/lemot/
  • Joined

That sounds pretty good. Did you use the "odbc" package on Windows? Because I am currently connecting to DB2 with ODBC from a WinForm client, and with Node I could try to automate some queries.

CollapseExpand
 
moyarich profile image
Moya Richards
Senior Software Developer
  • Location
    USA
  • Education
    Devry University
  • Work
    Senior Software Developer
  • Joined
• Edited on• Edited

Ohh I sure did:

this is a github link with a massive amount of mocha test cases that you can use to practice how to run queries using node:github.com/markdirish/node-odbc-ac...

odbc datasource

CollapseExpand
 
obito profile image
obito
  • Joined

Can u tell me or post the link which where should u download the iSeries Access ODBC Driver ?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Developer
  • Location
    USA
  • Education
    Devry University
  • Work
    Senior Software Developer
  • Joined

More fromMoya Richards

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp