アットウィキロゴ

DB2

条件

db2jcc.jar、db2jcc_license_cu.jarを追加して参照できるようにする

サンプル

import java.sql.*;
 
public class Sample{
 
    public static void main(String[] args) {
 
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
 
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet result = null;
 
        try {
 
            conn = DriverManager.getConnection("jdbc:db2://localhost:50000/sample",
                                               "user", "password");
            ps = conn.prepareStatement("SELECT * FROM employee");
            result = ps.executeQuery();
 
            while (result.next()) {
            	System.out.print(result.getInt("EMPNO"));
            	System.out.print(" ");
            	System.out.println(result.getString("FIRSTNME"));
            }
 
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) { }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) { }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) { }
            }
        }
    }
}
 
接続文字列には以下の書式
jdbc:db2://サーバ名(IPアドレス):ポート/データベース名




最終更新:2012年09月02日 10:46