首先,我觉得这很不对劲,但是有可能减去JasperReports来执行查询。
JasperReport report = (JasperReport) JRLoader.loadObject(inStream);//this is the actual query in the reportJRQuery query = report.getMainDataSet().getQuery;//once here you get the entire sql string, this will have any parameters replaced with //the '?' characterString queryString = query.getText();//now start building your prepared statement, I am assuming you already have your//connection in the conn variablePrepararedStatment 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 honestint index = 0; //this is the index to set the parameter at in the statementfor (JRQueryChunk chunk : query.getChunks()){ if (chunk.getType() == JRQueryChunk .TYPE_PARAMETER){ statement.setObject(index, params.get(chunk.getText())); index = index + 1; }}//then execute the queryResultSet results = statement.executeQuery();注意:
这里没有错误检查,您应该添加它。也不确定这样做是否是个好主意。最好将查询从报告中移出,然后完全移到Java代码中。然后只需将ResultSet作为数据源传递,就可以了。



