在 java 中捕获 sql 开发者脚本输出

capture sql developer script output in java

作为我的 java 应用程序的一部分,我必须从 java 代码创建 oracle 包。有时,包代码可能有问题,编译可能会失败。但是,我无法从 java 捕获简单的故障。所以,我必须从 java 编译 > 350 个 oracle 包,如果包中有错误,我需要通知用户修复它们。我在下面粘贴了 oracle 包和 java 代码。

CREATE OR REPLACE PACKAGE plat_test IS
    FUNCTION getmsg (
        p_empno IN NUMBER
    ) RETURN VARCHAR2;

END plat_test;
/

CREATE OR REPLACE PACKAGE BODY plat_test AS
    FUNCTION getmsg (
        p_empno IN NUMBER
    ) RETURN VARCHAR2 IS
    BEG
        RETURN 'sss';
    END getmsg;

END plat_teest;

编译/运行 sql 开发人员抛出上述代码:

LINE/COL  ERROR
--------- -------------------------------------------------------------
0/0       PL/SQL: Compilation unit analysis terminated
1/14      PLS-00201: identifier 'PLAT_TEEST' must be declared
1/14      PLS-00304: cannot compile body of 'PLAT_TEEST' without its specification
Errors: check compiler log

我想在java中创建上面的包并得到结果。这样我就可以通知用户失败。在 java 中,我无法捕获错误,但程序总是成功。

如何捕获 java

中的输出

我的java代码:

import java.sql.*;

public class NewJDBCTester {
    public static void one() {

        String s_sql = "CREATE OR REPLACE PACKAGE BODY plat_test AS\n" +
                "    FUNCTION getmsg (\n" +
                "        p_empno IN NUMBER\n" +
                "    ) RETURN VARCHAR IS\n" +
                "    BEG" +
                "        RETURN 'ret_val';\n" +
                "    END getmsg;\n" +
                "\n" +
                "END plat_test\n" +
                "/";

//        String s_sql ="alter table Metric_idf from ssssssss_ssst";
//        System.out.println(" SQL Stmt: " + sql);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:bhasoor/password@10.100.1.61:34171/ssssssssssdb");
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            stmt.execute (s_sql);
            System.out.println(" SQL Executed Successfully ");
        } catch (SQLException sqe) {
            System.out.println("Error Code = " + sqe.getErrorCode());
//            sqe.
            System.out.println("SQL state = " + sqe.getSQLState());
            System.out.println("Message = " + sqe.getMessage());
            System.out.println("printTrace /n");
            sqe.printStackTrace();

        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        one();

    }
}

你可以通过这种方式知道是否出错:

boolean result = stmt.execute(s_sql);
System.out.println(result ? " SQL Executed Successfully " : " SQL Executed with error ");

然后,下面的查询给你错误:

select * 
from ALL_ERRORS  
where owner = 'METRICSTREAM' 
and name = 'PLAT_TEST' 
--and type = 'PACKAGE BODY' 
order by sequence

这里没有SQLException因为编译已经完成,但是有一些错误

您应该像这样使用 try-with-resource 以避免内存泄漏:

try ( //
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.100.1.61:34171/pdb", "metricstream", "password"); //
        Statement stmt = conn.createStatement(); //
) {
    boolean result = stmt.execute(s_sql);
    System.out.println(result ? " SQL Executed Successfully " : " SQL Executed with error ");
}
catch (SQLException sqe) {
    System.out.println("Error Code = " + sqe.getErrorCode());
    System.out.println("SQL state = " + sqe.getSQLState());
    System.out.println("Message = " + sqe.getMessage());
    System.out.println("printTrace /n");
    sqe.printStackTrace();
}

你可以看看这个几年前的博客。 您可以像在命令行上一样,将 Oracle SQLDeveloper 或 Oracle SQLcl 中的库用于 运行 脚本。 https://barrymcgillin.blogspot.com/2018/04/run-your-sql-script-from-java-now.html

在您的脚本中,您可以执行简单的 sqlplus 操作,例如

Begin
 my stuff;
end;
/

show errors

您还可以从执行器对象中获取错误 运行。

所以最后我这样实现了。执行创建或替换包然后从 dba_errors...

中获取错误

在此处发布代码...

import java.sql.*;

public class NewJDBCTester {
    public static void one() throws ClassNotFoundException {

        String s_sql = "CREATE OR REPLACE PACKAGE BODY plat_test AS\n" +
                "    FUNCTION getmsg (\n" +
                "        p_empno IN NUMBER\n" +
                "    ) RETURN VARCHAR2 IS\n" +
                "    BEGIN\n" +
//                "       RETURN 'sss';\n" +
                "    END getmsg;\n" +
                "END plat_test;\n";

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:user/password@10.100.1.61:34171/somedb");
            stmt = conn.createStatement();
            System.out.println("EXECUTING QUERY");
            rset = stmt.executeQuery(s_sql);
            rset.close();
            stmt.close();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("SELECT line, text FROM DBA_ERRORS   WHERE OWNER = 'Bhasoor'  AND NAME = 'PLAT_TEST' ORDER BY SEQUENCE desc");
            while (rset.next()) {
                System.out.println("ERROR AT LINE ::: " + rset.getString("line"));
                System.out.println("\n");
                System.out.println("ERROR DETAILS::: " + rset.getString("text"));
            }
        } catch (SQLException sqe) {
            System.out.println("Error Code = " + sqe.getErrorCode());
            System.out.println("SQL state = " + sqe.getSQLState());
            System.out.println("Message = " + sqe.getMessage());
            System.out.println("printTrace \n");
            sqe.printStackTrace();

        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        one();
    }
}