New to JR, trying to figure it out. There is an application that displays data from the database depending on the selected conditions (selected flags), and there are a lot of combinations of these conditions. A report template has been created in JR Studio, and a query from an application is registered as a request, respectively, regardless of which conditions are selected in the application (and displayed on the screen), the conditions specified during its creation always get into the report. As I understand it, you need to somehow transfer to the report an existing query (in principle, it suffices to transfer only the "WHERE" block, since the conditions change only there) before its creation. Is this correct, and if correct, then how to do it?

    1 answer 1

    Maybe someone will help. I did this: 1. I made a jrxml parser of the file in which I substitute the necessary request

    String sourceFileName = getProperty("user.dir")+"/src/main/resources/jasper/Users.jrxml"; try { fXmlFile = new File(sourceFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("queryString"); // Get the queryString element by tag name directly Node qNode = doc.getElementsByTagName("queryString").item(0); NodeList list = qNode.getChildNodes(); for(int i = 0; i < list.getLength(); i++) { Node node = list.item(i); //System.out.println("node = " + node + " node.getTextContent() = " + node.getTextContent()); if(node.getNodeName().equals("#cdata-section")) { node.setTextContent("select * from s_users WHERE s_users.DOLZHNOST LIKE 'Консультант'"); } } //Запишем содержимое в xml файл TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(doc); StreamResult streamResult = new StreamResult(new File(sourceFileName)); transformer.transform(domSource, streamResult); } catch (Exception e) { e.printStackTrace(); } } 
    1. I compile a jasper file and form a report

      String sourceFileName = getProperty ("user.dir") + "/ src / main / resources / jasper / Users.jrxml"; JasperReport jReport = JasperCompileManager.compileReport (sourceFileName);
      JasperPrint jPrint = JasperFillManager.fillReport (jReport, new HashMap (), new DAOimpl (). GetCon ());
      JasperExportManager.exportReportToHtmlFile (jPrint, PATH + "users.html");

    where new DAOimpl (). getCon () is a connection to the database

    • Maybe I didn’t quite understand the question, but why was it impossible to simply pass the parameter? - iksuy
    • Probably it was possible, but I did not really figure it out with the parameter. - Tariel