简体   繁体   中英

How to return a JDBC ResultSet using Groovy

I am doing a report using JasperReport from Groovy+Grails and want to send the ResultSet to my report. The problem is the Jasper doesn't know the dataSource from Groovy+Grails. I am using this

JasperFillManager.fillReport(in, parameterMap, new JRResultSetDataSource(resultSet));

This class just receives a ResultSet object but I can't find the way to return a JDBC ResultSet from Groovy+Grails to pass to JRResultSetDataSource() .

You can use groovy.sql.Sql to create and execute sql queries and it also return a ResultSet object.

You can create a new groovy.sql.Sql instance using different strategies like using a existing sql connection, dataSource bean or by supplying the normal jdbc connection details. Look for more details here .

executeQuery and executePreparedQuery are two method that can be used to execute sql queries and will return a Resultset .

If you're working inside a service or a controller you can do this:

first you need to inject your datasource inside the service or controller:

def dataSource

And then inside the function that creates the jasper you can use something like:

 Sql sqlConn = new Sql(dataSource)
 String query = "SELECT * FROM my_table"
 sqlConn.executeQuery(query)

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.

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