Guest User

Untitled

a guest
Oct 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package wbreader;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import java.io.File;
  13. import java.io.FileWriter;
  14.  
  15. /**
  16.  *
  17.  * @author sdti
  18.  */
  19. public class WBReader {
  20.    
  21.     public void connectToAndQueryDatabase(String username, String password, String dirPath){
  22.         BufferedWriter bw = null;
  23.         String filePath = "";
  24.         ResultSet rset = null;
  25.         Statement stmt = null;
  26.         String url = "jdbc:oracle:thin:@//192.168.1.234:1521/maxrb";
  27.         Connection con = null;
  28.        
  29.         try {
  30.             Class.forName("oracle.jdbc.OracleDriver");
  31.             con = DriverManager.getConnection(url, username, password);
  32.        
  33.             stmt = con.createStatement();
  34.             String qry = "select "
  35.                     + "assetnum, wonum, round((actfinish-actstart)*24, 2) duration "
  36.                     + "from "
  37.                     + "workorder "
  38.                     + "where "
  39.                     + "status in ('COMP', 'INPRG') "
  40.                     + "and istask = '0' "
  41.                     + "order by "
  42.                     + "assetnum";
  43.             rset = stmt.executeQuery(qry);
  44.             String str = "";
  45.             String fileName = "";
  46.             String file1 = "";
  47.  
  48.             while (rset.next()){
  49.                 file1 = rset.getString(1) + ".txt";
  50.                 if (!file1.equals(fileName)){
  51.                     try{ bw.close();} catch (Exception e){e.toString();}
  52.                     fileName = file1;
  53.                     filePath = dirPath + "/" + file1;
  54.                     File file = new File(filePath);
  55.                     bw = new BufferedWriter(new FileWriter(file, false));
  56.                 }
  57.                 float fl;
  58.                 fl = rset.getFloat(3);
  59.                 str = fl < 0.0001 ? "" : String.valueOf(fl);
  60.                 if (!str.equalsIgnoreCase("")){
  61.                     bw.write(str);
  62.                     bw.write("\r\n");
  63.                 }
  64.             }
  65.         } catch (Exception e){
  66.             e.printStackTrace();
  67.         } finally {
  68.             try {
  69.                 bw.close();
  70.                 rset.close();
  71.                 stmt.close();
  72.                 con.close();
  73.             } catch (Exception ex) {
  74.                 ex.printStackTrace();
  75.             }
  76.         }
  77.     }
  78.    
  79.     public static void main(String[] args) {
  80.         WBReader wbReader = new WBReader();
  81.         String address = "D:\\Root\\Temp\\Test";
  82.         String backslash= System.getProperty("file.separator");
  83.         address= address.replace(backslash,"/");
  84.         System.out.println(address);
  85.         try{
  86.             wbReader.connectToAndQueryDatabase("maximo", "maximo", address);
  87.         }catch(Exception e){
  88.             e.toString();
  89.         }
  90.     }
  91.  
  92. }
Add Comment
Please, Sign In to add comment