简体   繁体   English

H2通过存储过程插入

[英]H2 Insert via stored procedure

I'm trying to write an H2 stored procedure to insert some values into a table. 我正在尝试编写H2存储过程以将一些值插入表中。 I'm doing this because in my unit testing environment, I'd rather connect to an in-memory than a real database. 之所以这样做,是因为在我的单元测试环境中,我宁愿连接到内存中而不是真实的数据库。 Here's what I have (note that I'm doing this in groovy, not java, but I don't think that matters). 这就是我所拥有的(请注意,我是用Groovy而不是Java来做的,但我认为这并不重要)。

Sql sql = new Sql(JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "user", "password"))
sql.executeUpdate("CREATE TABLE data (key VARCHAR(255) PRIMARY KEY,value VARCHAR(1023) )")
sql.execute("CREATE ALIAS insertInto FOR \"com.vanguard.fig.batch.core.StoredProcs.insertInto\"")
sql.execute("CALL insertInto(?,?)", val1, val2)
sql.eachRow("Select * From data") { row ->
                println row             
            }

And my procedure is as follows 我的程序如下

public class StoredProcs {
    void insertInto(java.sql.Connection con, String val1, String val2) throws Exception {
        String resultValue=null;
        Sql sql = new Sql(con)
        sql.executeInsert("Insert Into data VALUES(?, ?)", val1, val2)
    }
}

The examples were all fairly confusing and I'm not sure I'm doing this right. 这些示例都相当混乱,我不确定自己是否正确。 Here's the error 这是错误

WARNING: Failed to execute: CALL insertInto(?,?) because: Parameter "#1" is not set; SQL statement:

Insert Into data VALUES(?, ?) [90012-193] 插入数据VALUES(?,?)[90012-193]

Which version of h2 are you using.? 您正在使用哪个版本的h2。 Looks like h2 has a bug filed for this issue. 似乎h2已针对此问题提交了一个错误。 GitHub Bug GitHub错误

Please upgrade it to the latest version to fix this issue. 请升级到最新版本以解决此问题。

PS : I was not able to add this as comment since i need to have 50 reputation to add a comment. PS:我无法添加此评论,因为我需要50声誉才能添加评论。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM