Search code examples
kotlinjdbcdb2

Db2 error when inserting with PreparedStatement.executeUpdate()


I'm creating support for db2 database in my library and I have code that works on multiple SQL database that supports jdbc

But db2 is giving me an issue when trying to use existing codebase when trying to insert to database table with prepared statement like this...

INSERT INTO main.Parent (col) VALUES (?);

I got error like this...


Caused by: com.urosjarc.dbmessiah.exceptions.EngineException: Failed to process insert results from: 

INSERT INTO main.Parent (col) VALUES (?);

    1) col: VARCHAR = '-1934310868'

Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;ent (col) VALUES (?);<space>, DRIVER=4.33.31

The jdbc side is standard and I know it should be working...


        var ps: PreparedStatement? = null
        var rs: ResultSet? = null
        var rs2: ResultSet? = null

        //Execute query
        try {
            //Prepare statement
            ps = conn.prepareStatement(query.sql, Statement.RETURN_GENERATED_KEYS)
            
            //Custom logic for inserting values to result set...
            ps.setString(1, "asdf")

            //Get info
            val numUpdates = ps.executeUpdate()

            //If no updates happend close all
            if (numUpdates == 0) {
                this.closeAll(ps = ps)
                return null
            }
            //Continue with getting ids for inserts
        } catch (e: Throwable) {
            this.closeAll(ps = ps)
            throw EngineException(msg = "Failed to process insert results from: $query", cause = e)
        }

If I run the same query over jetbrains idea db console

INSERT INTO main.Parent (col) VALUES (?);

The insertion is working...

I'm using jdbc driver...

                this.runtimeOnly("com.ibm.db2:jcc:11.5.9.0")

What is going on here?


Solution

  • Remove the ; at the end of the statement.

    JDBC doesn't support multiple statements and ; is the statement delimiter. So it will lead in an error.