数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

在Oracle中使用Java存储过程


发布日期:2022年04月08日
 
在Oracle中使用Java存储过程

Java存储过程今后在各大数据库厂商中越来越流行功能也越来越强大这里以Oracle为例介绍一下java存储过程的具体用法

如何创建java存储过程?

通常有三种方法来创建java存储过程

使用oracle的sql语句来创建

eg 使用create or replace and compile java source named <name> as

后边跟上java源程序要求类的方法必须是public static的才能用于存储过程

SQL> create or replace and compile java source named javademo

as

import javasql*;

public class JavaDemo

{

public static void main(String[] argv)

{

Systemoutprintln(hello java demo

}

}

/

Java 已创建

SQL> show errors java source javademo

没有错误

SQL> create or replace procedure javademo

as

language java name JavaDemomain(javalangString[]);

/

过程已创建

SQL> set serveroutput on

SQL> call javademo()

调用完成

SQL> call dbms_javaset_output(

调用完成

SQL> call javademo()

hello java demo

调用完成

SQL> call javademo()

hello java demo

调用完成

使用外部class文件来装载创建

eg 这里既然用到了外部文件必然要将class文件放到oracle Server的某一目录下边

public class OracleJavaProc

{

public static void main(String[] argv)

{

Systemoutprintln(Its a Java Oracle procedure

}

}

SQL> grant create any directory to scott;

授权成功

SQL> conn scott/tiger@iiherooracledb

已连接

SQL> create or replace directory test_dir as d:/oracle;

目录已创建

SQL> create or replace java class using bfile(test_dir OracleJavaProcCLASS

/

Java 已创建

SQL> create or replace procedure testjavaproc as language java name OracleJavaProcmain(javalangString[]);

/

过程已创建

SQL> call testjavaproc()

调用完成

SQL> execute testjavaproc;

PL/SQL 过程已成功完成

SQL> set serveroutput on size

SQL> call dbms_javaset_output(

调用完成

SQL> execute testjavaproc;

Its a Java Oracle procedure

我推荐的一种方法直接使用loadjava命令远程装载并创建

先创建一个类 eg

import javasql*;

import oraclejdbc*;

public class OracleJavaProc {

//Add a salgrade to the database

public static void addSalGrade(int grade int losal int hisal) {

Systemoutprintln(Creating new salgrade for EMPLOYEE…

try {

Connection conn =

DriverManagergetConnection(jdbc:default:connection:

String sql =

INSERT INTO salgrade +

(GRADELOSALHISAL) +

VALUES(??);

PreparedStatement pstmt = connprepareStatement(sql)

pstmtsetInt(grade)

pstmtsetInt(losal)

pstmtsetInt(hisal)

pstmtexecuteUpdate()

pstmtclose()

}

catch(SQLException e) {

Systemerrprintln(ERROR! Adding Salgrade:

+ egetMessage())

}

}

}

使用loadjava命令将其装载到服务器端并编译

D:eclipseworkspacedbtest>loadjava u scott/tiger@iiherooracledb v resolve Or

acleJavaProcjava

arguments: u scott/tiger@iiherooracledb v resolve OracleJavaProcjava

creating : source OracleJavaProc

loading : source OracleJavaProc

resolving: source OracleJavaProc

查询一下状态

连接到

Oraclei Enterprise Edition Release Production

With the Partitioning OLAP and Oracle Data Mining options

JServer Release Production

SQL> SELECT object_name object_type status FROM user_objects WHERE object_type LIKE JAVA%;

OBJECT_NAME

OBJECT_TYPE STATUS

OracleJavaProc

JAVA CLASS VALID

OracleJavaProc

JAVA SOURCE VALID

测试一下存储过程

SQL> create or replace procedure add_salgrade(id number losal number hisal num

ber) as language java name OracleJavaProcaddSalGrade(int int int);

/

过程已创建

SQL> set serveroutput on size

SQL> call dbms_javaset_output(

调用完成

SQL> execute add_salgrade(

Creating new salgrade for EMPLOYEE…

PL/SQL 过程已成功完成

SQL> select * from salgrade where grade=;

GRADE LOSAL HISAL

如何更新你已经编写的java存储过程?

假如要往类OracleJavaProc里添加一个存储过程方法如何开发?

正确的步骤应该是先dropjava 改程序再loadjava

eg修改OracleJavaProc类内容如下

import javasql*;

import oraclejdbc*;

public class OracleJavaProc {

// Add a salgrade to the database

public static void addSalGrade(int grade int losal int hisal) {

Systemoutprintln(Creating new salgrade for EMPLOYEE…

try {

Connection conn =

DriverManagergetConnection(jdbc:default:connection:

String sql =

INSERT INTO salgrade +

(GRADELOSALHISAL) +

VALUES(??);

PreparedStatement pstmt = connprepareStatement(sql)

pstmtsetInt(grade)

pstmtsetInt(losal)

pstmtsetInt(hisal)

pstmtexecuteUpdate()

pstmtclose()

}

catch(SQLException e) {

Systemerrprintln(ERROR! Adding Salgrade:

+ egetMessage())

}

}

public static int getHiSal(int grade)

{

try {

Connection conn =

DriverManagergetConnection(jdbc:default:connection:

String sql = SELECT hisal FROM salgrade WHERE grade = ?;

PreparedStatement pstmt = connprepareStatement(sql)pstmtsetInt( grade)

ResultSet rset = pstmtexecuteQuery()

int res = ;

if (rsetnext())

{

res = rsetgetInt(

}

rsetclose()

return res;

}

catch (SQLException e)

{

Systemerrprintln(ERROR! Querying Salgrade:

+ egetMessage())

return ;

}

}

}

如何更新呢?

D:eclipseworkspacedbtest>dropjava u scott v OracleJavaProc

D:/tiger@iiherooracledbeclipseworkspacedbtest>loadjava u scott v resolve Or

acleJavaProc/tiger@iiherooracledbjava

arguments: u scott/tiger@iiherooracledb v resolve OracleJavaProcjava

creating : source OracleJavaProc

loading : source OracleJavaProc

resolving: source OracleJavaProc

后边的应用示例

SQL> create or replace function query_hisal(grade number) return number as langu

age java name OracleJavaProcgetHiSal(int) return int;

/

函数已创建

SQL> set serveroutput on size

SQL> call dbms_javaset_output(

调用完成

SQL> select query_hisal() from dual;

QUERY_HISAL(

               

上一篇:在LINUX环境中用PHP连接Oracle数据库

下一篇:Spring中使用JDBC