Jump to content
JasperReports Library 7.0 is now available ×

Sending query from java application


tanvirtonu

Recommended Posts

I want to send the entire sql query from my java application into a compiled jasper report.For this,I tried with the following code-

Code:

class frmReport
{

static HashMap <String,String> map=new HashMap<String,String>();

frmReport()

{
map.put("Sql","Select * from loan where loan_id=1"«»);
runReport("amabrick","root","tanvir","./Report/MyReport.jasper"«»);

}

public static void runReport(String databaseName, String userName, String password,String reportFile)
{
try{Connection jdbcConnection = connectDB("amabrick","root", "tanvir"«»);
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, map, jdbcConnection);
JasperViewer.viewReport(jasperPrint);
}catch(Exception ex) {
String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}

public static Connection connectDB(String databaseName, String userName, String pword)
{
Connection jdbcConnection = null;

String serverName = "localhost:3306";
String mydatabase = databaseName;
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = userName;
String password = pword;

try{
Class.forName("com.mysql.jdbc.Driver"«»);
jdbcConnection= DriverManager.getConnection(url, username, password);

}catch(Exception ex) {
String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
return jdbcConnection;
}

I cant understand how to use a Map to send query to jasper report.And how should I configure iReport for this.I mean-do I need any paraameter or something else? PLs Help me brother.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Use a parameter for the query entitled "Sql" (It has has to match up to the name attribute in the map). For your report query, simply put $P!{Sql}.

Where shud I put- $P!{Sql} . Which is the name attribute in Map.

I made a parameter and i name it- Sql , type-String,then in report query I ONLY wrote- $P!{Sql}.My code was as it is now. But it didnt work. Cud u be a little clearer.

Post edited by: tanvirtonu, at: 2007/12/10 18:30

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