SQL Zone is brought to you in partnership with:

My name is Zemian Deng, and I am a Senior Application Engineer working at Oracle for the Enterprise Knowledge Management product. NOTE: The views expressed on my blog and social network are my own and do not necessarily reflect the views of my employer. Zemian is a DZone MVB and is not an employee of DZone and has posted 77 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
| 6636 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.