Guest User

Untitled

a guest
Oct 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. public void connectToAndQueryDatabase(String username, String password, String dirPath){
  2.         BufferedWriter bw = null;
  3.         String filePath = "";
  4.         try {
  5.             Class.forName("oracle.jdbc.OracleDriver");
  6.         } catch (ClassNotFoundException ex) {
  7.             Logger.getLogger(WBReader.class.getName()).log(Level.SEVERE, null, ex);
  8.         }
  9.         String url = "jdbc:oracle:thin:@//192.168.1.234:1521/maxrb";
  10.         Connection con = null;
  11.         try {
  12.             con = DriverManager.getConnection(url, username, password);
  13.         } catch (SQLException ex) {
  14.             Logger.getLogger(WBReader.class.getName()).log(Level.SEVERE, null, ex);
  15.         }
  16.         try {
  17.             Statement stmt = con.createStatement();
  18.             try{
  19.                 String qry = "select "
  20.                         + "assetnum, wonum, round((actfinish-actstart)*24, 2) duration "
  21.                         + "from "
  22.                         + "workorder "
  23.                         + "where "
  24.                         + "status in ('COMP', 'INPRG') "
  25.                         + "and istask = '0' "
  26.                         + "order by "
  27.                         + "assetnum";
  28.                 ResultSet rset = stmt.executeQuery(qry);
  29.                 String str = "";
  30.                 try{
  31.                     String fileName = "";
  32.                     String file1 = "";
  33. //                    int j = 0;
  34.                     while (rset.next()){
  35.                         file1 = rset.getString(1) + ".txt";
  36.                         if (!file1.equals(fileName)){
  37. //                            System.out.println(j + ". " + fileName + " : " + file1);
  38. //                            j++;
  39.                             try{ bw.close();} catch (Exception e){e.toString();}
  40.                             fileName = file1;
  41.                             filePath = dirPath + "/" + file1;
  42.                             File file = new File(filePath);
  43.                             try {
  44.                                 bw = new BufferedWriter(new FileWriter(file, false));
  45.                             } catch (IOException ex) {
  46.                                 Logger.getLogger(WBReader.class.getName()).log(Level.SEVERE, null, ex);
  47.                             }
  48.                         }
  49.                         float fl;
  50.                         fl = rset.getFloat(3);
  51.                         str = fl < 0.0001 ? "" : String.valueOf(fl);
  52.                         try {
  53. //                            System.out.println("text: " + str);
  54.                             if (!str.equalsIgnoreCase("")){
  55.                                 bw.write(str);
  56.                                 bw.write("\r\n");
  57.                             }
  58.                         } catch (IOException ex) {
  59.                             Logger.getLogger(WBReader.class.getName()).log(Level.SEVERE, null, ex);
  60.                         }
  61.                     }
  62.                     try{ bw.close();} catch (Exception e){e.toString();}
  63.                 } finally{
  64.                     try { rset.close(); } catch (Exception ignore) {}
  65.                 }
  66.             } finally {
  67.                 try { stmt.close(); } catch (Exception ignore){}
  68.             }
  69.         } catch (SQLException ex) {
  70.             Logger.getLogger(WBReader.class.getName()).log(Level.SEVERE, null, ex);
  71.         } finally {
  72.             try {con.close();} catch (Exception ignore){}
  73.         }
  74.     }
Add Comment
Please, Sign In to add comment