简体   繁体   中英

Connect with DB2 from Java

I try to connect to db2 from java, here is my code:

    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();
                }
            } 
        }
    }
}

To be clear, I add db2jcc.jar to my project and run code with valid url. Program cannot jump over line:

connection = DriverManager.getConnection(url, user, password);

I receive no errors or exception, application just not execute. I have no idea how deal with it, can anyone help me?

在删除冒号后尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM