简体   繁体   English

我想使用java在excel中打印计数总数

[英]I want to print total number of count with exception in excel using java

I want to print in excel Like, "5" "nullpointer exception" "15" "ZZZZZ exception" "7" "XYZ exception" 我想在excel中打印像“ 5”“空指针异常”“ 15”“ ZZZZZ异常”“ 7”“ XYZ异常”

I am fetching all the exceptions from mysql database. 我正在从mysql数据库中获取所有异常。

Below code gives output of all the exceptions in excel file but dont know how to group those exceptions? 下面的代码给出了excel文件中所有异常的输出,但是不知道如何对这些异常进行分组? One more help, this code prints all the data horizontally Like, "nullpointer exception" "ZZZZZ exception" "XYZ exception". 另一个帮助是,此代码水平打印所有数据,例如“ nullpointer异常”,“ ZZZZZ异常”,“ XYZ异常”。

can you help me to print it vertically and group all those exceptions with count? 您能帮我垂直打印它并按计数将所有这些例外分组吗? Thanks. 谢谢。

public class Abcd {

    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://10.9.1.1:3306/XXXXX", "Unm", "pass");
    System.out.println("Creating statement...");
            stmt = conn.createStatement();
            ResultSet rs = stmt
                    .executeQuery("select EXCEPTION_MESSAGE from TABLENAME where E_TYPE = 'FAILED' and O_TYPE = 6 and TIME_ >= '2016-4-8 00:00:00' and TIME_ <= '2016-4-11 00:00:00'");

            // Excel file generation code
            String filename = "D:/RONAKExcelFile.xls" ;
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("FirstSheet");
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell(0).setCellValue("List of Exceptions");
            HSSFRow row = sheet.createRow((short)1);
            int i = 0;
            while (rs.next()) {
                // Retrieve by column name
                String msg = rs.getString("EXCEPTION_MESSAGE");
                // Display values
                System.out.println("Exception=> " + msg);
                row.createCell(i).setCellValue(msg);
                i++;
                System.out.println("Length = " + msg.length());
            }
            FileOutputStream fileOut = new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            System.out.println("Excel file has been generated!");
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // Handle errors for JDBC
            System.out.println("Unable to make connection with database...!");
            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) {
            }// nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }// end finally try
        }// end try
        System.out.println("Goodbye!");
    }// end main
}

Some suggestions: 一些建议:

1.) Use the GROUP BY statement in your query: 1.)在查询中使用GROUP BY语句:

SELECT
    EXCEPTION_MESSAGE, COUNT(*) AS EXCEPTION_COUNT
FROM
    ....
GROUP BY
    EXCEPTION_MESSAGE;

2.) Now each entry of your ResultSet matches one row of your "target-excel". 2.)现在,ResultSet的每个条目都与“ target-excel”的一行匹配。 Ie Your while loop should look like: 即您的while循环应如下所示:

for(int i=0; rs.next(); i++)
{
    HSSFRow row = sheet.createRow(i);
    row.createCell(0).setCellValue(rs.getString("EXCEPTION_MESSAGE"));
    row.createCell(1).setCellValue(rs.getString("EXCEPTION_COUNT"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从用户输入的java中计算并打印空行的总数 - count and print total number of empty lines from user input in java I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError” - I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError” 使用JAVA从Excel获取数据并将行数打印到Excel中 - Getting data from Excel and print count of rows into Excel using JAVA 我想在 java 中使用递归打印一系列 - i want to print a series using recursion in java 我想通过 java 中的递归打印 1 到 n 个数字 - i want print 1 to n number by help of recursion in java 我想在不使用 Java 中的 Print 函数的情况下打印任何文本? - I want to print any text without using Print function in java? 如何在Java中获取员工总数? - How to get total count number of employee in Java? 我如何使用apachi POI java从e​​xcel获取空白行总数(空白行)之外的总行数? - How can i get total number of rows excluding from BLANK SPACES(blank rows) from excel using apachi POI java? 我想用 Java 打印这个模式 - I want to print this pattern in Java 我想使用xpath读取xml并获取java中的父级计数 - I want to read xml using xpath and get the count of parent in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM