急:Hibernate+iReport

zyhowe 2004-07-01 01:27:52
JRDataSource怎么设置?及.jsp如何完成.

Thx!
...全文
176 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyhowe 2004-07-01
  • 打赏
  • 举报
回复
chshzh2000(地皮) :
Thx!
还请帮帮忙!

偶现在用的是struts+hibernate
ireport的datasource用JRDataSource

public class HibernateQueryResultDataSource implements dori.jasper.engine.JRDataSource {
private String[] fields;
private Iterator iterator;
private Object currentValue;

public HibernateQueryResultDataSource(List list, String[] fields) {
this.fields = fields;
this.iterator = list.iterator();
}

public Object getFieldValue(JRField field) throws JRException {
Object value = null;
int index = getFieldIndex(field.getName());
if (index > -1) {
Object[] values = (Object[])currentValue;
value = values[index];
}
return value;
}

public boolean next() throws JRException {
currentValue = iterator.hasNext() ? iterator.next() : null;
return (currentValue != null);
}

private int getFieldIndex(String field) {
int index = -1;
for (int i = 0; i < fields.length; i++) {
if (fields[i].equals(field)) {
index = i;
break;
}
}
return index;
}


}

现在的问题就是下面的ds得不到正确的值!
HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(list, fields);
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,ds);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
春秋2000 2004-07-01
  • 打赏
  • 举报
回复
进入iReport中的 Datasource/Connections/Datasources 菜单就可以进行可视化的数据源连接。

以下是JSP调用jasper文件:

JSP:
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="dori.jasper.engine.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
//数据库连接
Connection conn=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.10:1433;DatabaseName=am;user=sa;password=

sa");

//取到编译后的jasper文件
File reportFile = new File(application.getRealPath("test/iteminfo.jasper"));

//向报表中定义的参数赋值
Map parameters = new HashMap();
Integer i=new Integer(8);
parameters.put("pjId", i);


byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
conn
);


response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();

%>

ecaol 2004-07-01
  • 打赏
  • 举报
回复
UP

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧