Oracle Java stored procedure access to Heterogeneous Databases

Source: Internet
Author: User

The following articles mainly introduce how to use Oracle Java stored procedures to access heterogeneous databases. We all know that there may be many types of Oracle databases in actual enterprise applications, the interconnection between these heterogeneous databases often becomes the bottleneck of an application.

There are many solutions. One is to bridge the heterogeneous databases through applications, and the other is the solutions provided by database vendors, such as the Link Server of SQL Server, the Transparent Gateway technology of Oracle; the third is through the database application extension interface, for example, Oracle PL/SQL allows access to external databases through Java extension.

This article focuses on the third point, taking Oracle connection to SQL Server as an example, discusses how to use Java to write PL/SQL to access SQL Server, this is especially meaningful for Oracle applications that require access to SQL Server on non-WINDOWS platforms.

The system configuration in this article is as follows.

I. Hardware and Operating System

Dell Intel Xeon Server + Windows2000 Server

Ii. database environment

Oracle 8.1.7

Iii. Installation

To enable Oracle to support Java, you must install the Jserver component on Oracle. When installing the Oracle software, if you choose a typical installation, Jserver is installed by default. If you choose custom installation, select the Jserver component. Another method is to manually install Jserver. The method is as follows:

Go to the \ javavm \ install directory under the Oracle installation directory and run initjvm as the sys user. SQL. This script configures a Java Runtime Environment for the database and loads the Basic java class library to the database. Before executing this script, we recommend that you read the \ javavm \ readme.txt file, which includes instructions on necessary modifications to some database configurations before installation, and descriptions on how to handle script execution errors.

The script execution time is about 10 minutes. If the script fails, you can execute it again.

After the script is executed, you can use the DBA Studio tool to see the addition of Jserver.

Iv. Grant Permissions

To execute a Java stored procedure in Oracle, you need a role named javauserpriv. This role is automatically created after Jserver is installed and assigned to each Oracle account.

5. Compile the Oracle Java stored procedure

1. Compile a Java class on the local machine.

 
 
  1. public class TEST  
  2. {  
  3. public static void main (String args[])   
  4. {  
  5. System.out.println("HELLO THIS iS A JAVA PROCEDURE");  
  6. }  
  7. }  
  8. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

2. Use the loadjava command to upload the written java class files to the database.

Example: C :\> loadjava-u test/oracle @ mytest-o-v-r d: \ TEST. java

Of course, in addition to uploading the. java file, you can also upload the. class/. jar/. properties file. the uploaded file will exist in the database as an Object.

If the file is changed, you can upload the file again. The previous version will be overwritten.

3. Generate a declaration Interface

The uploaded java class needs to be released into an interface that can be called. These interfaces can be Store Procesure, Trrigger, Function, and so on.

 
 
  1. SQL>create or replace procedure test_java   
  2. as language java   
  3. name 'TEST.main(java.lang.String[])';  

4. Test

 
 
  1. SQL> set serveroutput on size 5000  
  2. SQL> call dbms_java.set_output(5000); 

The call is complete.

 
 
  1. SQL> execute test_java;  
  2. HELLO THIS iS A JAVA PROCEDURE 

The PL/SQL process is successfully completed.

 
 
  1. SQL> call test_java();  
  2. HELLO THIS iS A JAVA PROCEDURE 

The call is complete.

5. The above is a simple java class. to access a non-Oracle Database in Oracle, only

You can access non-Oracle databases in the java class.

 
 
  1. public static String getStringFomSql(){  
  2. String strResult ="";  
  3. Connection conn = null;  
  4. Statement stmt = null;  
  5. ResultSet rs = null;  
  6. try {  
  7. Class.forName("com.inet.tds.TdsDriver").newInstance();   
  8. String url="jdbc:inetdae7:10.24.09.192:1433?database=pubs";   

Pubs for your database

 
 
  1. String user="sa";   
  2. String password="123";   
  3. conn= DriverManager.getConnection(url,user,password);   
  4. String sql="select top 1 dtype,dname from test";   
  5. stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   
  6. rs=stmt.executeQuery(sql);   
  7. if( rs.next()){  
  8. strResult = "The result from sql server is:dtype->" + rs.getString("dtype") + " dname->" + rs.getString("dname");  
  9. }  
  10. } catch (InstantiationException e) {  
  11. TODO Auto-generated catch block  
  12. e.printStackTrace();  
  13. strResult = "operator fail in InstantiationException";  
  14. } catch (IllegalAccessException e) {  
  15. TODO Auto-generated catch block  
  16. e.printStackTrace();  
  17. strResult = "operator fail in IllegalAccessException";  
  18. } catch (ClassNotFoundException e) {  
  19. TODO Auto-generated catch block  
  20. e.printStackTrace();  
  21. strResult = "operator fail in ClassNotFoundException";  
  22. } catch (SQLException e) {  
  23. TODO Auto-generated catch block  
  24. e.printStackTrace();  
  25. strResult = "operator fail in SQLException";  
  26. }  
  27. finally{  
  28. try {  
  29. rs.close();  
  30. conn.close();  
  31. } catch (SQLException e1) {  
  32. e1.printStackTrace();  
  33. }  
  34. }  
  35. return strResult;  
  36. }  


It is worth noting that accessing SQL Server through java storage, if a result set is returned, there is no corresponding data type to be converted in Oracle versions below 9i, A work und is to either return a string or store the returned result set in the Oracle table. This is done to make it easier for programs that cannot directly access SQL Server on non-Windows platforms.

6. External programs call Oracle java stored procedures

The method for calling a java stored procedure is the same as that for calling a general Oracle Java stored procedure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.