Skip to content

Java Code on sqlplus

February 17, 2011

Trying something new. This blog page gives steps to write, compile and execute a piece of java code on sqlplus. So if you are lonely with no java compiler sqlplus got the answer to your java code.

Here it is.

Step-1: Write Java code by following below sql syntax.

ops@sqindia:/opt/10g > create or replace and compile java source
named “ABC”
2 as
3 import java.util.*;
4 import java.text.*;
5 public class ABC
6 {
7 static public void java_get_timezone( String[] p_timezone )
8 {
9 Date d = new Date();
10 DateFormat df2 = new SimpleDateFormat( “z” );
12 df2.setTimeZone( TimeZone.getDefault() );
13 p_timezone[0] = df2.format(d);
14 }
15 }
16 /

Java created

Step-2: Create a SQL procedure get_timezone which will create a java instance and call the particular method.

ops@sqindia:/opt/10g > create or replace
2 procedure get_timezone( p_timezone out varchar2 )
3 as language java
4 name ‘ABC.java_get_timezone( java.lang.String[] )’;
5 /
Procedure created.

3. Anonymous block to invoke the above created procedure.
ops@sqindia:/opt/10g > declare
2 tz varchar(25);
3 begin
4 get_timezone( tz );
5 dbms_output.put_line( tz );
6 end;
7 /

Quite amazing; this facility really helps when you have only sqlplus in hand to validate a piece of java code.


From → java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: