Java Code to Read History Data of Job Using Database


  1. package com.oimacademy.NeedEvaluate;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.ObjectInputStream;
  5. import java.sql.Blob;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.text.SimpleDateFormat;
  12. public class ReadJobHistroyDataDB {
  13.  private static Connection connectionObject = null;
  14.  private static Connection makeConnection() {
  15.   try {
  16.    Class.forName("oracle.jdbc.OracleDriver");           
  17.    connectionObject = DriverManager.getConnection("jdbc:oracle:thin:@host:5521:oimdb", "SCHEMA", "PASSWORD");
  18.   } catch (SQLException se) {
  19.      se.printStackTrace();
  20.   } catch (Exception e) {   
  21.    e.printStackTrace();
  22.   }
  23.   return connectionObject;
  24.  }
  25.  public static synchronized Connection getConnection() {
  26.   if (connectionObject == null) {
  27.    connectionObject = makeConnection();
  28.   }
  29.   return connectionObject;
  30.  }
  31.  public static void closeConnection() {
  32.              System.out.println("Closing  to database connection ...");
  33.   try {   
  34.    if (connectionObject != null)
  35.     connectionObject.close();
  36.   } catch (SQLException se) {
  37.    se.printStackTrace();
  38.   }
  39.  }
  40.  public void execute() throws Exception {
  41.   String mname = "execute()";
  42.   String jname = "Issue Audit Messages Task";//Job Name  which is having issue.
  43.   jname=jname.replaceAll("\\*", "%");
  44.   if (null == jname)
  45.    throw new NullPointerException("JobName is null.");
  46.   jname = jname.replace('*', '%');
  47.   String query = "select ERROR_DATA,job_name from JOB_HISTORY where id=7747";
  48.   try {
  49.             PreparedStatement prepStmt = getConnection().prepareStatement(query);           
  50.    ResultSet rs = prepStmt.executeQuery(query);   
  51.    boolean found = false;
  52.    while(rs.next()) {
  53.     System.out.println("");
  54.     System.out.println("*****************"+rs.getString("job_name")+"*****************");
  55.     found = true;
  56.     Blob b = rs.getBlob("ERROR_DATA");
  57.     if(b==null)
  58.      continue;
  59.     InputStream bis = b.getBinaryStream();
  60.     ObjectInputStream ois = new ObjectInputStream(bis);
  61.     Object obj = ois.readObject();
  62.     if(obj instanceof Exception) {
  63.      print((Exception) obj);
  64.     }
  65.     ois.close();
  66.     bis.close();
  67.     System.out.println("**********************************");
  68.     System.out.println("");
  69.    }
  70.    rs.close();
  71.             prepStmt.close();
  72.    if(!found) {
  73.     System.out.println("Job Not found : " + jname);
  74.    }
  75.   } catch (SQLException ex) {
  76.    ex.printStackTrace();
  77.   } catch (IOException ex) {
  78.    ex.printStackTrace();
  79.   } finally {
  80.    closeConnection();
  81.         }
  82.  } 
  83.  private void print(Exception jdm) {
  84.    if(jdm != null)
  85.    System.out.println("Exception Message :"+jdm.getMessage());
  86.  }  
  87.  public SimpleDateFormat dateFormat(){
  88.   SimpleDateFormat simpleDate = new SimpleDateFormat("DDMMYY");  
  89.   return simpleDate;
  90.  } 
  91.  public static void main(String[] args) throws Exception{
  92.   new ReadJobHistroyDataDB().execute();
  93.  }
  94. }

No comments:

Post a Comment