如何使用 Java 存储过程显示表数据?
目前,我正在 oracle db
上测试 JAVA
中的一些存储过程
,因此我尝试显示所有 emp 实体。
所以我的问题是如何通过java存储过程显示整个表?
这就是我尝试过的:
create or replace and compile java source named getEMP
as
import java.sql.*;
import java.util.*;
public class Example{
public static void showEmp() throws SQLException, ClassNotFoundException {
ResultSet rs;
Properties p = new Properties();
p.put("user", "user");
p.put("password","password");
String strCon = "Connection String";
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(strCon, p);
Statement stmt;
con = DriverManager.getConnection(strCon, p);
String query =
"select empno, ename, deptno, sal, comm " +
"from emp";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
//Display the ResultSet
}
}
我在此方法中的问题是我无法使用 System.out.println("");
。此后它只显示任何内容:
create or replace procedure showEmp
as language java name 'Example.showEmp()';
exec showEmp;
提前致谢!
Currently, I'm testing some stored procedures
in JAVA
on oracle db
, so I tried to display all the emp entities.
So my question is how can I display a whole table throught a java stored procedure?
This is what i tried:
create or replace and compile java source named getEMP
as
import java.sql.*;
import java.util.*;
public class Example{
public static void showEmp() throws SQLException, ClassNotFoundException {
ResultSet rs;
Properties p = new Properties();
p.put("user", "user");
p.put("password","password");
String strCon = "Connection String";
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(strCon, p);
Statement stmt;
con = DriverManager.getConnection(strCon, p);
String query =
"select empno, ename, deptno, sal, comm " +
"from emp";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
//Display the ResultSet
}
}
My problem in this methode is that i can't use System.out.println("");
. It just displays nothing after this:
create or replace procedure showEmp
as language java name 'Example.showEmp()';
exec showEmp;
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保启用服务器输出并将 java 输出重定向到它。在 SQL*PLus 中,您可以使用以下两条语句来完成此操作:
数字 20000 是最大缓冲区大小,与 DBMS_OUTPUT 中的相同。
此后,如果运行
exec showEmp;
,您应该能够看到打印出System.out.println()
调用的结果。You need to make sure that server output is enabled and also to redirect java output to it. In SQL*PLus you can accomplish that with the following two statements:
The number 20000 is the maximum buffer size, same as in
DBMS_OUTPUT
.After this if you run
exec showEmp;
you should be able to see the results of aSystem.out.println()
call being printed out.