Connection with DB2 and jdbc?

I am trying to connect with DB2 using below code but i am getting following exception

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ROOT.EMPLOYEE, DRIVER=3.63.123
    at com.ibm.db2.jcc.am.fd.a(fd.java:679)
    at com.ibm.db2.jcc.am.fd.a(fd.java:60)
    at com.ibm.db2.jcc.am.fd.a(fd.java:127)
    at com.ibm.db2.jcc.am.ho.c(ho.java:2644)
    at com.ibm.db2.jcc.am.ho.d(ho.java:2632)
    at com.ibm.db2.jcc.am.ho.a(ho.java:2097)
    at com.ibm.db2.jcc.t4.cb.h(cb.java:141)
    at com.ibm.db2.jcc.t4.cb.b(cb.java:41)
    at com.ibm.db2.jcc.t4.q.a(q.java:32)
    at com.ibm.db2.jcc.t4.sb.i(sb.java:135)
    at com.ibm.db2.jcc.am.ho.gb(ho.java:2066)
    at com.ibm.db2.jcc.am.ho.a(ho.java:3120)
    at com.ibm.db2.jcc.am.ho.a(ho.java:681)
    at com.ibm.db2.jcc.am.ho.executeQuery(ho.java:665)

Code:

      Class.forName("com.ibm.db2.jcc.DB2Driver");                             
      System.out.println("**** Loaded the JDBC driver");

        String url = bundle.getString("db2.url");  
        String user = bundle.getString("db2.username");  
        String password = bundle.getString("db2.password");

      // Create the connection using the IBM Data Server Driver for JDBC and SQLJ
      Connection con = DriverManager.getConnection (url, user, password);                
      // Commit changes manually
      con.setAutoCommit(false);
      System.out.println("**** Created a JDBC connection to the data source");

      // Create the Statement
      Statement stmt = con.createStatement();                                           
      System.out.println("**** Created JDBC Statement object");

      // Execute a query and generate a ResultSet instance
      ResultSet rs = stmt.executeQuery("SELECT EMPNO FROM  EMPLOYEE");                   
      System.out.println("**** Created JDBC ResultSet object");

      String empNo;   

      // Print all of the employee numbers to standard output device
      while (rs.next()) {
        empNo = rs.getString(1);
        System.out.println("Employee number = " + empNo);
      }
      System.out.println("**** Fetched all rows from JDBC ResultSet");
      // Close the ResultSet
      rs.close();
      System.out.println("**** Closed JDBC ResultSet");

      // Close the Statement
      stmt.close();
      System.out.println("**** Closed JDBC Statement");
              if(con!=null){System.out.println("Connection successfull with DB2");}

Upvotes: 0

Views: 1645

Answers (1)

AngocA
AngocA

Reputation: 7693

The problem is not the connection. You need to understand how to read the DB2 error messages.

In this part the problem is explained:

SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ROOT.EMPLOYEE

In means, that you already established a connection, and when looking for the object ROOT.EMPLOYEE, db2 did not find anything. The error is 204, than can be interpreted as SQL0204. You look for that in the db2clp or the internet and you got the answer.

It seems that your problem is that you are connecting as root to the database, and the implicit schema 'ROOT' does not contain the EMPLOYEE table. Make sure you are using the right schema.

Upvotes: 2

Related Questions