简单的JasperReport生成 [英] Simple JasperReport generation

查看:78
本文介绍了简单的JasperReport生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在做一个简单的JasperReport项目.因此,这有一个用于验证用户输入的用户名和密码的Java代码,以及另一个用于以.pdf格式导出报告的Java类文件.因此,请帮助我整合两个类并生成报告.我只想在报告中打印用户名和密码.

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

}

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

}

推荐答案

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

As @Alex K mentioned, what you have to do is edit jrxml file:

  1. 添加两个报告参数USER_NAMEPASSWORD.参数名称必须与JavaCallJasperReport classparameterMap.put(?, ?)的第一个参数相同.
  2. 编辑报告元素以显示这些变量.例如,如果要在 TextField 中显示报告参数PASSWORD的值.只需将其文本字段表达式设置为$P{PASSWORD}.
  1. Add two report parameter USER_NAME and PASSWORD. The parameter name must be same as first argument of parameterMap.put(?, ?) in JavaCallJasperReport class.
  2. 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.

这篇关于简单的JasperReport生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆