简体   繁体   中英

How to execute log miner PL/SQL query for oracle 10g using jdbc in java

I am using a tool called LogMiner in Oracle for my project. I am using Oracle 10g on a Windows 7 32 bit machine.

In order to start the log miner tool I log on into sqlplus and execute the following query:

 //Query to create flat file

 alter system set utl_file_dir='C:\oracle\product\10.2.0\logminer_dir' scope=spfile;
 shutdown immediate
 startup
 show parameter utl_file_dir
 SELECT SUPPLEMENTAL_LOG_DATA_MIN FROM V$DATABASE;    
 ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;

 SELECT SUPPLEMENTAL_LOG_DATA_MIN FROM V$DATABASE;
 alter system switch logfile;

This PL/SQL query runs fine using sqlplus but now I want to run the same using jdbc in Java

I have written the following code for it as follows:

    package src;
    import java.awt.event.ActionEvent;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;

    public class LogMiner extends javax.swing.JFrame {
      private JLabel jLabel1;
      private JButton jButton1;
      private JButton jButton4;
      private JButton jButton3;
      private JButton jButton2;


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            LogMiner inst = new LogMiner();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public LogMiner() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);
        {
            jLabel1 = new JLabel();
            getContentPane().add(jLabel1);
            jLabel1.setText("LogMiner Tool");
            jLabel1.setBounds(236, 18, 97, 21);
        }
        {
            jButton1 = new JButton();
            getContentPane().add(jButton1);
            jButton1.setText("Create the flat file");
            jButton1.setBounds(212, 71, 133, 28);
        }
        pack();
        setSize(600, 400);
    } catch (Exception e) {

        e.printStackTrace();
    }




 jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {

        //write query statement
            try
              {
               // load oracle driver
              Class.forName("oracle.jdbc.driver.OracleDriver");
              // connect using Thin driver
              Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","oracle");
              System.out.println("Connected Successfully To Oracle");
                      //getting errors in the following line...need help here
              String sql="alter system set                              
                     utl_file_dir='C:\oracle\product\10.2.0\logminer_dir'scope=spfile;"
                     +"shutdown immediate"+
                     "startup"+
                     "show parameter utl_file_dir"+
                     "SELECT SUPPLEMENTAL_LOG_DATA_MIN FROM V$DATABASE"+
                     "alter system switch logfile";


                      Statement statement = con.createStatement();
              ResultSet resultset = statement.executeUpdate(sql);  

           resultset.next();
           String s = resultset.getString(1);      
           System.out.println(s);
          statement.close();
              con.close();
              }
              catch(Exception ex)
              {
                ex.printStackTrace();
              }

               }

         });

     }

    }

How should I write the same query in Java for proper execution? Can someone please tell me ?

There are several problems with your code:

  1. you cannot run more than one statement with a single executeUpdate() call
  2. executeUpdate() will never return a result set, you need to use execute() or executeQuery()
  3. shutdown and startup are SQL*Plus commands and cannot be executed through JDBC. Since 11.1 there is however an extension in Oracle's JDBC driver that will allow you to do so: http://docs.oracle.com/cd/E11882_01/java.112/e16548/dbmgmnt.htm#CHDJABJI
    However I don't know if this will also work with Oracle 10 (which by the way is de-supported if I'm not mistaken, you should upgrade to 11.x anyway)
  4. show parameter is a SQL*Plus command that you cannot run from JDBC. You need to run a select * from v$parameter instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM