aperez_6 Posted April 17, 2019 Share Posted April 17, 2019 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(); } } Link to comment Share on other sites More sharing options...
hozawa Posted April 19, 2019 Share Posted April 19, 2019 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. Link to comment Share on other sites More sharing options...
aperez_6 Posted April 23, 2019 Author Share Posted April 23, 2019 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, Link to comment Share on other sites More sharing options...
aperez_6 Posted May 20, 2019 Author Share Posted May 20, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now