Java Code to Read History Data of Job Using Database

package com.NeedEvaluate;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class ReadJobHistroyDataDB {
 private static Connection connectionObject = null;
 private static Connection makeConnection() {

  try {
   Class.forName("oracle.jdbc.OracleDriver");           
   connectionObject = DriverManager.getConnection("jdbc:oracle:thin:@host:5521:oimdb", "SCHEMA", "PASSWORD");
  } catch (SQLException se) {
     se.printStackTrace();
  } catch (Exception e) {
   
   e.printStackTrace();
  }
  return connectionObject;
 }

 public static synchronized Connection getConnection() {
  if (connectionObject == null) {
   connectionObject = makeConnection();
  }
  return connectionObject;

 }

 public static void closeConnection() {
             System.out.println("Closing  to database connection ...");
  try {   
   if (connectionObject != null)
    connectionObject.close();
  } catch (SQLException se) {
   se.printStackTrace();
  }
 }
 public void execute() throws Exception {
  String mname = "execute()";
  String jname = "Issue Audit Messages Task";//Job Name  which is having issue.
  jname=jname.replaceAll("\\*", "%");
  if (null == jname)
   throw new NullPointerException("JobName is null.");
  jname = jname.replace('*', '%');
  String query = "select ERROR_DATA,job_name from JOB_HISTORY where id=7747";
  try {
            PreparedStatement prepStmt = getConnection().prepareStatement(query);
           
   ResultSet rs = prepStmt.executeQuery(query);
   
   boolean found = false;
   while(rs.next()) {
    System.out.println("");
    System.out.println("*****************"+rs.getString("job_name")+"*****************");
    found = true;
    Blob b = rs.getBlob("ERROR_DATA");
    if(b==null)
     continue;
    InputStream bis = b.getBinaryStream();
    ObjectInputStream ois = new ObjectInputStream(bis);
    Object obj = ois.readObject();
    if(obj instanceof Exception) {
     print((Exception) obj);
    }
    ois.close();
    bis.close();
    System.out.println("**********************************");
    System.out.println("");
   }
   rs.close();
            prepStmt.close();
   if(!found) {
    System.out.println("Job Not found : " + jname);
   }
  } catch (SQLException ex) {
   ex.printStackTrace();
  } catch (IOException ex) {
   ex.printStackTrace();
  } finally {
   closeConnection();
        }
 }
 
 private void print(Exception jdm) {
   if(jdm != null)
   System.out.println("Exception Message :"+jdm.getMessage());
 } 
 
 public SimpleDateFormat dateFormat(){
  SimpleDateFormat simpleDate = new SimpleDateFormat("DDMMYY");  
  return simpleDate;
 }
 
 public static void main(String[] args) throws Exception{
  new ReadJobHistroyDataDB().execute();
 }
}

No comments:

Post a Comment

About OIM

Oracle Identity Management enables organizations to effectively manage the end - to - end life - cycle of user ide...

Popular Posts