Jump to content
Changes to the Jaspersoft community edition download ×

problem creat pdf with ResultSet

Recommended Posts

By: Ha Hieu - hieuhn

problem creat pdf with ResultSet

2003-09-30 19:41

i have a problem when i use "bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,new JRResultSetDataSource(rs));" when i try to modify servlet PdfServlet.java example in webapp, when i use rs in WebappDataSource.java like that ;


package jasper_reports;


import dori.jasper.engine.*;

import java.sql.*;


public class WebappDataSource implements JRDataSource{

private String DRIVER = "com.mysql.jdbc.Driver";

private String USERNAME = "root";

private String PASSWORD = "";

private String STRING = "jdbc:mysql://localhost:3306/test_report";

private Object[][] data ;

private int index = -1;

public WebappDataSource(){

data=new Object[40][4];

try {

Driver Driverrs = (Driver)Class.forName(DRIVER).newInstance();

Connection Connrs = DriverManager.getConnection(STRING,USERNAME,PASSWORD);

PreparedStatement Statementrs = Connrs.prepareStatement("SELECT * FROM address ORDER BY CITY ASC");

ResultSet rs = Statementrs.executeQuery();

boolean rs_isEmpty = !rs.next();

boolean rs_hasData = !rs_isEmpty;

int rs_numRows = 0;

int i=0;

while (rs_hasData && i<30) {

data[0]=(((rs.getObject("CITY"))==null || rs.wasNull())?"":rs.getObject("CITY"));

data[1]=new Integer((((rs.getObject("ID"))==null || rs.wasNull())?"0":rs.getObject("ID")).toString());

data[2]=(((rs.getObject("FIRSTNAME"))==null || rs.wasNull())?"":rs.getObject("FIRSTNAME")).toString()

+" "+(((rs.getObject("LASTNAME"))==null || rs.wasNull())?"":rs.getObject("LASTNAME")).toString();

data[3]=(((rs.getObject("STREET"))==null || rs.wasNull())?"":rs.getObject("STREET"));


rs_hasData = rs.next();






catch (Exception ex) {

for (int i = 0; i < data.length; i++) {

data = new Object[] {"San Francisco", new Integer(7), "James Peterson", "231 Upland Pl."};





public boolean next() throws JRException




return (index < data.length);


public Object getFieldValue(JRField field) throws JRException


Object value = null;


String fieldName = field.getName();


if ("City".equals(fieldName))


value = data[index][0];


else if ("Id".equals(fieldName))


value = data[index][1];


else if ("Name".equals(fieldName))


value = data[index][2];


else if ("Street".equals(fieldName))


value = data[index][3];



return value;




it successfull to creat pdf then I repalce :bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,new JRResultSetDataSource(rs)); by bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,new WebappDataSource()); . But it not work, please help me to use bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,new JRResultSetDataSource(rs)); to creat pdf report

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...