1. 首先要在工程中加入DB2连接文件db2jcc.jar, 该文件可从DB2安装目录中找到.
2. 建立连接:
public void setUp() throws Exception { Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); Properties connProperties = new Properties(); connProperties.put("user", "ADMINISTRATOR"); connProperties.put("password", "liguoliang.com"); connection = DriverManager.getConnection(db2.getJdbcUrl("192.168.1.113", 50000, "meta"), connProperties); }
3. 执行SQL语句举例:
public void validateConnection(Connection connection) throws SQLException { if(connection == null) { throw new SQLException("Connection is null"); } Statement stmt = null; ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery("VALUES(1)"); // 该语句将返回1 rs.next(); if(rs.getInt(1) != 1) { throw new SQLException("SQL Exception: values(1) != 1"); } }finally { JDBCUtilities.close(rs); } }
有用的链接:
用java访问数据库DB2代码的实际操作 http://database.51cto.com/art/201008/216908.htm
使用 JDBC 连接不同版本 DB2 数据库的兼容性问题 http://www.ibm.com/developerworks/cn/data/library/techarticles/0402chenjunwei/0402chenjunwei.html#author1
com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.59.81] Exception java.net.ConnectException: Error opening socket to server /192.168.1.113 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
第一次使用JDBC连接DB2数据库时, 出现上述错误.
改动了下端口, 结果重新start时出现错误.
在本机使用DB2正常, 但无法从远程使用. telnet 50000端口失败.
本机telnet 50000端口, 失败.
db2cmd中报错:
SQL5043N Support for one or more communications protocols failed to start sussfully. However, core database manager functionality started successfully.
简单总结Windows下DB2端口有关操作.
1. 在DB2命令行中, 运行: DB2 GET DBM CFG |FIND "SVCENAME", 将会获得如下输出:
TCP/IP Service name (SVCENAME) = db2c_DB2
2. 打开c:\windows\system32\drivers\etc\services, 查看1中找到的服务名称对应的端口号, 如:
db2c_DB2 50000/tcp
直接修改ttc\Services中的对应端口. 然后db2stop, db2start即可.
可通过telnet ip:port 检查是否已启动.
1. 增加Service: 编辑etc\services文件, 增加Service名称及端口, 如增加:
db2c_MYINST 50006/tcp
2. 分配端口:
db2 UPDATE DBM CFG USING SVCENAME db2c_MYINST
参考: DB2 Instance实例 的创建 http://topic.csdn.net/t/20030704/12/1990179.html
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.