技术开发 频道

DB2驱动类型

  * 测试驱动体系结构为类型4驱动:com.ibm.db2.jcc.DB2Driver, 该驱动位于包db2jcc.jar中.

    *

    * @throws InstantiationException

    * @throws IllegalAccessException

    * @throws ClassNotFoundException

    * @throws SQLException

    */

    public void testJcc() throws InstantiationException,

    IllegalAccessException, ClassNotFoundException, SQLException {

    Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

    String url = "jdbc:db2://192.168.0.17:50000/SAMPLE";

    Connection con = DriverManager.getConnection(url, "db2admin", "cherub");

    con.setAutoCommit(false);

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select * from Employee");

    while (rs.next()) {

    System.out.println(rs.getString(1) + "," + rs.getString(2));

    }

    }

    /**

    * 测试驱动体系结构为类型3驱动:COM.ibm.db2.jdbc.net.DB2Driver, 该驱动位于包 db2java.zip中.

    *

    * @throws InstantiationException

    * @throws IllegalAccessException

    * @throws ClassNotFoundException

    * @throws SQLException

    */

    public void testNet() throws InstantiationException,

    IllegalAccessException, ClassNotFoundException, SQLException {

    Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();

    // 目标DB2系统侦听该服务于默认端口6789,否则还需要在 URL Pattern 中指定目标端口号

    String url = "jdbc:db2:192.168.0.17:SAMPLE";

    Connection con = DriverManager.getConnection(url, "db2admin", "cherub");

    con.setAutoCommit(false);

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select * from Employee");

    while (rs.next()) {

    System.out.println(rs.getString(1) + "," + rs.getString(2));

    }

    }

    /**

    * 测试驱动体系结构为类型2驱动:COM.ibm.db2.jdbc.app.DB2Driver, 该驱动也位于包 db2java.zip中.

    *

    * @throws InstantiationException

    * @throws IllegalAccessException

    * @throws ClassNotFoundException

    * @throws SQLException

    */

    public void testApp() throws InstantiationException,

    IllegalAccessException, ClassNotFoundException, SQLException {

    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();

    String url = "jdbc:db2:sample";

    Connection con = DriverManager.getConnection(url, "db2admin", "cherub");

    con.setAutoCommit(false);

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select * from Employee");

    while (rs.next()) {

    System.out.println(rs.getString(1) + "," + rs.getString(2));

    }

    }

    }
 

0
相关文章