java - java从oracle插入语句中获取结果
问题描述
我有一个小而简单的 gui,用户可以在其中粘贴一些 oracle 插入 SQL 语句并将它们提交以插入数据库。
我的问题是,如何检索结果状态和/或操作的 oracle 输出并将其显示在 GUI 上?例如“插入 X 行”或错误(例如异常堆栈跟踪)以防失败?
我的 2 个相关方法如下 - 第一个方法从 GUI 获取 SQL 命令并调用第二个方法来运行该语句:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String SQL = jEditorPane1.getText();
jEditorPane1.setText("");
String[] arraySQL = SQL.split(System.lineSeparator());
for (String s : arraySQL) {
System.out.println(s);
}
executeSQL(arraySQL);
}
private void executeSQL(String[] commands) {
BasicDataSource dataSource = DatabaseUtility.getDataSource();
try (Connection connection = dataSource.getConnection()) {
for (String sql : commands) {
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.execute();
}
}
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
解决方案
我的开源程序PLSQL_LEXER或许可以帮助您完成任务。
听起来您正在尝试设置较小版本的 SQL Fiddle,这是一项艰巨的任务。尝试拆分、分类和运行 SQL 语句时存在大量奇怪的解析问题。我无法在 Java 方面为您提供帮助,但如果您愿意在 PL/SQL 中完成大部分繁重的工作,那么下面的代码应该会有所帮助。
如果安装包,然后在下面创建存储函数,您可以简单地将字符串 (CLOB) 传递给 Oracle,然后返回结果。
该代码还显示了如何防止某些类型的命令运行。但这只是为了方便,如果想警告人们不要运行某些东西。您不想在此处尝试重新实现 Oracle 安全性。您应该假设向该函数提交命令的任何人都具有与拥有该函数的用户相同的权限。
create or replace function run_commands(p_statements clob) return clob
as
v_split_statements token_table_table;
v_category varchar2(100);
v_statement_type varchar2(100);
v_command_name varchar2(64);
v_command_type number;
v_lex_sqlcode number;
v_lex_sqlerrm varchar2(4000);
v_output clob;
begin
--Tokenize and split the string into multiple statements.
v_split_statements := statement_splitter.split_by_semicolon(
plsql_lexer.lex(p_statements));
--Loop through the statements.
for i in 1 .. v_split_statements.count loop
--Classify each statement.
statement_classifier.classify(
p_tokens => v_split_statements(i),
p_category => v_category,
p_statement_type => v_statement_type,
p_command_name => v_command_name,
p_command_type => v_command_type,
p_lex_sqlcode => v_lex_sqlcode,
p_lex_sqlerrm => v_lex_sqlerrm
);
--For debugging, print the statement and COMMAND_NAME.
v_output := v_output||chr(10)||'Statement '||i||' : '||
replace(replace(
plsql_lexer.concatenate(v_split_statements(i))
,chr(10)), chr(9));
v_output := v_output||chr(10)||'Command Name: '||v_command_name;
--Handle different command types.
--
--Prevent Anonymous Blocks from running.
if v_command_name = 'PL/SQL EXECUTE' then
v_output := v_output||chr(10)||'Error : Anonymous PL/SQL blocks not allowed.';
--Warning message if "Invalid" - probably a typo.
elsif v_command_name = 'Invalid' then
v_output := v_output||chr(10)||'Warning : Could not classify this statement, '||
'please check for a typo: '||
replace(replace(substr(
plsql_lexer.concatenate(v_split_statements(i))
, 1, 30), chr(10)), chr(9));
--Warning message if "Nothing"
elsif v_command_name = 'Nothing' then
v_output := v_output||chr(10)||'No statements found.';
--Run everything else.
else
declare
v_success_message varchar2(4000);
v_compile_warning_message varchar2(4000);
begin
--Remove extra semicolons and run.
execute immediate to_clob(plsql_lexer.concatenate(
statement_terminator.remove_semicolon(
p_tokens => v_split_statements(i))));
--Get the feedback message.
statement_feedback.get_feedback_message(
p_tokens => v_split_statements(i),
p_rowcount => sql%rowcount,
p_success_message => v_success_message,
p_compile_warning_message => v_compile_warning_message
);
--Print success message.
v_output := v_output||chr(10)||'Status : '||v_success_message;
--Print compile warning message, if any.
--This happens when objects successfully compile but are invalid.
if v_compile_warning_message is not null then
v_output := v_output||chr(10)||'Compile warning: '||v_compile_warning_message;
end if;
exception when others then
v_output := v_output||chr(10)||'Error : '||dbms_utility.format_error_stack||
dbms_utility.format_error_backtrace;
end;
end if;
end loop;
return v_output;
end;
/
下面是运行存储过程和输出的示例。您必须将此 PL/SQL 块转换为 Java 调用,但我认为这不会太复杂。
declare
--A collection of statements separated by semicolons.
--These may come from a website, text file, etc.
v_statements clob := q'<
create table my_table(a number);
insert into my_table values(1);
begin null; end;
udpate my_table set a = 2;
>';
v_results clob;
begin
v_results := run_commands(v_statements);
dbms_output.put_line(v_results);
end;
/
Statement 1 : create table my_table(a number);
Command Name: CREATE TABLE
Status : Table created.
Statement 2 : insert into my_table values(1);
Command Name: INSERT
Status : 1 row created.
Statement 3 : begin null; end;
Command Name: PL/SQL EXECUTE
Error : Anonymous PL/SQL blocks not allowed.
Statement 4 : udpate my_table set a = 2;
Command Name: Invalid
Warning : Could not classify this statement, please check for a typo: udpate my_table set a = 2;
推荐阅读
- pointers - 设置指针类型的结构字段
- email - 如何通过 Digital Ocean 上的外部 SMTP 服务发送电子邮件?
- javascript - 无法保存,因为列中有值(不允许使用“WR#”(它不在合法值列表中)
- java - Struts2 和 PI4J
- java - 我如何实现从 API 下载文件
- c# - 一旦页面加载,asp.net页面上的加载图标就会消失
- python - boolean.conjugate() 有什么用?
- vba - MSXML2.ServerXMLHTTP.6.0 PATCH 请求的 Open 方法存在问题
- linux - /proc/ 中的 VmLck(锁定内存)与 VmPin(固定内存)
/地位 - sqf - 使用 SQF 在 Arma 3 中编辑虚拟军械库