开发者

How to mimic PreparedStatement to produce a valid SQL query (Sql#withBatch)

Here is my code:

// my SQL query used with PreparedStatement before
def query = "INSERT INTO TAB_A (x,y) VALUES (?,?)

Sql myDb = Sql.newInstance(...)


myDb.withBatch(10000, {stmt ->
  // I g开发者_如何学JAVAet parameters as an array
  def params = getParams();

  String rowQuery = funcThatMimicsPrepStat(query, params)

  stmt.addBatch(rowQuery);  
}

I currently looking for a way to miminc preparedstatement (funcThatMimicsPrepStat) inside withBatch, which works only with Statement. Any advice more than welcome.


Groovy 1.8.0 doesn't support it yet but it looks like one of the next minor versions will support it. The trunk source code has a couple of examples that are really helpful. Here's one example that uses a PreparedStatement:

def updateCounts = sql.withBatch(20, 'insert into TABLENAME(a, b, c) values (?, ?, ?)') { ps ->
    ps.addBatch(10, 12, 5) // varargs style
    ps.addBatch([7, 3, 98]) // list
    ps.addBatch([22, 67, 11])
    ...
}

I guess you could either snatch that piece of code or wait for a Groovy 1.8.x release.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜