Programming Geek
Rated 4.1/5 based on 446 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 {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("Driver class not found\nexiting...");
        }
        System.out.println("Connecting...");
        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) {
            System.out.println("connected!!!");
        } else {
            System.out.println("failed to connect!!!\n exiting...");
            return;
        }
        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 (rs.next()) {
                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 “DB2Conn.java”.

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

To Compile:

Javac DB2Conn.java

To run:

Java DB2Conn

See the screenshot below: