I am just working on a simple JasperReport project. So this I have a Java code for authenticating the username and password entered by the user and another Java class file for exporting the report in a .pdf format. So please help me in integrating both the classes and generating the report. I just want to print the username and password in the report.
public class Login {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/CNVS";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args) {
String userName;
String password;
Connection conn = null;
Statement stmt = null;
Scanner in = new Scanner(System.in);
System.out.println("Enter the username");
userName = in.nextLine();
System.out.println("Enter the password");
password = in.nextLine();
try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
// System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
// System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT USER_NAME, PASSWORD FROM USER_INFO WHERE USER_NAME = '" + userName + "'";
ResultSet rs = stmt.executeQuery(sql);
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
String dbUserName = rs.getString("USER_NAME");
String dbPassword = rs.getString("PASSWORD");
if (userName.equals(dbUserName) && password.equals(dbPassword)) {
System.out.println("Successfully Logged in ! ");
} else {
System.out.println("Please enter the valid login credentials");
}
}
// STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
And here is the class for Java call
public class JavaCallJasperReport {
public static void main(String[] args) throws JRException,
ClassNotFoundException, SQLException {
String reportSrcFile = "C:/Users/stephenjebaraj_b/Test_Report.jrxml";
// First, compile jrxml file.
JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
Connection conn = MySQLConnUtils.getMySQLConnection();
// Parameters for report
Map parameters = new HashMap();
parameterMap.put(USER_NAME, dbUserName);
parameterMap.put(PASSWORD, dbPassword);
JasperPrint print = JasperFillManager.fillReport(jasperReport,
parameters, conn);
// Make sure the output directory exists.
File outDir = new File("C:/JasperReport_Test");
outDir.mkdirs();
// PDF Exportor.
JRPdfExporter exporter = new JRPdfExporter();
ExporterInput exporterInput = new SimpleExporterInput(print);
// ExporterInput
exporter.setExporterInput(exporterInput);
// ExporterOutput
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
"C:/JasperReport_Test/TestJasper.pdf");
// Output
exporter.setExporterOutput(exporterOutput);
//
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
System.out.print("Done!");
}
}
解决方案
As @Alex K mentioned, what you have to do is edit jrxml file:
Add two report parameter USER_NAME and PASSWORD. The parameter name must be same as first argument of parameterMap.put(?, ?) in JavaCallJasperReport class.
Edit the report element to display these variables. For example, if you want to display the value of report parameter PASSWORD in a TextField. Just set it's Text Field Expression to $P{PASSWORD}.
For more detail, please refer this article.