DB2连接 - 从IBM DataStudio工作但从java程序失败

时间:2017-04-07 08:00:59

标签: java database jdbc db2

我正在尝试使用JDBC连接从我的Windows机器连接到DB2数据库。

我能够使用来自IBM Data Studio客户端的相同凭据成功连接,但是当我从Java程序尝试相同时,我得到以下异常。

com.ibm.db2.jcc.am.io: [jcc][t4][2043][11550][4.7.89] Exception java.net.ConnectException: Error opening socket to server lnx1iitmon05s/XXXXXX on port YYYY with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
    at com.ibm.db2.jcc.am.dd.a(dd.java:319)
    at com.ibm.db2.jcc.am.dd.a(dd.java:337)
    at com.ibm.db2.jcc.t4.xb.a(xb.java:378)
    at com.ibm.db2.jcc.t4.xb.<init>(xb.java:76)
    at com.ibm.db2.jcc.t4.a.y(a.java:263)
    at com.ibm.db2.jcc.t4.b.a(b.java:1786)
    at com.ibm.db2.jcc.am.lb.a(lb.java:538)
    at com.ibm.db2.jcc.am.lb.<init>(lb.java:494)
    at com.ibm.db2.jcc.t4.b.<init>(b.java:309)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:214)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:224)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.genericframework.db.RRDBConnection.getConnection(RRDBConnection.java:63)
    at com.genericframework.db.RRDBConnection.main(RRDBConnection.java:77)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at com.ibm.db2.jcc.t4.y.run(y.java:34)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.ibm.db2.jcc.t4.xb.a(xb.java:372)
    ... 12 more

这是我的代码..

 try {
            String filename = "com/genericframework/db/RRDBConnection.properties";
            //input = SPARKApp.class.getClassLoader().getResourceAsStream(filename);
            input = getClass().getClassLoader().getResourceAsStream(filename);

            // load a properties file
            prop.load(input);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        dbHostName = prop.getProperty("DBHost");
        dbPort = prop.getProperty("DBPort");
        dbDatabasename = prop.getProperty("DBname");
        dbUsername = prop.getProperty("DBUserName");
        dbPassword = prop.getProperty("DBPassword");



        String connectionString = "jdbc:db2://"+dbHostName+":"+dbPort+"/"+dbDatabasename;

        System.out.println(connectionString);
        Connection  connection=null;
        try {
            //Class.forName( "COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver");
            Class.forName( "com.ibm.db2.jcc.DB2Driver");
            connection = DriverManager.getConnection(connectionString,dbUsername,dbPassword);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

        try{  
            Class.forName(className);  
            //System.out.println("class not found");  
            con=DriverManager.getConnection(  
                    dbURL,dbName,dbPassword);  
            Statement stmt=con.createStatement(); 

            //your operations like select,insert or update

        catch(Exception e)  { 
            System.out.println(e.getMessage());
        } 
        finally {
            con.close(); 
        }

并添加maven依赖项:

<dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>licences</artifactId>
            <version>1.0</version>
            <!-- Adjust this properly -->
            <scope>system</scope>
            <systemPath>${basedir}/lib/db2jcc_license_cu.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>1.0</version>
            <!-- Adjust this properly -->
            <scope>system</scope>
            <systemPath>${basedir}/lib/db2jcc4.jar</systemPath>
        </dependency>

或使用以下罐子:

1. db2jcc.jar 
2. db2jcc_license_cu.jar

答案 1 :(得分:0)

使用DB2命令窗口中的db2set DB2COMM命令启动TCP / IP连接:

db2set DB2COMM = protocol_names

例如,要设置数据库管理器以启动TCP / IP通信协议的连接管理器,请输入以下命令:

db2set DB2COMM = tcpip DB2STOP DB2START