数据库从Java连接到DB2
我尝试从java连接到db2,下面是我的代码:
public class Db2Connection {
public static void main(String[] args) {
String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://localhost:50001/TEST";
String user="user1";
String password="pass";
System.out.println("before try-catch");
Connection connection = null;
try {
System.out.println("try");
//Load class into memory
Class.forName(jdbcClassName);
//Establish connection
System.out.println("before conn");
connection = DriverManager.getConnection(url, user, password);
System.out.println("after conn");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(connection!=null){
System.out.println("Connected successfully.");
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
为了清楚起见,我添加了db2jcc。jar连接到我的项目,并使用有效的url运行代码。程序无法跳过以下行:
connection = DriverManager.getConnection(url, user, password);
我没有收到任何错误或异常,应用程序只是不执行。我不知道怎么处理,有人能帮我吗
# 1 楼答案
删除冒号后,请尝试: