我是在web工程下直接实验的
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DB2Connect {
public static Log log = LogFactory.getLog(DB2Connect.class);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String uri = "jdbc:db2:/127.0.0.1:50000/temp";
String userName = "admin";
String passWord = "admin";
public DB2Connect() {
try {
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").isInterface();
} catch (ClassNotFoundException e) {
log.error(e.getMessage());
}
}
public Connection setConn() {
try {
conn = DriverManager.getConnection(uri, userName, passWord);
} catch (SQLException e) {
log.error(e.getMessage());
}
System.out.println(conn);
return conn;
}
public void closeConn() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
log.error(e.getMessage());
}
}
public static void main(String[] args) {
DB2Connect db=new DB2Connect();
db.setConn();
db.closeConn();
}
}
------解决方案--------------------
能行才见鬼了呢。db2java.zip这种驱动要求要安装db2客户端,而且要对目标数据库编目,类似于oracle的oci驱动。jdbc:db2:/127.0.0.1:50000/temp这种url得用db2jcc.jar和db2jcc_license_cu.jar这两个包。
db2java.zip的连接主要参数:
driver=COM.ibm.db2.jdbc.app.DB2Driver"
url=jdbc:db2:[catalog]
catalog是目标数据库本地编目名称。
用db2jcc_license_cu.jar和db2jcc.jar驱动连接需要用如下参数
driver=com.ibm.db2.jcc.DB2Driver
url=jdbc:db2:/127.0.0.1:50000/temp