Jump to content
We've recently updated our Privacy Statement, available here ×

Getting JDBC Connection inside Scriptlet?s


2005 IR Help

Recommended Posts

By: Allan Chamon - achamon

Getting JDBC Connection inside Scriptlet?s

2004-09-20 11:27

How can I get the current JDBC connection inside scriptlets. I want to get the connection in order to load a map structure based on multiple resultsets. So, what I want is that:

 

- the method beforeReportInit (from the interface JRDefaultScriptlet) would get the connection

- the following statements would look like that

ResultSet rs1 = connection.createStatement(query1);

String att1 = rs1.getString("ATT1");

ResultSet rs2 = connection.createStatement(query2);

String att2 = rs2.getString("ATT1");

HashMap map;

map.put("ATT1", att1);

map.put("ATT2", att2);

 

this.setVariableValue("HASHMAP", map );

 

 

Thanks in advance.

 

 

 

 

 

 

 

 

 

By: Allan Chamon - achamon

RE: Getting JDBC Connection inside Scriptlet?s

2004-09-21 10:57

Any help? It?s extremely important for me to get some clue about this question.

 

 

 

 

By: Laurent Asfaux - del65

RE: Getting JDBC Connection inside Scriptlet?

2004-09-22 03:44

I think the best way you can do that is to pass directly a JRDataSource as a parameter, like you do it for a subreport.

 

For knowing if the datasource is present or not, you need also a list of the names of your JRDataSource parameters.

 

In your scriptlet, add this method :

 

/* retrieve a field value from a JRDataSource */

(Object) getField(String source,String fieldName) {

 

JRDataSource myJRDataSource = this.getVariableValue(source);

 

return myJRDataSource.getField(fieldName);

}

 

and include in the beforeDetailInit method the JRDataSource.next() method for each of your sources :

 

String[] listOfSources = this.getVariableValue(listOfSources);

 

// it is faster from the last to the first

for (i=listOfSources.length();i>1;i--) {

this.getVariableValue(listOfSources).next();

}

 

 

and finaly add this where you want to use your datas :

 

(<String>) ((<myScriptlet>) myScriptlet.getField("mySource","ATT1"));

 

don't forget to set the correct field type (String, Date, etc...) !

 

 

OK, this method is probably a lot more slower than others ;-), but it may be a simple way to do the job in "write once use anywhere" manner.

 

I hadn't tested it yet but it should work quite well... Just check the correct syntax to retrieve a Field value and the size of an array, and add the necessary try, catch statements...

 

H@ppy scripting !!

 

 

 

 

By: Burim Gosalci - bgosalci

RE: Getting JDBC Connection inside Scriptlet?s

2004-09-22 03:47

As long as the Scriptlet class extends JRDefaultScriptlet you can retrieve any parameters available on the JasperReport. You can retrieve the connection by using the method getParameterValue.

 

E.g.

connection = (Connection)this.getParameterValue("REPORT_CONNECTION");

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