0

How can i use Jasperreport component with Connection instead of JRDatasource ?

asked 2009-01-18 03:29:37 +0800

ks9289 gravatar image ks9289
39 1

Hi,

Is there any method to directly use JDBC Connection in jasperreport component because
query already defined within japer template , so that i can fill report ??

thanks in advance
Kamen Lai

delete flag offensive retag edit

8 Replies

Sort by » oldest newest

answered 2009-01-19 13:50:54 +0800

YamilBracho gravatar image YamilBracho
1722 2

You can pass a Connection objet to JasperReports JasperFillManager.
Check JasperReports API Docs http://jasperreports.sourceforge.net/api/index.html

link publish delete flag offensive edit

answered 2009-01-19 23:24:20 +0800

ks9289 gravatar image ks9289
39 1

Yes I know if I directly use JasperFillManager to genernate report and like in smalltalk document.

What I mean is use ZK Jasperreport component , there is oly JRDataSource property and no Connection object?

Thanks

link publish delete flag offensive edit

answered 2009-01-20 13:34:58 +0800

YamilBracho gravatar image YamilBracho
1722 2

I guess there is no copnnection usage inside Jasperreport component because it is already receving the data source. I think this is made to separate concers between JasperReports and ZK.
You could make write your own component that derivates from Jasperreport component and add connection objeto support.

link publish delete flag offensive edit

answered 2012-07-12 13:11:34 +0800

javaenthu gravatar image javaenthu
141 2

I have set the connection to the template from composer. Report is generated correctly but I am seeing lot of db connections happening.
Doesn't zk jasper close the connection?
How can i close the connection explicitly if required? Because i see call to generate report is a non blocking call.
Thanks
-JE

link publish delete flag offensive edit

answered 2012-07-13 21:41:33 +0800

marcelodecampos gravatar image marcelodecampos
183

updated 2012-07-13 21:41:58 +0800

if I understood your question, is this what you´re looking for?

	protected JasperPrint compileReport( String report, Map<String, Object> params )
	{
		if ( report == null ) {
			return null;
		}
		if ( params == null ) {
			params = new HashMap<String, Object>( );
		}
		String realPath;

		realPath = getRealPath( "/img" );
		params.put( "IMAGE_PATH", realPath );

		try {
			realPath = getRealPath( report );
			File file = new File( realPath );
			String name = SysUtils.getExtension( file );
			if ( SysUtils.isEmpty( name ) ) {
				name = realPath + ".jrxml";
			}
			JasperReport jasperReport = JasperCompileManager.compileReport( name );
			return JasperFillManager.fillReport( jasperReport, params, getConnection( ) );
		}
		catch ( JRException e ) {
			logger.error( "JRException", e );
		}
		catch ( SQLException e ) {
			logger.error( "JRException", e );
		}
		catch ( ClassNotFoundException e ) {
			logger.error( "JRException", e );
		}
		return null;
	}

	protected Connection getConnection( ) throws SQLException, ClassNotFoundException
	{
		Class.forName( "org.postgresql.Driver" );
		String url = "jdbc:postgresql:database";
		Properties props = new Properties( );
		props.setProperty( "user", "user" );
		props.setProperty( "password", "password" );
		return DriverManager.getConnection( url, props );
	}


link publish delete flag offensive edit

answered 2012-07-17 08:17:42 +0800

javaenthu gravatar image javaenthu
141 2

Thanks but can you tell where are we closing the dbconnection?

link publish delete flag offensive edit

answered 2012-07-17 16:48:59 +0800

marcelodecampos gravatar image marcelodecampos
183

After fulfilling your report, you don't need the connection anymore!

This is the corrected code.

	protected JasperPrint compileReport( String report, Map<String, Object> params )
	{
		if ( report == null ) {
			return null;
		}
		if ( params == null ) {
			params = new HashMap<String, Object>( );
		}
		String realPath;

		realPath = getRealPath( "/img" );
		params.put( "IMAGE_PATH", realPath );

		Connection conn = null;
		try {
			realPath = getRealPath( report );
			File file = new File( realPath );
			String name = SysUtils.getExtension( file );
			if ( SysUtils.isEmpty( name ) ) {
				name = realPath + ".jrxml";
			}
			conn = getConnection( );
			JasperReport jasperReport = JasperCompileManager.compileReport( name );
			JasperPrint nRet = JasperFillManager.fillReport( jasperReport, params, conn );
			return nRet;
		} catch( JRException e ) {
			logger.error( "JRException", e );
		} catch( SQLException e ) {
			logger.error( "JRException", e );
		} catch( ClassNotFoundException e ) {
			logger.error( "JRException", e );
		} finally {
			try {
				if ( conn != null && conn.isClosed( ) == false ) {
					conn.close( );
					conn = null;
				}
			} catch( SQLException e ) {
				e.printStackTrace( );
			}
		}
		return null;
	}

link publish delete flag offensive edit

answered 2012-07-23 09:59:50 +0800

javaenthu gravatar image javaenthu
141 2

updated 2012-07-23 10:00:25 +0800

Thank you marcelodecampos.

But I see here you are using JasperFillManager.fillReport(JasperReport jasperReport, java.util.Map<java.lang.String,java.lang.Object> parameters, java.sql.Connection connection)

Here In your code JasperReport is an instance of net.sf.jasperreports.engine.JapserReport.

But in my code I am using org.zkoss.zkex.zul .Jasperreport and i don't see connection.
And hence I continue to face this issue!!

And my code is based on http://www.zkoss.org/zkdemo/reporting/jasperreport

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-01-18 03:29:37 +0800

Seen: 794 times

Last updated: Jul 23 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More