Take the 2-minute tour ×

I need to access DB2 database in plain standalone Java project.

I cannot use datasource from container right? Do I need to write JDBC connection?

share|improve this question

4 Answers 4

up vote 1 down vote accepted

You need to have necessary db2 JDBC jars in your program's compile time, runtime classpath. If you have native DB2 client installed on target machines, you may use JDBC ODBC Bridge. However, best to stick with pure Java- type4 drivers. (db2jcc.jar, db2jcc_license*.jar etc)

Once the drivers are in classpath, you may use usual JDBC code to start with. Such as:

import java.sql.*;
import java.lang.*;
import java.io.*;
import Com.ibm.db2.jcc.*;  //Type4 library
public class DB2Sample{

    catch (ClassNotFoundException e)
       System.err.println("Could not load DB2 driver \n");

public static void main(String args[]) 

    /* Type 4 driver url */
    String url = "jdbc:db2j:net://machine-name:port-number/TGSAMPLE";
         Connection conn = DriverManager.getConnection(url,"userid", "psswrd");

and so on.

Look at IBM documentation here for better examples and details

share|improve this answer
I am geting this exception: java.sql.SQLException: No suitable driver, I am using the same calss. Any idea –  user234194 Jul 6 '10 at 14:42
Ensure that you have the required JAR files in your CLASSPATH. How are you running the example? Also, make sure you have right parameters for connection - such as url, user and password; Can you share your code? –  ring bearer Jul 7 '10 at 2:00

JDBC connection

share|improve this answer

Depending on your needs, either handle a JDBC connection yourself or use a standalone JDBC pool e.g. C3P0, DBCP or BoneCP.

share|improve this answer


You can try to get the connection from the container using JNDI Lookup. For instance see this.

But I think it would be much easier not do it.

Do I need to write JDBC connection?

Yes, if it's easier for you to manage the connection your self.

You can also use a connection pool such as Apache DBCP or C3PO. Here's a list of others that support Connection pooling for standalone app

share|improve this answer

Your Answer


