Oracle Java Procedure
--------------------------------------------------------------------
目地: Oracle Database 中使用Java 開發過程,將MySQL中的數據存放到Oracle中來
測試環境說明:
操作平台: Debian Linux(Testing)
MySQL: 5.0 (mysqlsrv)
Oracle Database : 10.2.0.1.0 (oraclehost)
-----------------------------------------------------------------------------------------
一般情況下 ,Java執行環境已經由DBA配置好.
Java Procedure程序員只需要第7,8,9步
1 .在Mysql中授權Oracle主機可以訪問
a. 在mysqlsrv主機 /user, database, pass 可視需要改為合適的選擇
grant all privileges on mysql.* to user1@ymdbsrv identified by 'user1pass'
flush privileges;
2 .在oraclehost 測試可以用mysql工具訪問mysql database
$mysql -h mysqlsrv -u user1 --password=user1pass mysql
如能正確連接表明授權成功
3. 檢查Oracle中java組件是否正常可用.
4. 裝入mysql jdbc Driver到Oracle java環境.
#loadjava -v /home/oracle/mysql-3.2.0.jar
loadjava -user user/pass@db -v -genmissing mysql-3.2.0.jar
5. java權限設置 默認情況下,java訪問網絡,文件系統等都是受限制的
以下代碼設置該java可訪問mysqlsrv主機
exec dbms_java.grant_permission('PUBLIC','java.net.SocketPermission','mysqlsrv','connect,resolve');
6. 建立測試Table
drop table test
/
create table test
(uname varchar2(80) not null,
mail varchar2(80) ) ;
7. 編寫java procedure
1 create or replace java source named "Loaduser" as
2 import java.sql.*;
3 import java.io.*;
4 import com.mysql.jdbc.*;
5 import oracle.jdbc.*;
6 public class Loaduser{
7 public static int getuser() {
8 try {
9 Class.forName("com.mysql.jdbc.Driver").newInstance();
10 java.sql.Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://mysqlsrv/database","user","pass");
11 java.sql.Statement stmt=con.createStatement();
12 java.sql.ResultSet rst=stmt.executeQuery("SELECT name, mail from users ");
13 java.sql.Connection conn_ora = java.sql.DriverManager.getConnection("jdbc:default:connection:");
14 String sql ="INSERT INTO test (UNAME,MAIL) VALUES(?, ?)";
15 java.sql.PreparedStatement pstmt = conn_ora.prepareStatement(sql);
16 String uname ;
17 while (rst.next()) {
18 uname = rst.getString(1) ;
19 if ( uname.length() > 0) {
20 pstmt.setString(2,rst.getString(2));
21 pstmt.setString(1,uname);
22 pstmt.executeUpdate();
23 }
24 }
25 pstmt.close();
26 rst.close();
27 stmt.close();
28 con.close();
29 return 0;
30 }
31 catch (Exception e) {
32 System.out.println(e.toString());
33 return -1 ;
34 }
35
36 }
37 public static void main(String[] args) {
38
39 getuser() ;
40 }
41 }
42 /
43
44 alter java source "Loaduser" compile
45 /
46
47 create or replace procedure loaduser as language java name 'Loaduser.getuser()' ;
48 /
|
8. 在sqlplus中執行
exec loaduser
select count(*) from test ;
9. 測試,調試 Oracle中的java class
使用ojvmjava執行class的main方法
ojvmjava -user user/pass
java myclass
10. Over
Create Date : 2007-11-29
Write :阿飛
阅读(2917) | 评论(0) | 转发(0) |