how to use db name as input control/parameter in jasper report - jasper-reports

I am using jasper reports and want to give user to select the db name as input control and want to use that in query.
tool used is ireport /jasper soft studio 6.x

Got the solution for above question I used list of input control and then used the parameter value as
$P!{parametername}
Same as usage of table name as parameter/input control in jasper.

If I am understanding correctly, following code might help you.
Ofcourse "foo DB" and "baz DB" should have same tables.
Connection conn;
// Change the DB depend on input value.
if (userInput.equals("foo")){
conn = DriverManager.getConnection("jdbc:mysql://localhost/foo", "user", "password");
}
else if (userInput.equals("baz")){
conn = DriverManager.getConnection("jdbc:mysql://localhost/baz", "user", "password");
}
else{
// error
}
JasperReport jasper = (JasperReport)JRLoader.loadObject(your_jasper_filePath);
Map paramMap = new HashMap();
paramMap.put("param1", "some_param1");
paramMap.put("param2", "some_param2");
JasperPrint print = JasperFillManager.fillReport(jasper, paramMap, conn);
JasperExportManager.exportReportToPdfFile(print, PdfPath);

Related

Getting 3 mins to Generate the Jasper Report

I am developing the Invoice printing System using the Jasper report, Now I have can print the report but it will take 2000ms to load the report, also 2000ms take to start the printing,
These are I am using the JAR FILES,
commons-beanutils-1.4
commons-digester-1.7
commons-logging-1.0.3
commons-beanutils-1.4
groovy-all-1.7.5
batik-all-1.7
barcode4j-2.1
itextpdf-5.1.0
jasperreports-6.0.3
xercesImpl-2.11.0
xml-apis-ext-1.3.04
preparedStatement = conn.prepareStatement(sqlString);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
HashMap<String, Object> hm = new HashMap<>();
JasperDesign jasperDesign = JRXmlLoader.load(new File(
"C:/Invoice/Invoice.jrxml"));
JRDesignQuery designQuery = new JRDesignQuery();
designQuery.setText(sqlString);
jasperDesign.setQuery(designQuery);
JasperReport jasperReport = JasperCompileManager
.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, null, conn);
for (int i = 0; i < copies; i++) {
JasperPrintManager.printReport(jasperPrint, false);
}
JasperViewer.viewReport(jasperPrint);
The above code I used to print the report but, getting too much time to load the report. Please suggest my fault and some other idea...
I guess the SQL query could be the problem. Have you tried executing it in a sql query tool and check how long it runs? Optimizing it should yield a performance win.
You are also running the query twice: before the report and when you are filling it via the JasperFillManager.fillReport() call.
If there are no specific reasons to check wheter the query contains any records, I would recommand to let JasperReports handle the query and the case that no record is found. You can determine a specific behaviour in that case via the "When No Data Type" property in the reportdesign.

iDB2Commands in Visual Studio 2010

These are the basic things I know about iDB2Commands to be used in Visual Studio 2010. Could you please help me how could I extract data from DB2? I know INSERT, DELETE and Record Count. But SELECT or Extract Data and UPDATE I don't know.
Imports IBM.Data.DB2
Imports IBM.Data.DB2.iSeries
Public conn As New iDB2Connection
Public str As String = "Datasource=10.0.1.11;UserID=edith;password=edith;DefaultCollection=impexplib"
Dim cmdUpdate As New iDB2Command
Dim sqlUpdate As String
conn = New iDB2Connection(str)
conn.Open()
'*****Delete Records and working fine
sqlUpdate = "DELETE FROM expusers WHERE username<>#username"
cmdUpdate.Parameters.Add("username", iDB2DbType.iDB2Date)
cmdUpdate.Parameters("username").Value = ""
'*****Insert Records and working fine
sqlUpdate = "INSERT INTO expusers (username, password, fullname) VALUES (#username, #password, #fullname)"
cmdUpdate.Parameters.Add("username", iDB2DbType.iDB2VarChar)
cmdUpdate.Parameters.Add("password", iDB2DbType.iDB2VarChar)
cmdUpdate.Parameters.Add("fullname", iDB2DbType.iDB2VarChar)
cmdUpdate.Parameters("username").Value = txtUsername.Text
cmdUpdate.Parameters("password").Value = txtPassword.Text
cmdUpdate.Parameters("fullname").Value = "Editha D. Gacusana"
'*****Count Total Records and working fine
Dim sqlCount As String
Dim cmd As New iDB2Command
sqlCount = "SELECT COUNT(*) AS count FROM import"
cmd = New iDB2Command(Sql, conn)
Dim count As Integer
count = Convert.ToInt32(cmd.ExecuteScalar)
'*****Update Records and IT IS NOT WORKING AT ALL
sqlUpdate = "UPDATE expusers SET password = #password WHERE RECNO = #recno"
cmdUpdate.Parameters.Add("recno", iDB2DbType.iDB2Integer)
cmdUpdate.Parameters.Add("password", iDB2DbType.iDB2VarChar)
cmdUpdate.Parameters("recno").Value = 61
cmdUpdate.Parameters("password").Value = txtPassword.Text
cmdUpdate.Connection = conn
cmdUpdate.CommandText = sqlUpdate
cmdUpdate.ExecuteNonQuery()
conn.Close()
Please help me how to code the SELECT query wherein I could extract/fetch data from DB2 Database. Also, how could i update the records in the database.
Thanks!
Instead of ExecuteNonQuery(), look at ExecuteReader(). I don't have VS2010 installed, but try something like this:
iDB2Command cmdSelect = new iDB2Command("SELECT username, password, fullname FROM expusers", conn);
cmdSelect.CommandTimeout = 0;
iDB2DataAdapter da = new iDB2DataAdapter(cmdSelect);
DataSet ds = new DataSet();
da.Fill(ds, "item_master");
GridView1.DataSource = ds.Tables["expusers"];
GridView1.DataBind();
Session["TaskTable"] = ds.Tables["expusers"];
da.Dispose();
cmdSelect.Dispose();
cn.Close();
See: http://gugiaji.wordpress.com/2011/12/29/connect-asp-net-to-db2-udb-for-iseries/
If you aren't trying to bind to a grid, look at iDB2Command.ExecuteReader() and iDB2DataReader()
The DELETE is working fine? The code has the parameter type for "username" set to iDB2Date. The INSERT has "username" set to iDB2VarChar. How is the column defined in the table? Char, Varchar or Date?
On the UPDATE, you reference RECNO, but that does not seem to be a column in the table. Updating a relational database table by row number is a bad idea - the row numbers are not guaranteed to stay constant. If you are just testing, as I think you are, don't use RECNO, use RRN(). The DB2 for i syntax is WHERE rrn(expusers) = #recno
To help your testing, do a SELECT without a WHERE clause and list out all the rows. Make sure the name stored in the username column matches the name you are trying to update. Pay particular attention to the case of the data. If the name in expusers looks like "EDITHA D. GACUSANA", and #username is "Editha D. Gacusana" then it will not match on the WHERE clause.

Insert query in jasper reports

Is it possible to execute "insert query" in IReports/jasper reports during report generation?
Yes, the idea you need is parameters using this syntax: $P!{PARAM_NAME}.
So your entire SQL query (or other type of query) could be simply $P!{SQL}. Then you pass in exactly the dynamic SQL that you need.
UPDATE:
After reading Sharad's comment, I realized that my answer above is not good. What I wrote is true... but it fails to address the core question.
No, your report cannot really execute an insert statement. Strictly speaking, I'm sure it's not impossible. You could add a scriptlet or custom function in a .jar file that makes a connection and does an insert. But realistically speaking... a report will execute one or more queries. The JR framework is not intended to execute inserts or updates.
Yes you can. You can execute the query when you want to display the report. Here is a sample that works for me.
try {
Map parameters = new HashMap();
connectionString ="jdbc:mysql://localhost/myDb", "myUsername", "myPassword"
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString);
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
JRResultSetDataSource rsdt = new JRResultSetDataSource(rs);
JasperPrint jp;
jp = JasperFillManager.fillReport("sourceFileName.jasper", parameters, rsdt);
JasperViewer jv = new JasperViewer(jp, false);
jv.setVisible(true);
} catch (ClassNotFoundException | SQLException | JRException ex) {
ex.printStackTrace();
}

crystal report parameters

I use the following code to pass parameters into a CR
// Reuse myDiscreteValue, and assign second country
myParam = new ParameterField();
myDiscreteValue = new ParameterDiscreteValue();
myParam.Name = "#toDate";
myDiscreteValue.Value = RmtUtility.Utility.DisplayDate(toDate);
myParam.CurrentValues.Add(myDiscreteValue);
myParams.Add(myParam);
I get an error message "Missing Parameter Value" when I tried to export the document using CR Option.
:(
Try clearing and then setting the values of the parameters within the Report object.
Something like this:
ParameterDiscreteValue paramDV = new ParameterDiscreteValue();
paramDV.Value = RmtUtility.Utility.DisplayDate(toDate);
report.ParameterFields["#toDate"].CurrentValues.Clear();
report.ParameterFields["#toDate"].DefaultValues.Clear();
report.ParameterFields["#toDate"].CurrentValues.Add(paramDV);
Note: report being your Crystal Report Document

Crystal Report on C#

Well I have a Cyrstal report which has 4 sub reports on and it is linked through an ItemID column and a Culture, so it has a parameter value "?Pm-ItemID" and "?Pm-Culture" now i'm using DataSet to load the data to the Crystal Report's datasource, when I run the report its giving me an error which was an asking parameter field was not suplied, so i think my question would be what am I going to pass to those ParameterFields?
here's an idea.
ReportDocument myreport = new ReportDocument();
myreport.Load("C:\MyReport.rpt");
DataSet ds = GenerateReportData();
myreport.SetDataSource(ds);
//Loop through each to Load the DataSet
for (int i = 0; i < myreport.Subreports.Count; i++)
{
ReportDocument subreport = myreport.SubReports[i];
DataSet subds = GenerateReportData(subreport.name)
subreport.SetDataSource(subds);
}
//I can see that there's a parameterfields in myreport.ParameterFields
//As I look through inside it there are 8 ParameterFields repeating Pm-ItemID and Pm-Culture
foreach (ParameterField pf in myreport.ParameterFields)
{
myreport.SetParameterValue(pf.Name, Value???);
}
Well, I see what's wrong.
ReportDocument subreport = myreport.SubReports[i];
DataSet subds = GenerateReportData(subreport.name)
subreport.SetDataSource(subds);
should not be done this way, it should be
DataSet subds = GenerateReportData(subreport.name)
myreport.SubReports[i].SetDataSource(subds);
I don't know about Crystal, but in SSRS it works like this:
1) create a subreport and create some parameters
2) create the main report, put there the subreport and in the properties you can specify what to bind to the subreport's parameters
Conclusion: I don't think this is supposed to be set in code, rather report designer.