与DB2和jdbc连接?

时间:2014-08-18 09:16:59

标签: java jdbc db2

我正在尝试使用以下代码与DB2连接,但我得到了以下异常

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)

代码:

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

1 个答案:

答案 0 :(得分:2)

问题不在于连接。您需要了解如何阅读DB2错误消息。

在这部分中解释了问题:

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

意思是,您已经建立了连接,并且在查找对象ROOT.EMPLOYEE时,db2没有找到任何内容。错误是204,可以解释为SQL0204。你在db2clp或互联网上寻找它,你就得到了答案。

您的问题似乎是您以root身份连接到数据库,以及隐式架构' ROOT'不包含EMPLOYEE表。确保使用正确的架构。