I have a stored procedure which accepts a parameter of type Array
and I'd like to invoke it via groovy.sql.Sql.call(...)
but I can't figure out how to instantiate a java.sql.Array
instance to pass as a parameter.
In normal JDBC, I can create a java.sql.Array
via java.sql.Connection.createArrayOf(...)
but I can't get a reference to the connection through groovy.sql.Sql
.
Note, I have created my Sql instance by passing a DataSource so groovy.sql.Sql.getConnection()
returns null.
The groovy.sql.Sql
class will create a connection on demand from the DataSource and throw it away when it's done. Use cacheConnection
to keep the connection around for you to use:
def sql = new Sql(datasource)
sql.cacheConnection {
assert sql.connection != null
println sql.rows('select * from mytable where arraycol = ?',
sql.connection.createArrayOf('integer', [1, 2, 3] as Object[]))
}
Thanks @ataylor. Using your answer as a guide, I found that the connection is passed into the closure so you don't need to reference sql.getConnection(). I came up with the following which I prefer:
def sql = new Sql(datasource)
sql.cacheConnection { Connection con ->
assert con != null
def array = con.createArrayOf('integer', [1, 2, 3] as Object[]))
println sql.rows('select * from mytable where arraycol = ?', array)
}
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.