【问题标题】:Groovy SQL and Array parameterGroovy SQL 和数组参数
【发布时间】:2013-10-07 14:57:06
【问题描述】:

我有一个接受Array 类型参数的存储过程,我想通过groovy.sql.Sql.call(...) 调用它,但我不知道如何实例化java.sql.Array 实例以作为参数传递。

在普通 JDBC 中,我可以通过java.sql.Connection.createArrayOf(...) 创建一个java.sql.Array,但我无法通过groovy.sql.Sql 获得对连接的引用。

注意,我通过传递 DataSource 创建了我的 Sql 实例,所以 groovy.sql.Sql.getConnection() 返回 null。

【问题讨论】:

    标签: arrays stored-procedures jdbc groovy connection


    【解决方案1】:

    groovy.sql.Sql 类将根据需要从 DataSource 创建一个连接,并在完成后将其丢弃。使用cacheConnection 保持连接供您使用:

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

    【讨论】:

      【解决方案2】:

      谢谢@ataylor。以您的回答为指导,我发现连接已传递到闭包中,因此您无需引用 sql.getConnection()。我想出了以下我更喜欢的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-10
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        相关资源
        最近更新 更多