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

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
con;
    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 {
            Class.forName(driverName).newInstance();
            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) {
               try{
                 
con.close();
               } catch(Exception e){
                  e.printStackTrace();
               }
            }
         }
       
         return
con;
    }  


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

        Connection con = getConnection();
       
    }   
}

aperez_6's picture
221
Joined: Mar 20 2018 - 1:12am
Last seen: 3 years 11 months ago

3 Answers:

Scriptlet class is made available to other classes as well so you'll be creating a security hole by passing username/password for the data source to scriptlet.

hozawa's picture
190407
Joined: Apr 24 2010 - 4:31pm
Last seen: 4 years 3 months ago

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)?

Regards,

 

aperez_6's picture
221
Joined: Mar 20 2018 - 1:12am
Last seen: 3 years 11 months ago

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

aperez_6's picture
221
Joined: Mar 20 2018 - 1:12am
Last seen: 3 years 11 months ago
Feedback