简体   繁体   中英

Create table query in DB2

I want to create DB2 table with from Java code. I tested this code but I get error:

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

What is the proper way to create table from Java code?

您需要对诸如CREATE TABLE之类的INSERT,UPDATE,DELETE和DDL语句使用pstmt.executeUpdate()

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