要求是从数据库中获取数据并将其显示在动态报告中,但是当我尝试通过createDataSource()传递列表并调用report.setDataSource(createDataSource())时,它仍会显示&#39 ; s在报告中为空值,能够显示像jobid,status_value和mobile_no这样的列标题,但是即使数据库中存在该值,它们的对应值也为空,请帮助!我是在正确的路径中
java代码
package com.unify.avcv.utils.threads;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import com.google.gson.Gson;
import com.itextpdf.text.pdf.hyphenation.TernaryTree.Iterator;
import com.unify.avcv.hibernate.util.HibernateDBUtil;
import com.unify.avcv.hibernate.model.AvcvJobData;
import net.sf.dynamicreports.jasper.builder.*;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.TextFieldBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import java.io.File;
import java.math.BigInteger;
import org.w3c.dom.*;
import javax.activation.DataSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class DynamicReportsGeneration {
public static void main(String[] args) throws FileNotFoundException, DRException {
try
{
JasperReportBuilder report=DynamicReports.report();
StyleBuilder boldstyle=DynamicReports.stl.style().bold();
StyleBuilder boldstyle_center=DynamicReports.stl.style(boldstyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
TextFieldBuilder<String> Title=DynamicReports.cmp.text("My First Dynamic Report");
Title.setStyle(boldstyle_center);
report.title(Title);
TextColumnBuilder<String> job_id=Columns.column("jobid", "jobId", DynamicReports.type.stringType());
TextColumnBuilder<Integer> status_value=Columns.column("status_value", "status", DynamicReports.type.integerType());
TextColumnBuilder<BigInteger> mobile_value=Columns.column("mobile_no", "mobileNo", DynamicReports.type.bigIntegerType());
report.columns(job_id,status_value,mobile_value);
report.setDataSource(createDataSource());
report.show();
System.out.println("done!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static JRDataSource createDataSource() {
DRDataSource dataSource = new DRDataSource("jobid", "status_value", "mobile_no");
Session session = (Session) HibernateDBUtil.getSessionFactory().openSession();
String avcvAuditLogQuery = "select jobId,status,mobileNo from AvcvJobData where mobileNo="+8888888811L;
List<AvcvJobData> avcvAuditLogResult = session.createQuery(avcvAuditLogQuery).list();
dataSource.add(avcvAuditLogQuery);
return dataSource;
}
}
AvcvJobData.java(POJO文件)
package com.unify.avcv.hibernate.model;
import java.util.Date;
public class AvcvJobData implements java.io.Serializable, Cloneable {
private String jobId;
private Integer status;
private long mobileNo;
public AvcvJobData() {
}
public AvcvJobData(String jobId, long mobileNo) {
this.jobId = jobId;
this.mobileNo = mobileNo;
}
public AvcvJobData(String jobId, Integer status, long mobileNo
) {
this.jobId = jobId;
this.status = status;
this.mobileNo = mobileNo;
}
public String getJobId() {
return this.jobId;
}
public void setJobId(String jobId) {
this.jobId = jobId;
}
public Integer getStatus() {
return this.status;
}
public void setStatus(Integer status) {
this.status = status;
}
public long getMobileNo() {
return this.mobileNo;
}
public void setMobileNo(long mobileNo) {
this.mobileNo = mobileNo;
}
}
AvcvJobData.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.unify.avcv.hibernate.model.AvcvJobData" table="avcv_job_data" catalog="avcvmts">
<id name="jobId" type="string">
<column name="job_id" length="15" />
<generator class="assigned" />
</id>
<property name="status" type="java.lang.Integer">
<column name="status" />
</property>
<property name="mobileNo" type="long">
<column name="mobile_no" not-null="true" />
</property>
<property name="cafNo" type="string">
<column name="caf_no" length="20" />
</property>
</class>
</hibernate-mapping>
答案 0 :(得分:0)
首先你应该看看:
TextColumnBuilder<BigInteger> mobile_value=Columns.column("mobile_no", "mobileNo", DynamicReports.type.bigIntegerType());
您声明了不存在的mobile_value
列以及不是报告列名称的job_id
。
report.columns(job_id,status_value,mobile_value);
试试这个:
report.columns(jobId,status_value,mobile_no);