gpt4 book ai didi

java - 在 java 中捕获 sql 开发人员脚本输出

转载 作者:行者123 更新时间:2023-12-04 14:55:03 26 4
gpt4 key购买 nike

作为我的 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 developer 中编译/运行以上代码抛出:

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

关于java - 在 java 中捕获 sql 开发人员脚本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68220524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com