Jump to content
Changes to the Jaspersoft community edition download ×

querydataset in jbuilder x


2005 IR Help

Recommended Posts

By: Dimitri - koudim

querydataset in jbuilder x

2004-05-02 05:38

Hi,

 

it is working finally and it was so freaking simple. I'm using the following code to implement jasperreports in my swing application getting data from one existing MS ACCESS data through one ODBC connection. It is one public void which I'm calling from within one swing button and it is working.

 

My question is : is there a way to use the querydataset (implemented in my swing app) as the connection for jasperreports insted of the actual database ? The purpose of this is to get a report that will depict the actual data displayed on my screen (from the querydataset) insted of getting the data from the actual database by running the query in the .xml .

 

public void report() {

try {

System.setProperty("org.xml.sax.driver",

"org.apache.xerces.parsers.SAXParser");

 

// Load JasperDesign from XML and compile it into JasperReport

JasperDesign jasperDesign = JasperManager.loadXmlDesign(

"reports\Pelates_Report.xml");

JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

 

// Get a database connection

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

java.sql.Connection conn = java.sql.DriverManager.getConnection(

"jdbc:odbc:Resource Management", "", "");

 

// Create JasperPrint using fillReport() method

JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, null,

conn);

 

// View report in the JasperViewer

JasperViewer.viewReport(jasperPrint, false);

 

}

catch (Exception e) {

e.printStackTrace();

}

 

}

 

void jButton1_actionPerformed(ActionEvent e) {

report();

}

 

 

Thanks in advance,

 

Dimitri

 

 

 

 

By: C-Box - c-box

RE: querydataset in jbuilder x

2004-05-02 11:52

Hi Dimitri,

 

I don't use directConnection also. Try to use CustomDataSource as base for your report...

See the included DataSource Sample for usage... I prefered using JRTableModelDataSource and it works quite well...

Just build the recordSet with your SQL-StateMent and then fill a CustomDatasource with the ResultSetData

 

You could experience some trouble if you want to use SubReports with MASTER-DETAIL functionality where RelationShip is required.. therefore I didn't found a given solution in jasper 0.5.2 yet. (Just a SourceCodeChange of the TableDataSource class and the basefiller brought light to the end of the tunnel...) but I hope Theodord will reply one of my questions yet.

 

hth

C-Box

 

 

 

 

By: Dimitri - koudim

RE: querydataset in jbuilder x

2004-05-02 21:22

yes but....

 

I'm usin an existing querydataset, implemented in jbuilder which is working by using a database connection and feeding data to a tabledataset.

 

Can I use the same querydataset to feed info to the report too ?

 

How ?

 

the code for filling the report is....

 

// Create JasperPrint using fillReport() method

JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,

parameters,

conn);

 

-----

jasperReport is the compiled .xml jasperreport object, parameters do not exist (null) and conn is the ODBC database connection.

 

Is there a way to replace the "conn" part with the forementioned querydataset?

 

all I could find was....

 

// Custom Dataset

ResultSet rs = getResultSet();

JRDataSource dataSource = new JRResultSetDataSource(rs);

 

---

 

that doesn't work though if I put the querydataset to the resultset line. What am I doing wrong ? What should I do to get data for the report from my querydataset ?

 

 

This is urgent guys, help please,

 

 

Dimitri

 

 

 

 

By: Dimitri - koudim

RE: querydataset in jbuilder x

2004-05-02 21:25

The query data set implementation is :

 

// object creation

QueryDataSet queryDataSet1 = new QueryDataSet();

 

// db connection

database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor(

"jdbc:odbc:Resource Management", "Administrator", "", false,

"sun.jdbc.odbc.JdbcOdbcDriver"));

 

// querydataset

queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(

database1, "select Pel_Eponimo, Pel_Onoma from Pelates", null, true,

Load.ALL));

 

// feed to tabledataset

jdbTable1.setDataSet(queryDataSet1);

 

 

 

 

By: C-Box - c-box

RE: querydataset in jbuilder x

2004-05-02 22:39

Did you already tried to use the FillMethod without Connection but therefore with your CustomDataSource? (like following lines):

 

JasperReport myJR = null;

JasperCompileManager myJC = new JasperCompileManager();

JasperPrint myJP = new JasperPrint();

myJR = myJC.compileReport(getMyInputStreamFromMyFile(myReportXMLFileObject))

// Custom Dataset

ResultSet rs = getResultSet(); //your Call to retrieve rs

JRDataSource myDS = new JRResultSetDataSource(rs);

JasperFillManager myJFM = new JasperFillManager();

myJP = myJFM.fillReport(myJR, parameters,myDS);

 

hth

C-Box

 

 

 

 

By: Dimitri - koudim

RE: querydataset in jbuilder x

2004-05-02 23:56

dear c-box,

 

do you mean to alter my existing code to ...

 

JasperReport myJR = null;

JasperCompileManager myJC = new JasperCompileManager();

JasperPrint myJP = new JasperPrint();

myJR = myJC.compileReport(jasperDesign);

// Custom Dataset

ResultSet rs = getResultSet(); //your Call to retrieve rs

JRDataSource myDS = new JRResultSetDataSource(rs);

JasperFillManager myJFM = new JasperFillManager();

myJP = myJFM.fillReport(myJR, parameters,myDS);

 

ok..... it kicks on the resultset rs - getresultset(); line as it cannot find method getresultset();

 

a. Am I not importing needed libraries ?????

Up to know I work with.....

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

import com.borland.dx.sql.dataset.*;

import com.borland.dbswing.*;

import java.util.*;

import java.sql.*;

 

// JasperReports libraries

import dori.jasper.engine.*;

import dori.jasper.engine.design.*;

import dori.jasper.view.*;

 

b. I line : resultset rs - getresultset(); should I use the existing querydataset object ? Where should I acknowledge the existance of the querydataset within the myds implementation ?

 

damn... there must be a way to user querydataset as the source..... man this is complicated.

 

 

 

 

By: C-Box - c-box

RE: querydataset in jbuilder x

2004-05-03 01:19

I thought you already have a method "getResultSet()" ?????

 

If not, then write one for example:

 

private ResultSet getResultSet(){

String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

String connectString =

"jdbc:microsoft:sqlserver://192.168.1.21:1433;DatabaseName=ReportDB"; // change that to your connection String

String user = "sa";

String password = "";

Class.forName(driver);

Connection conn = DriverManager.getConnection(connectString, user, password);

java.sql.Statement stmt = conn.createStatement();

ResultSet rs = null;

rs = stmt.executeQuery("Select * from MyTableThatContainsData"); // Change that to your Statement

return rs;

}

 

the rest should work if you have a jasperDesign object that contains the loaded report from your XML-File

 

hth

C-Box

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...