2016-01-23 100 views
1

我想用Java代碼創建DB2表格。我測試此代碼,但我得到的錯誤:在DB2中創建表格查詢

com.ibm.db2.jcc.am.SqlException: [jcc][10103][10941][4.19.26] Method executeQuery cannot be used for update. ERRORCODE=-4476, SQLSTATE=null

public void testDB2TableWithRandomData() throws Exception 
    { 
     System.out.println("\nTesting SQL query for DB2 test table and data\n"); 
     try 
     { 
      Class.forName("com.ibm.db2.jcc.DB2Driver"); 
     } 
     catch (ClassNotFoundException e) 
     { 
      System.out.println("Please include Classpath Where your DB2 Driver is located"); 
      e.printStackTrace(); 
      return; 
     } 
     Connection conn = null; 
     PreparedStatement pstmt = null; 
     ResultSet rset = null; 
     try 
     { 
      conn = DriverManager.getConnection("jdbc:db2://34.137.10.143:20002/SAMPLE", "db2inst1", "pass"); 
      if (conn != null) 
      { 
       System.out.println("DB2 Database Connected"); 
      } 
      else 
      { 
       System.out.println("DB2 connection Failed "); 
      } 
      pstmt = conn.prepareStatement("CREATE TABLE EMPL (ENO INTEGER, LASTNAME VARCHAR(30), HIREDATE DATE, SALARY INTEGER)"); 
      rset = pstmt.executeQuery(); 
      if (rset != null) 
      { 
       while (rset.next()) 
       { 
        System.out.println("Status: " + rset.getString(1)); 
       } 
      } 
     } 
     catch (SQLException e) 
     { 
      System.out.println("DB2 Database connection Failed"); 
      e.printStackTrace(); 
      return; 
     } 
    } 

什麼是從Java代碼中創建表的正確方法?

回答

1

對於像CREATE TABLE這樣的INSERT,UPDATE,DELETE和DDL語句,您需要使用pstmt.executeUpdate()