代码之家  ›  专栏  ›  技术社区  ›  UroÅ¡ Jarc

使用PreparedStatement.executeUpdate()插入时出现Db2错误

  •  -1
  • UroÅ¡ Jarc  · 技术社区  · 2 年前

    我正在我的库中创建对db2数据库的支持 我有一些代码可以在支持jdbc的多个SQL数据库上工作

    但是db2在尝试使用现有的代码库时给我带来了一个问题 尝试使用prepared语句插入数据库表时 这样地。。。

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

    我犯了这样的错误。。。

    
    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
    

    jdbc端是标准的,我知道它应该可以工作。。。

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

    如果我在jetbrains idea数据库控制台上运行相同的查询

    插入主体。父(列)值(?);
    

    插入正在工作。。。

    我正在使用jdbc驱动程序。。。

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

    这是怎么回事?

    1 回复  |  直到 2 年前
        1
  •  0
  •   Jens    2 年前

    删除 ; 在陈述的最后。

    JDBC不支持多个语句,并且 ; 是语句分隔符。所以这会导致一个错误。