简单的JasperReport生成

时间:2016-03-11 07:29:53

标签: java jasper-reports

我正在研究一个简单的JasperReport项目。所以我有一个Java代码用于验证用户输入的用户名和密码,另一个Java类文件用于以.pdf格式导出报告。所以请帮我整合课程并生成报告。我只想在报告中打印用户名和密码。

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!");
}

}

这是Java调用的类

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<String, Object> parameters = new HashMap<String, Object>();
    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!");
}

}

1 个答案:

答案 0 :(得分:1)

正如@Alex K所提到的,你要做的就是编辑jrxml文件:

  1. 添加两个报告参数USER_NAMEPASSWORD。参数名称必须与parameterMap.put(?, ?)中的JavaCallJasperReport class的第一个参数相同。
  2. 编辑报表元素以显示这些变量。例如,如果要在 TextField 中显示报表参数PASSWORD的值。只需将其文本字段表达式设置为$P{PASSWORD}
  3. 有关详细信息,请参阅this article