I am looking for information on the following:
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.
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();
}
}
3 Answers:
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,
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