My name is Zemian Deng, and I am a software developer. I have been working in various industries with many Java related technologies. My current job is with the Bank of New York Mellon, writing distributed application that manage tri-parties trades and settlements. In my free time, I work on open source projects such as TimeMachine Scheduler. Zemian is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

Creating Oracle Stored Procedure Using Java

12.13.2012
| 2290 views |
  • submit to reddit

Did you know you can write Oracle database stored procedure in Java? Give this a try in your sqlplus prompt. 

sql> create or replace and compile java source named "MyJavaDbProcedure" as
sql> public class MyJavaDbProcedure {
sql>   public static String upcase(String text) {
sql>     return text.toUpperCase();
sql>   }
sql> };
sql> /
sql> 
sql> create or replace function upcase (s in varchar2)
sql>   return varchar2
sql> as language java
sql>   name 'MyJavaDbProcedure.upcase(java.lang.String) return java.lang.String';
sql> /
sql> 
sql> select upcase('hello') from dual;
sql> /


I let the database compile a Java source directly, but there is also the java class PL/SQL that you can load Java binary .class file as well. I am sure your DBA will fight all their might to prevent you doing stuff like this. But it's cool to see that this option is available.


Published at DZone with permission of Zemian Deng, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Christopher Brown replied on Fri, 2012/12/14 - 6:58am

 Hi,

I guess the example should check for NULL and also use text.toUpperCase(Locale.ROOT) if it's production ready... but for the purposes of an example, it shows the idea. :-)

--

Christopher

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.