Jump to content
Changes to the Jaspersoft community edition download ×

How to pass from Jasper to Scriptlet the Username/password of the Data Adapters?


Recommended Posts

Good afternoon,

I am looking for information on the following:

- From Jasper I'm using a Scriptlet, I have several data adapters (different connections). Well, from my Java class, I want to try to establish the connection without directly using the username and password (it's much better without showing them). But yes, of the URL connection.

My idea is to pass the username / password connection from the same jasper to the Scriptlet method / function. It will be the best way, won't?

If someone knows/works with this, I would appreciate an example of how to interact between jasper and the java class (scriptlet).

I am sure that someone knows the answer and it would be a detail to explain to all the people in the same situation tha's me.


Thanks in advance.
Aranzazu P.
PD: My attemps in the Java Class
package gestor.documental;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.fill.JRFillParameter;

public class ConnectionManager extends JRDefaultScriptlet{
    private static String url = "jdbc:oracle:thin:@host:port:SERVICE_NAME";
    private static String driverName = "oracle.jdbc.driver.OracleDriver";      
    private static String username = "username";  
    private static String password = "pswd";
    private static Connection
    private Connection conn;
    private Object server;


    // EXAMPLE 1: I try to use but it doesn't work and I don't know WHY? I don't understand why the people who exposed him in a forum and it works.
    public Connection getConnection_pruebas() throws JRScriptletException {

        if (getParameterValue(JRFillParameter.REPORT_CONNECTION) != null) {
conn= ((Connection) getParameterValue(JRParameter.REPORT_CONNECTION) );
        } else {
          throw new RuntimeException("No db-connection configured in the report!");

        return conn;

    // EXAMPLE 2: I try to use but it doesn't work

    public static Connection getConnection() throws JRScriptletException, ClassNotFoundException {
        try {
            try {
                con= DriverManager.getConnection(url, username, password);
            } catch (SQLException ex) {
                // log an exception. fro example:
                //System.out.println("Failed to create the database connection.");                
        } catch (ClassNotFoundException ex) {
            System.out.println("ClassNotFound: "
                + ex.getCause() );
        } finally {
            if (
con != null) {
               } catch(Exception e){

    public static void main(String[] args) throws JRScriptletException, SQLException {               

        Connection con = getConnection();

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Hi hozawa,

I was on vacation these days and I just saw it. Well with your answer, does it mean that it is impossible to pass the username and password of the Jasper connection?

How do you work in a class to execute queries from the Jasper connection (using the Scriptlet)?



Link to comment
Share on other sites

  • 4 weeks later...

Now, that I have time to give my solution that I discovered; between your answers and what was searched in other forums. Say that through the REPORT_CONNECTION parameter and passing it through the Scriptlet through java functions; I have managed to get the report connection directly.

And that's it!

The thread of the question closes, for my part

Link to comment
Share on other sites

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