JDBC access DB2

本文档介绍了使用JDBC访问DB2数据库的先决条件,包括安装DB2 JDBC驱动和许可证JAR,并提供了示例Java代码来列出数据库/节点配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sample program using JDBC access DB2 database.


Prerequisite: installing DB2 JDBC driver and license JARs

The DB2 Universal JDBC Driver.

1. db2jcc.jar : includes functions in the JDBC 3.0 and earlier specifications, or
2. db2jcc4.jar : includes functions in JDBC 4.0 and later, as well as JDBC 3.0 and earlier specifications.

The DB2 JDBC License JARs

1. db2jcc_license_cisuz.jar: contains licenses for Linux, Unix, Windows®, IBM System i®, and IBM System z.
2. db2jcc_license_cu.jar. This alternate license is not sufficient to connect to DB2 running on z/OS.

Sample Java Code

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

public class DB2Test {
    public static void main(String[] argv) {
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        }
        catch (ClassNotFoundException e) {
            System.out.println("Please include classpath where your DB2 Driver is located");
            e.printStackTrace();
            return;
        }
        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:<dbname>","<username>","<passwd>");
            //or, if you want to connect remove DB2 server
            // conn = DriverManager.getConnection("jdbc:db2://<hostname>:<port>/<dbname>","<username>","<passwd>");
            if (conn != null)
            {
                System.out.println("DB2 Database Connected");
            }
            else
            {
                System.out.println("Db2 connection Failed ");
            }
            pstmt=conn.prepareStatement("select * from testtab");
            rset=pstmt.executeQuery();
            if(rset!=null)
            {
                while(rset.next())
                {
                    found=true;
                    System.out.println("a=[" + rset.getInt("a") + "]");
                    System.out.println("b=[" + rset.getString("b") + "]");
                }
            }
            if (found ==false)
            {
                System.out.println("No Information Found");
            }
        } catch (SQLException e) {
            System.out.println("DB2 Database connection Failed");
            e.printStackTrace();
            return;
        }
    }
}

Compile and Run

#!/bin/ksh

DB2CLIENT=/home/db2inst1

javac DB2Test.java
java -cp .:${DB2CLIENT}/sqllib/java/db2jcc.jar:${DB2CLIENT}/sqllib/java/db2jcc_license_cisuz.jar DB2Test

List Database/Node Configuration

$ db2 list database directory

 System Database Directory

 Number of entries in the directory = 2

Database 1 entry:

 Database alias                       = <DBNAME>
 Database name                        = <DBNAME>
 Node name                            = <NODENAME>
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Remote
 Authentication                       = SERVER
 Catalog database partition number    = -1
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = <DBNAME>
 Database name                        = <DBNAME>
 Local database directory             = /home/db2inst1
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

$ db2 list node directory    

 Node Directory

 Number of entries in the directory = 1

Node 1 entry:

 Node name                      = <NODENAME>
 Comment                        =
 Directory entry type           = LOCAL
 Protocol                       = TCPIP
 Hostname                       = <HOSTNAME>
 Service name                   = <PORT>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值