Java Sample Code to connect to DB2 Database using JDBC Driver

The following example code is for connecting and accessing DB2 database. It loads the DB2 JDBC Driver and connects to the DB2 database Student and prints the records of the qual table. For running the program you have to do the following. Assume that Student database , qual table already been created in DB2.

1. Download DB2 JDBC driver.
2. Include db2 driver path in the CLASSPATH

Steps to include to db2 drivers in the class path
copy the db2 driver files db2jcc.jar, db2jcc_license_cisuz.jar, db2jcc_license_cu.jar, to any folder for example d:\db2drivers. Now set the classpath to include the driver files as follows.

d:\javaonline>set CLASSPATH=.;d:\db2drivers\db2jcc.jar;d:\db2drivers\db2jcc_license_cisuz.jar;d:\db2drivers\db2jcc_license_cu.jar;d:\db2drivers\;.

OR permanantly set by Right click on MyComputer ->Advanced Tab – > Environment variables – > select CLASSPATH and edit, set the variable value to above path. For running in Servlet / DAO in struts include the above driver files in the libraries (WebContent\WEB-INF\lib). If you use any developer tool like WSAD, Myeclipe, Use Add External Jar from Project -> Java Build Path -> Libraries Tab -> Add External Jars.

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class DB2Connect {
 public static void main(String[] argv) {
 try {
 } catch (ClassNotFoundException e) {
 System.out.println("Please include Classpath Where your DB2 Driver is located");
 System.out.println("DB2 driver is loaded successfully");
 Connection conn = null;
 PreparedStatement pstmt = null;
 ResultSet rset=null;
 boolean found=false;
 try {
 conn = DriverManager.getConnection("jdbc:db2:student");
 if (conn != null)
 System.out.println("DB2 Database Connected");
 System.out.println("Db2 connection Failed ");
 pstmt=conn.prepareStatement("Select * from qual");
 System.out.println("Class Code: "+rset.getString("clcode"));
 System.out.println("Name: "+rset.getString("name"));
 if (found ==false)
 System.out.println("No Information Found");
 } catch (SQLException e) {
 System.out.println("DB2 Database connection Failed");

Output if the qual table has 3 records

D:\javaonline>java DB2Connect

DB2 driver is loaded successfully

DB2 Database Connected

Class Code: 1

Name: MCA

Class Code: 2

Name: BE

Class Code : 3

Name : BTech

Leave a Reply

pro v4 male enhancement sex power medicine male erection pills that work how to last longer in bed without using pills 18 4 ever male enhancement xzen gold male enhancement natural male enhancement walgreens alpha rx male enhancement system enerex male enhancement penis enlargement actual surgery growxl male enhancement review pines growth medicine vitality pill black ant king pills male enhancement jack rabbit pills for sale extenz pills natural boner pills what does pills like extenze do blue pill online penis enlargement surgery thailand cost male power pills extentions penis pills strongest ed pills buy ed pills at gnc zyx10 male enhancement store bought natural male enhancement success rate of male enhancement side effects to extenze pills male enhancement advertising on 1037 fm vesele pill pills to reduce male libido pills that keep you erect blood enhancer reviews on erectzan male enhancement pills dr phil penis pills generic male enhancement pills sex pills shop near me best male sexual enhancer amazon prime wholesale dealer for sex pills sarms and penis enlargement stree overlord sex pills good natural male enhancement top sex pills review blue pill pfizer vgr 100 king of lion sex pills stinger male enhancement pill red pill sidebar male enhancement pills shoppers have morning wood but ed pills dont work 72hr sex pills malaysian pills to take before sex to prevent pregnancy calcium supplements and ed

gnc cbd cbd shower gel 7 hemp cbd oil reviews cbd oil no high cbd oil and fibromyalgia buy cbd oil gummies cbd oil vaping cbd manufacturers near me cbd oil murfreesboro tn how long does it take cbd oil to work cbd patches for sale cbd for tumors etsy cbd oil charlottes web cbd gummies review cbd oil hunger cbd oil for pinched nerve in neck cbd oil for agoraphobia cbd oil bulk cbd security clearance extract cbd oil cbd oil bottles liquid cbd 100 best cbd for bone healing cbd developments pure cbd oil with thc cbd jobs near me carrier oil extraction cbd strain with highest cbd content best cbd edibles reddit daily cbd dose indianapolis cbd oil wellington cbd apartments rent 60mg cbd gummies where to buy cbd oil in huntsville al cbd oil vs hemp seeds adelaide cbd to airport mappen sydney cbd sydney nsw cloud 9 south padre island cbd oil marys nutritionals cbd patch review lenitivlabs cbd oil better u cbd oil level 5 cbd oil cbd oil products near me cbd clones cbd oil for mono treating anxiety with cbd oil plus cbd oil test positive for thc cbd oil l where to find cbd oil in lakeland select cbd oil sell cbd oil texas accommodation auckland cbd

hormone imbalance weight loss pill dairy diet and weight loss medicine with weight loss side effects can you do the keto diet without the pills which weight loss pills work t3 drug weight loss keto diet pills reddit weight loss over the counter pills that work pancakes keto diet recipes with cream cheese repairvite diet weight loss milkshake diets lose weight fast changed diet cat weight loss diets for men to lose weight hypoglycemic diet plan for weight loss salt free diet plan weight loss extreme weight loss pills with ephedra diet for weight loss bodybuilding weight loss supplements dr oz recommends dim tablets for weight loss best weight loss diet plans 2020 skinny fiber weight loss pill best fiber pills to lose weight diet plan for weight loss after stroke patients vitamin b12 weight loss pills force factor weight loss pills reviews weight loss diet chart vegetarian weight loss pills xoveree by applecare weight loss diets for men reviews dr gessner weight loss pills weight loss pills that make you horny no doz and weight loss pills diet but no weight loss is it wrong to use weight loss pills jordyn woods weight loss diet weight loss pills fresno ca beware of keto burn diet pills side effects vata weight loss diet diet plan for pcos to lose weight natural diet weight loss pill over the counter pills to help lose weight weight loss 20 exercise 80 diet green tea in weight loss pills weight loss success with liquid diet number one weight loss pill for women high protein diet for weight loss and diabetes top ten weight loss prescription pills how to restart weight loss diet dr recommended weight loss pills how to maintain weight loss on keto diet macros for weight loss low fat diet easy way to lose weight with pills how fast will i lose weight on a keto diet