in my Java project I have a lot of JasperReports reports with complex SQL queries, containing a lot of parameters. The reports are used to produce pdf documents containing t
I would like to start with that this feels wrong and hacky, but it is possible, minus actually having JasperReports executing the query.
JasperReport report = (JasperReport) JRLoader.loadObject(inStream);
//this is the actual query in the report
JRQuery query = report.getMainDataSet().getQuery;
//once here you get the entire sql string, this will have any parameters replaced with
//the '?' character
String queryString = query.getText();
//now start building your prepared statement, I am assuming you already have your
//connection in the conn variable
PrepararedStatment statement = con.prepareStatement(queryString);
//almost there, need to set the parameters
//the sql query is broke up into chunks inside the JRQuery. The chunks have types
//that are either text, parameter, or parameter clause. We care about parameter,
//not sure what parameter clause would be to be honest
int index = 0; //this is the index to set the parameter at in the statement
for (JRQueryChunk chunk : query.getChunks()){
if (chunk.getType() == JRQueryChunk .TYPE_PARAMETER){
statement.setObject(index, params.get(chunk.getText()));
index = index + 1;
}
}
//then execute the query
ResultSet results = statement.executeQuery();
Note: There is no error checking here, and you should add that. Also not sure if doing this is a great idea. It could be better to move the queries out of the reports and into your java code altogether. Then just pass in the ResultSet as a datasource and you are good to go.