时间:2021-07-01 10:21:17 帮助过:168人阅读
通常有三种方法来创建java存储过程。 一、手动编写Java存储过程/函数 利用SQL脚本代码,手动编写可供Oracle调用的Java存储过程或函数,和普通的存储过程/函数创建方法类,以存储过程为例,语法如下: CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED java_
通常有三种方法来创建java存储过程。
利用SQL脚本代码,手动编写可供Oracle调用的Java存储过程或函数,和普通的存储过程/函数创建方法类似,以存储过程为例,语法如下:
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED java_soure_name
AS
示例:创建一个简单的Hello World输出过程,调用JavaSource实现
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED OracleJavaProc AS public class OracleJavaProc { public static void main(String[] args) { System.out.println("Hello World!"); } }
2. 创建存储过程 ,调用Java Source
CREATE OR REPLACE PROCEDURE testoraclejava AS LANGUAGE JAVA NAME 'OracleJavaProc.main(java.lang.String [])';
begin dbms_java.set_output(2000); ---设置Java输出缓冲区大小,否则无法输出数据 testoraclejava; ---调用存储过程 end;输出结果:Hello World!
将外部编译好的Java类文件(*.class)通过命令导入到数据库服务器中,实现Java存储过程的创建。
创建目录和Java Source
SQL> create or replace directory test_dir as 'd:/oracle'; 目录已创建。 SQL> create or replace java class using bfile(test_dir, 'OracleJavaProc.class') 2 / Java 已创建。
解决办法:用$ORACLE_HOME/jdk/bin/javac 重新编译java文件。
import java.sql.*; import oracle.jdbc.*; public class OracleJavaProc { //Insert a record to the database. public static void insertRecord(int id, String name, int age ,String xman_id) { System.out.println("Inserting new records for EMP..."); try { Connection conn = DriverManager.getConnection("jdbc:default:connection:"); String sql = "INSERT INTO emp " + "(ID,NAME,SALARY,XMAN_ID) " + "VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1,id); pstmt.setString(2,name); pstmt.setInt(3,age); pstmt.setString(4,xman_id); pstmt.executeUpdate(); pstmt.close(); } catch(SQLException e) { System.err.println("ERROR! Inserting record: " + e.getMessage()); } } //Select a reords from database public static boolean recordQuery(int id) { System.out.println("Querying records from EMP..."); try { Connection conn = DriverManager.getConnection("jdbc:default:connection:"); String sql = "SELECT * FROM emp WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1,id); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+ rs.getInt(3)+"\t"+rs.getString(4)); } return true; } catch (SQLException e) { System.err.println("ERROR! Querying record: " + e.getMessage()); return false; } } }
D:\Oracle>loadjava -u hbi/hbi@orcl -v -resolve OracleJavaProc.java
arguments: '-u' 'hbi/hbi@orcl' '-v' '-resolve' 'OracleJavaProc.java'
creating : source OracleJavaProc
loading : source OracleJavaProc
resolving: source OracleJavaProc
Classes Loaded: 0
Resources Loaded: 0
Sources Loaded: 1
Published Interfaces: 0
Classes generated: 0
Classes skipped: 0
Synonyms Created: 0
Errors: 0
创建存储过程,实现数据插入:
create or replace procedure INSERT_RECORD(V_ID number, V_NAME varchar2, V_SALARY number, V_XMAN_ID varchar2) as language java name 'OracleJavaProc.insertRecord(int,java.lang.String,int,java.lang.String)';创建函数,实现数据查询:
create or replace function QUERY_TABLE(v_id number) return number as language java name 'OracleJavaProc.recordQuery(int) return int';
SQL> set serveroutput on size 2000 SQL> call dbms_java.set_output(2000); 调用完成。 SQL> execute add_salgrade(29, 'Charles', 5000, '029'); Inserting new records for EMP... PL/SQL 过程已成功完成。
修改好Java源代码后,先dropjava ,再loadjava。
D:\Oracle>dropjava -u hbi/hbi@orcl -v OracleJavaProc
dropping: source OracleJavaProc
D:\Oracle>loadjava -u hbi/hbi@orcl -v -resolve OracleJavaProc.java
arguments: '-u' 'hbi/hbi@orcl' '-v' '-resolve' 'OracleJavaProc.java'
creating : source OracleJavaProc
loading : source OracleJavaProc
resolving: source OracleJavaProc
Classes Loaded: 0
Resources Loaded: 0
Sources Loaded: 1
Published Interfaces: 0
Classes generated: 0
Classes skipped: 0
Synonyms Created: 0
Errors: 0