Convert Java functions to functions in Oracle

Source: Internet
Author: User

 

A very powerful function in Oracle is to convert functions written in Java into functions in Oracle databases. after conversion, we can use it like a common function. because Oracle is developed in Java, it is naturally compatible with Java. let's talk about the specific operation. for example, create a function to generate a file.

 

1. The first step is to create a java resource object. objects such as function and procedure are all objects in Oracle. Therefore, the syntax is similar.

 

 

Create or replace and compile java source named createfile as -- in addition to this sentence, all of the following are standard Java. If you create a class in eclipse, you can copy it intact.

-- Createfile is the object name, just like the table name. however, it is not directly used in Function Conversion. you only need to use the class name and function name in Java. java source object names and java class names cannot be repeated.

 

Package weiwenhp; -- the definition of the package name can also be removed here. However, if you want to call this class later, you must add package.

Import java. io .*;

Public class JavaClass

{

Public static boolean createFile (String path, String fileName)

{

String fileInfo = path + "\" + fileName;

File newFile = new File (fileInfo );

Try {

Return newFile. createNewFile ();

}

Catch (Exception e ){

System. out. println (e. toString ());

Return false;

}

}

}

 

2. Step 2. Create an Oracle function and reference the above Java function createFile

 

 

Create or replace function newfile (path in string, filename in string)

RETURN boolean

IS

LANGUAGE JAVA

NAME 'weiwenhp. JavaClass. createFile (java. lang. String, java. lang. String) return boolean ';

The statement before "IS" IS the standard statement for creating a function. remember that the parameters and return types of the function must be the same as those of the referenced java function. the language java name following is a fixed keyword.

Then there is the content in the single quotes: The format is package name. Class Name. Function Name (parameter list) return function return type.

This task is completed. A function called newfile is generated in Oracle.

 

3. Write a pl/SQL statement to verify it.

 

 

Declare

Createresult boolean;

Begin

Createresult: = newfile ('d: \ ', 'weiwenhp.txt'); -- note that strings in Java use double quotation marks, but single quotation marks are used in Oracle. in addition, Java is case sensitive. oracle is not distinguished. however, the quotation marks are differentiated.

If (createresult) then

Dbms_output.put_line ('create file success ');

Else

Dbms_output.put_line ('create file failed ');

End if;

End;

 

4. Supplement: Call a custom class.

 

 

We know that in Java, in addition to calling the system-defined classes, we can also call custom classes. This can also be done here.

Suppose we create another java source. We need to call the JavaClass class defined earlier.

Create or replace and compile java source named newcreatefile

Import weiwenhp. JavaClass;

-- If you call this method directly, the import JavaClass will be incorrect.

Import java. io .*;

Public class NewJavaClass

{

//...........

}

 

From smart dummies

Related Article

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.