我试图用我的数据库中的信息填充我的HashMap参数。我创建了一个检索信息的SQL查询,以及一个显示信息的ResultSet。然后,我使用ResultSet以一种允许我将每个值分配给字符串的方式打破信息。这是我目前的代码:
Connection connection;
Statement stmt;
ResultSet rs;
try {
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection("jdbc:h2:./GiftCertificateManagerDatabase;AUTO_SERVER=TRUE");
String query = "SELECT CertificateCode, FirstName, LastName, IssueDate, ExpirationDate, Used FROM giftcertificates";
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
JRResultSetDataSource rsdt = new JRResultSetDataSource(rs);
JasperReport jasperReport;
JasperPrint jasperPrint;
jasperReport = JasperCompileManager.compileReport("Reports/GiftCertificateReport.jrxml");
HashMap parameters = new HashMap();
rs.next();
String CertificateCode = rs.getString("CertificateCode");
String FirstName = rs.getString("FirstName");
String LastName = rs.getString("LastName");
String IssueDate = rs.getString("IssueDate");
String ExpirationDate = rs.getString("ExpirationDate");
String Used = rs.getString("Used");
parameters.put("CertificateCode", CertificateCode);
parameters.put("FirstName", FirstName);
parameters.put("LastName", LastName);
parameters.put("IssueDate", IssueDate);
parameters.put("ExpirationDate", ExpirationDate);
parameters.put("Used", Used);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, rsdt);
JasperViewer view = new JasperViewer(jasperPrint);
view.setVisible(true);
System.out.println(rs);
connection.close();
} catch (ClassNotFoundException | SQLException | JRException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
}
如您所见,我使用rs.next();
转到数据库的第一行。它提取信息,并实际发送到我的报告,这是伟大的。然而,它出现在我的报告上两次。以下是我的报告的外观(点击here查看大图):
这是我的JRXML代码:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="29a25e63-6738-4f28-a9b1-84d9571ae46e">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="CertificateCode" class="java.lang.String"/>
<parameter name="FirstName" class="java.lang.String"/>
<parameter name="LastName" class="java.lang.String"/>
<parameter name="IssueDate" class="java.lang.String"/>
<parameter name="ExpirationDate" class="java.lang.String"/>
<parameter name="Used" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="bb628493-cab6-4d39-acff-79669009ac6b"/>
<text><![CDATA[Certificate Code]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20" uuid="2c18a226-2bdf-45e1-b286-f7795cf1d17f"/>
<text><![CDATA[First Name]]></text>
</staticText>
<staticText>
<reportElement x="200" y="0" width="100" height="20" uuid="e6b35970-0592-4d97-a054-fb0c65244618"/>
<text><![CDATA[Last Name]]></text>
</staticText>
<staticText>
<reportElement x="300" y="0" width="100" height="20" uuid="ee345d19-12f4-4ea7-a9d9-924c50904cb9"/>
<text><![CDATA[Issue Date]]></text>
</staticText>
<staticText>
<reportElement x="400" y="0" width="100" height="20" uuid="c5d69f5e-e716-4393-9815-d9cee8bab88b"/>
<text><![CDATA[Expiration Date]]></text>
</staticText>
<staticText>
<reportElement x="500" y="0" width="55" height="20" uuid="9760f598-6e30-45ad-9d05-2696f6d45c8e"/>
<text><![CDATA[Used?]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="34b20574-2e15-41c6-928d-2dc52b9c4e7e"/>
<textFieldExpression><![CDATA[$P{CertificateCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="1652ab96-50cd-4ce3-84b1-83bf074b480c"/>
<textFieldExpression><![CDATA[$P{FirstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20" uuid="bcfec07b-d07d-474b-a397-781b9ce0254f"/>
<textFieldExpression><![CDATA[$P{LastName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="300" y="0" width="100" height="20" uuid="cdd427e7-b1cf-4ed3-9cfe-8a13e71c0e0d"/>
<textFieldExpression><![CDATA[$P{IssueDate}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="400" y="0" width="100" height="20" uuid="1f64f071-8d53-4de2-be05-23e78753f467"/>
<textFieldExpression><![CDATA[$P{ExpirationDate}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="500" y="0" width="55" height="20" uuid="38402e3c-3279-4a76-85f8-0266d77e34e1"/>
<textFieldExpression><![CDATA[$P{Used}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
以下是我的iReport设计师的图片(点击here查看大图):
我显然需要使用while循环,例如while (rs.next()) { }
,但我在哪里放置它,以及我在循环中放入了什么代码,以便从数据库中获取所有行被插入报告?
与往常一样,提前感谢您提供任何有用的答案。
答案 0 :(得分:0)
List<?> list=new ArrayList<HashMap<String,String>>();
while(rs.hasNext()){
rs.next();
HashMap<String,String> parameters=new HashMap<>();
String CertificateCode = rs.getString("CertificateCode");
String FirstName = rs.getString("FirstName");
String LastName = rs.getString("LastName");
String IssueDate = rs.getString("IssueDate");
String ExpirationDate = rs.getString("ExpirationDate");
String Used = rs.getString("Used");
parameters.put("CertificateCode", CertificateCode);
parameters.put("FirstName", FirstName);
parameters.put("LastName", LastName);
parameters.put("IssueDate", IssueDate);
parameters.put("ExpirationDate", ExpirationDate);
parameters.put("Used", Used);
list.add(parameters);
}
结果;
[
{Certificatecode:lskff,姓:asdkff,姓氏:lsdkfs,issuedate:sdjflksf,EXPIRATIONDATE:lsdfjlsdf,用于:skdfjl},
{Certificatecode:lskff,姓:asdkff,姓氏:lsdkfs,issuedate:sdjflksf,EXPIRATIONDATE:lsdfjlsdf,用于:skdfjl},
{Certificatecode:lskff,姓:asdkff,姓氏:lsdkfs,issuedate:sdjflksf,EXPIRATIONDATE:lsdfjlsdf,用于:skdfjl}
答案 1 :(得分:0)
我认为您将JRDataSource提供的报告详细信息与参数map混淆。
参数图是一组可以在报告上显示的键值。
JRDataSource可用于填充详细数据。
What is the difference between variable, parameter and field in JasperReports?