Programming Geek
Rated 4.7/5 based on 1446 reviews

JAVA Program to Establish Connection with IBM DB2

JAVA Program to Establish Connection with IBM DB2 database and executing several Queries

The following program demonstrates the connection with IBM DB2 using jdbc driver. To run the below program you need to perform following steps:

Step1:  Download the jdbc driver here.
Step2: Include jdbc driver in “CLASSPATH” environment variable or copy      “db2jcc.jar”and ”db2jcc4.jar” files to”\jdk1.7.0\jre\lib\ext”.
where is your directory where JAVA is installed . In my case it is “C:\Program Files (x86)\Java”

Copy the program given below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

 * @author VIK
public class DB2Conn {

    public static void main(String[] args) {

        Connection con = null;
        Statement st = null;
        PreparedStatement pstmt = null;
        //load driver class
        System.out.println("Loading driver class...");
        try {
        } catch (ClassNotFoundException e) {
            System.err.println("Driver class not found\nexiting...");
        try {
            con = DriverManager.getConnection("jdbc:db2://localhost:50000/SAMPLE", "db2admin", "db2admin");

        } catch (SQLException e) {
            System.err.println("Failed to connect to DB2\nexiting...");

        if (con != null) {
        } else {
            System.out.println("failed to connect!!!\n exiting...");
        try {
            st = con.createStatement();
            //to create table with blob field (One time only)
            st.executeUpdate("CREATE TABLE student(sroll integer,sname varchar(10) , sadd varchar(50))");
        } catch (SQLException e) {
            System.err.println("Table already exists...");
        System.out.println("Inserting Data in table...");
        try {
            pstmt = con.prepareStatement("INSERT INTO student VALUES(?,?,?)");
            pstmt.setInt(1, 1);
            pstmt.setString(2, "VIKASH");
            pstmt.setString(3, "Dhanbad");
            int result = pstmt.executeUpdate();
            if (result >= 1) {
                System.out.println("Details added successfully...!");
        } catch (SQLException e) {
            System.err.println("Error in inserting details...");
        try {
            //Retrieving details from database...
            System.out.println("Retrieving details from database...");
            ResultSet rs = st.executeQuery("select * from STUDENT");
            while ( {
                System.out.println("SROLL : " + rs.getInt(1) + "  SNAME : " + rs.getString(2) + "  SADD : " + rs.getString(3));
        } catch (SQLException e) {
            System.err.println("Error in retrieving details...\nexiting...");


Step3: Save the program as “”.

Step4: Open command prompt in directory where is present and type the following command to compile and execute the program .

To Compile:


To run:

Java DB2Conn

See the screenshot below: