问题
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!
回答1:
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:
SQL> set serveroutput on;
SQL> call dbms_java.set_output(20000);
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 a System.out.println()
call being printed out.
来源:https://stackoverflow.com/questions/9148008/how-can-i-display-tabledata-with-java-stored-procedure