Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. Connection conn = null;
  4. Statement stmt = null;
  5. try {
  6. Class.forName("com.mysql.jdbc.Driver");
  7. System.out.println("Connecting to database...");
  8. conn = DriverManager.getConnection(
  9. "jdbc:mysql://10.9.1.1:3306/XXXXX", "Unm", "pass");
  10. System.out.println("Creating statement...");
  11. stmt = conn.createStatement();
  12. ResultSet rs = stmt
  13. .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'");
  14.  
  15. // Excel file generation code
  16. String filename = "D:/RONAKExcelFile.xls" ;
  17. HSSFWorkbook workbook = new HSSFWorkbook();
  18. HSSFSheet sheet = workbook.createSheet("FirstSheet");
  19. HSSFRow rowhead = sheet.createRow((short) 0);
  20. rowhead.createCell(0).setCellValue("List of Exceptions");
  21. HSSFRow row = sheet.createRow((short)1);
  22. int i = 0;
  23. while (rs.next()) {
  24. // Retrieve by column name
  25. String msg = rs.getString("EXCEPTION_MESSAGE");
  26. // Display values
  27. System.out.println("Exception=> " + msg);
  28. row.createCell(i).setCellValue(msg);
  29. i++;
  30. System.out.println("Length = " + msg.length());
  31. }
  32. FileOutputStream fileOut = new FileOutputStream(filename);
  33. workbook.write(fileOut);
  34. fileOut.close();
  35. System.out.println("Excel file has been generated!");
  36. rs.close();
  37. stmt.close();
  38. conn.close();
  39. } catch (SQLException se) {
  40. // Handle errors for JDBC
  41. System.out.println("Unable to make connection with database...!");
  42. se.printStackTrace();
  43. } catch (Exception e) {
  44. // Handle errors for Class.forName
  45. e.printStackTrace();
  46. } finally {
  47. // finally block used to close resources
  48. try {
  49. if (stmt != null)
  50. stmt.close();
  51. } catch (SQLException se2) {
  52. }// nothing we can do
  53. try {
  54. if (conn != null)
  55. conn.close();
  56. } catch (SQLException se) {
  57. se.printStackTrace();
  58. }// end finally try
  59. }// end try
  60. System.out.println("Goodbye!");
  61. }// end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement