简体   繁体   English

Oracle Java存储过程命令行交互

[英]Oracle Java Stored Procedure Command-line Interaction

I have a funny requirement. 我有一个有趣的要求。

We need to have a command-line interaction in a Java Stored Procedure. 我们需要在Java存储过程中进行命令行交互。 In spite of granting appropriate permissions using the dbms_java.grant_permission commands, I am encountering java.io.IOException , where I read from System.in using java.io.InputStreamReader . 尽管使用dbms_java.grant_permission命令授予了适当的权限,但我还是遇到了java.io.IOException ,在这里我使用java.io.InputStreamReaderSystem.in读取了内容。

Where is the problem? 问题出在哪儿?

The Java Source is here: Java源代码在这里:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;


import oracle.jdbc.driver.OracleDriver;

public class ExecuteInteractiveBatch {

    public static void aFunction() {

        Connection connection = null;
        try {
            int rowFetched = 0;

            connection = new OracleDriver().defaultConnection();

            Statement stmt = connection.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT count(1) cnt from sometable where c = 2");

            int count = 0;
            if (rs.next()) {
                count = rs.getInt(1);
            }
            rs.close();
            stmt.close();
            rs = null;
            stmt = null;
            if (count == 1) {
                System.out.println("Do you want to continue?");
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                String response = reader.readLine();
                if ("Y".equalsIgnoreCase(response)) {
                    stmt = connection.createStatement();
                    int rowsAffected = stmt.executeUpdate("DELETE from sometable where c=2");
                    System.out.println("" + rowsAffected + " row(s) deleted");
                    stmt.close();
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {

            try {
                if (connection != null || !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}

You can't perform I/O to the console from within Oracle... not in java, not in PL/SQL, nowhere. 您无法从Oracle内部执行对控制台的I / O操作……不在Java中,在PL / SQL中,无处不在。 Oracle is running in a separate process from the user, quite likely even a different computer, and java stored procedures are being run in that process. Oracle在与用户不同的进程中运行,甚至可能在不同的计算机上运行,​​并且Java存储过程正在该进程中运行。 You will need some java code running on the client system to perform console I/O. 您将需要在客户端系统上运行一些Java代码以执行控制台I / O。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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