从Java连接DB2

时间:2016-09-27 13:20:39

标签: java database db2 database-connection

我尝试从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 个答案:

答案 0 :(得分:-1)

尝试删除冒号之后:

String url="jdbc:db2//localhost:50001/TEST";