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文件來裝載創建
e
g
這裡既然用到了外部文件
必然要將class文件放到oracle Server的某一目錄下邊
public class OracleJavaProc
{
public static void main(String[] argv)
{
System
out
println(
It
s a Java Oracle procedure
)
}
}
SQL> grant create any directory to scott;
授權成功
SQL> conn scott/tiger@iihero
oracledb
已連接
SQL> create or replace directory test_dir as
d:/oracle
;
目錄已創建
SQL> create or replace java class using bfile(test_dir
OracleJavaProc
CLASS
)
/
Java 已創建
SQL> create or replace procedure testjavaproc as language java name
OracleJavaProc
main(java
lang
String[])
;
/
過程已創建
SQL> call testjavaproc()
調用完成
SQL> execute testjavaproc;
PL/SQL 過程已成功完成
SQL> set serveroutput on size
SQL> call dbms_java
set_output(
)
調用完成
SQL> execute testjavaproc;
It
s a Java Oracle procedure
我推薦的一種方法直接使用loadjava命令遠程裝載並創建
先創建一個類
e
g
import java
sql
*;
import oracle
jdbc
*;
public class OracleJavaProc {
//Add a salgrade to the database
public static void addSalGrade(int grade
int losal
int hisal) {
System
out
println(
Creating new salgrade for EMPLOYEE…
)
try {
Connection conn =
DriverManager
getConnection(
jdbc:default:connection:
)
String sql =
INSERT INTO salgrade
+
(GRADE
LOSAL
HISAL)
+
VALUES(?
?
?)
;
PreparedStatement pstmt = conn
prepareStatement(sql)
pstmt
setInt(
grade)
pstmt
setInt(
losal)
pstmt
setInt(
hisal)
pstmt
executeUpdate()
pstmt
close()
}
catch(SQLException e) {
System
err
println(
ERROR! Adding Salgrade:
+ e
getMessage())
}
}
}
使用loadjava命令將其裝載到服務器端並編譯
D:eclipse
workspacedbtest>loadjava
u scott/tiger@iihero
oracledb
v
resolve Or
acleJavaProc
java
arguments:
u
scott/tiger@iihero
oracledb
v
resolve
OracleJavaProc
java
creating : source OracleJavaProc
loading : source OracleJavaProc
resolving: source OracleJavaProc
查詢一下狀態
連接到
Oracle
i 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
OracleJavaProc
addSalGrade(int
int
int)
;
/
過程已創建
SQL> set serveroutput on size
SQL> call dbms_java
set_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
e
g
修改OracleJavaProc類內容如下
import java
sql
*;
import oracle
jdbc
*;
public class OracleJavaProc {
// Add a salgrade to the database
public static void addSalGrade(int grade
int losal
int hisal) {
System
out
println(
Creating new salgrade for EMPLOYEE…
)
try {
Connection conn =
DriverManager
getConnection(
jdbc:default:connection:
)
String sql =
INSERT INTO salgrade
+
(GRADE
LOSAL
HISAL)
+
VALUES(?
?
?)
;
PreparedStatement pstmt = conn
prepareStatement(sql)
pstmt
setInt(
grade)
pstmt
setInt(
losal)
pstmt
setInt(
hisal)
pstmt
executeUpdate()
pstmt
close()
}
catch(SQLException e) {
System
err
println(
ERROR! Adding Salgrade:
+ e
getMessage())
}
}
public static int getHiSal(int grade)
{
try {
Connection conn =
DriverManager
getConnection(
jdbc:default:connection:
)
String sql =
SELECT hisal FROM salgrade WHERE grade = ?
;
PreparedStatement pstmt = conn
prepareStatement(sql)
pstmt
setInt(
grade)
ResultSet rset = pstmt
executeQuery()
int res =
;
if (rset
next())
{
res = rset
getInt(
)
}
rset
close()
return res;
}
catch (SQLException e)
{
System
err
println(
ERROR! Querying Salgrade:
+ e
getMessage())
return
;
}
}
}
如何更新呢?
D:eclipse
workspacedbtest>dropjava
u scott
v OracleJavaProc
D:/tiger@iihero
oracledbeclipse
workspacedbtest>loadjava
u scott
v
resolve Or
acleJavaProc/tiger@iihero
oracledb
java
arguments:
u
scott/tiger@iihero
oracledb
v
resolve
OracleJavaProc
java
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
OracleJavaProc
getHiSal(int) return int
;
/
函數已創建
SQL> set serveroutput on size
SQL> call dbms_java
set_output(
)
調用完成
SQL> select query_hisal(
) from dual;
QUERY_HISAL(
)
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25916.html