我想在 Excel 中打印,例如, “5”“空指针异常” “15”“ZZZZZ异常” “7”“XYZ异常”
我正在从 mysql 数据库中获取所有异常。
下面的代码给出了 Excel 文件中所有异常的输出,但不知道如何对这些异常进行分组?还有一个帮助,此代码水平打印所有数据,例如“空指针异常”“ZZZZZ 异常”“XYZ 异常”。
你能帮我垂直打印它并用计数对所有这些异常进行分组吗?谢谢。
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
}
最佳答案
一些建议:
1.) 在查询中使用 GROUP BY
语句:
SELECT
EXCEPTION_MESSAGE, COUNT(*) AS EXCEPTION_COUNT
FROM
....
GROUP BY
EXCEPTION_MESSAGE;
2.) 现在,结果集中的每个条目都与“target-excel”的一行相匹配。 IE。你的 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"));
}
关于java - 我想使用java打印excel中的异常计数总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842525/