如何使用Java存储过程显示表数据? [英] How can I display tabledata with Java Stored Procedure?

查看:169
本文介绍了如何使用Java存储过程显示表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在oracle db上的JAVA中测试一些stored procedures,因此我尝试显示所有emp实体.

Currently, I'm testing some stored procedures in JAVA on oracle db, so I tried to display all the emp entities.

所以我的问题是如何通过Java存储过程显示整个表?

So my question is how can I display a whole table throught a java stored procedure?

这是我尝试过的:

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("");.在此之后它什么也不显示:

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;

提前谢谢!

推荐答案

您需要确保启用服务器输出,并将Java输出重定向到它.在SQL * PLus中,您可以使用以下两个语句来完成该操作:

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

数字20000是最大缓冲区大小,与DBMS_OUTPUT中的一样.

The number 20000 is the maximum buffer size, same as in DBMS_OUTPUT.

此后,如果您运行exec showEmp;,则应该能够看到打印出System.out.println()呼叫的结果.

After this if you run exec showEmp; you should be able to see the results of a System.out.println() call being printed out.

这篇关于如何使用Java存储过程显示表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆