0

我在 Oracle 中为我的项目使用了一个名为 LogMiner 的工具。我在 Windows 7 32 位机器上使用 Oracle 10g。

为了启动日志挖掘工具,我登录sqlplus并执行以下查询:

 //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;

这个 PL/SQL 查询使用 sqlplus 运行良好,但现在我想在 Java 中使用 jdbc 运行同样的查询

我为它编写了以下代码,如下所示:

    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();
              }

               }

         });

     }

    }

我应该如何在 Java 中编写相同的查询才能正确执行?有人可以告诉我吗?

4

1 回答 1

3

您的代码有几个问题:

  1. executeUpdate()一次调用不能运行多个语句
  2. executeUpdate()永远不会返回结果集,您需要使用execute()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.
于 2012-06-01T06:48:19.633 回答