Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: May 14, 2006 - 12:04 pm UTC

Last updated: January 29, 2008 - 2:42 am UTC

Version: 10G

Viewed 1000+ times

You Asked

Tom

Do you have any suggested readings for PL/SQL developers, who need to learn Java?

I'm particularly insterested in material which compares PL/SQL and Java.

To start with can you help me understand, what is the equivalent of the below in Java.

1.function
2.Procedure
3.Package

Thanks


and Tom said...

I doubt you'll find anything that compares "java" to "plsql" - in fact, I doubt you would find any USEFUL things that compare "language A" to "language B".

for example, your list would be

1) function
2) procedure
3) "class" - but not really, sort of, but not entirely. similar in some aspects,
very different in others.


to learn a language, embrace the language. I hate it for example when a SQL Server developer tries to make Oracle be SQL Server (thinking "I know sql server, let me not really bother to learn how to use this thing called Oracle - let me just pretend it is sql server and I won't have to learn anything new").

Just dive into Java if you are going to learn it. It is a programming language, PLSQL is a programming language - from there, they begin to diverge.

Rating

  (15 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Introduction to Java – PL/SQL Developers Take Heart!

Andries Hanekom, May 15, 2006 - 8:38 am UTC

Found this presentation from 2005 OOW: </code> http://download-east.oracle.com/oowsf2005/644.pdf <code>

It won't turn you into a JAVA programmer over night but it will answer some of those basic questions every PL/SQL developer ask when they start using JAVA.

Best Regards,
Andries Hanekom

PL/SQL to Java.

Bill, May 15, 2006 - 8:44 am UTC

As someone who has used PL/SQL for 12 years and who decided to learn Java a couple of years ago, it sounds like we have some similarities.

If it's JDBC you are interested in, then "Expert Oracle JDBC Programming" by RM Menon, is a good read. For pure Java and a good grasp of the language, then I would recommend "Learning Java" by Niemeyer and Knudsen.

As always the standard documentation on both the Oracle and Sun sites is immensely helpful, both for JDBC / SQLJ and for Java itself.

Hope this may have been of some help.


This doesn't necessarily compare the 2 languages, but...

Kashif, May 15, 2006 - 10:32 am UTC

it gives you a good idea of how PL/SQL and Java work together. It is especially useful if you are planning to use Java inside the Oracle database. It was originally written for 9i but should apply to 10G in most cases.

</code> http://www.amazon.com/gp/product/1861006020/102-8508482-2167356?v=glance&n=283155 <code>

Kashif


Object oriented theory

Bill, May 15, 2006 - 3:11 pm UTC

If you want / need an introduction to OO theory (essential to properly understand Java), I would recommend the classic work "Object Oriented Analysis and Design with Applications", by Grady Booch.

This book is now a few years old and the examples are in C++. It remains however, a superb primer in understanding OO techniques.

Pleased be warned: it is pretty dry and stodgy reading.

PL/SQL CANNOT "Cannot subclass a procedure or package"

A reader, September 17, 2007 - 1:00 am UTC

The document http://download-east.oracle.com/oowsf2005/644.pdf referred above states that PL/SQL CANNOT "Cannot subclass a procedure or package"

Confused here, we can call a procedure from another procedure and the same with packages. Correct? or I'm I missing something here.
Tom Kyte
September 18, 2007 - 1:58 pm UTC

subclassing is an objecty thing.

packages are good old fashioned structured, modular programming constructs

object types can be subclassed.

Need for using Java in database

A reader, September 17, 2007 - 3:37 am UTC

Except for interacting with the os file system, what is the use of using java inside Oracle database? Do we have the exampleof reading the os file system using a java stored procedure on this website( tried searching for it)?
Tom Kyte
September 18, 2007 - 2:12 pm UTC

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:439619916584

there is one


they are few and far far between (the use cases whereby java is the only way to go)

A reader, October 16, 2007 - 10:43 am UTC

Am an Oracle developer for over a decade, found the following book a good primer, not something that will make you an expert, but will introduce you to the key concepts and keep your interest:

http://books.google.co.uk/books?q=head+first+java


object oriented programming in PL/SQL

Otn, January 24, 2008 - 1:28 pm UTC

Tom

I hear Java developers repeatedly boasting that orjecting oriented programming especially inheritance is only possible in Java and not PL/SQL.

But hasnt PL/SQL been enhanced since 9i rel1 for object oriented programming.

Can you please demo an OOPS example using PL/SQL.
Tom Kyte
January 24, 2008 - 1:47 pm UTC

first tell me why OO is the cats meow and the end all be all.

And then have them develop the list of what is necessary in their mind for being "OO"

Is it encapsulation? Got it.
Is it inheritance? Got it.
Is it polymorphism? Got it.

but my bottom line question would be.... yawn. so what. I was doing object oriented implementations before OO existed. As were all educated, good developers. We used to call it modular coding with subroutines.


Encapsulation

rc, January 24, 2008 - 2:15 pm UTC

Please, please correct if I'm wrong but there is no encapsulation when you work with Oracle Objects. There is inheritance and polymorphism but no encapsulation. You can't define private members and private fields and you can't turn off the default constructor.

Packages provide encapsulation but Oracle object types...?

Objects views could have been a great tool but Oracle doesn't really promote it. All the developers who code the middle tier with Java or C# spent much of their time (50%?)doing object relational mapping. They could have used object views.

But if you use ODP.NET (the 'tool' that provides the connection between a .Net program and Oracle) you can't use object views, well you can but the ODP.NET release that supports Oracle objects is dated 27-dec-2007. So it is possible but only for less than a month.


Tom Kyte
January 24, 2008 - 2:41 pm UTC

you do not even need objects to have encapsulation.


think about what encapsulation is....
then then packages.


We don't promote "regular old views" either. Or sequences. Or - well, most mundane things like that.

and object views would not have gotten those people out of the rathole of OR mapping, they'd still be doing it.

for you see - to the OO programming, the application is the end all, be all. It starts and ends with their tiny little application, one of thousands.

oo and or

rc, January 24, 2008 - 3:50 pm UTC

It would be handy if Oracle objects would also provide encapsulation.

I think good ODP.NET support for object views could have made my job easier and support for mdsys.sdo_geometry in combination with ODP.NET is also a very important topic.

I like working with .NET objects and interfaces, properties, inheritance, polymorphism and especially generics.

Good support for Oracle Types makes it also possible to do more business logic with PL/SQL. In my middle tier I have a .NET object, the question is how to get that object in the database? And the next question is how to get my relational data in my middle tier? I think that object views can answer the second question.

For me OO is more than the cats meow, I have made an OO example with PL/SQL:


set echo on
set serveroutput on size 100000

drop table big;

create table big as select object_name from all_objects where 1=0;

drop type action1;

drop type action2;

drop type actionbase;


create or replace type actionbase as object
( action_description varchar2(200)
, action_type varchar2(1)
, not instantiable member procedure do_bulk
, member procedure exec
)
not final
not instantiable
/


create or replace type body actionbase as

member procedure exec
is
begin
dbms_output.put_line('**');
dbms_output.put_line(action_description||':');

do_bulk;

dbms_output.put_line(to_char(sql%rowcount)||' rows '||case action_type
when 'D' then ' deleted '
when 'I' then ' inserted '
when 'U' then ' updated '
end);
dbms_output.put_line('**');
commit;

exception
when others then
--do some logging
raise;
end;


end;
/



create or replace type action1 under actionbase
(
constructor function action1 return self as result
, overriding member procedure do_bulk
)
/


create or replace type body action1
is

constructor function action1 return self as result
is
begin
action_description := 'Insert rows into big from table all_objects';
action_type := 'I';
return;
end;


overriding member procedure do_bulk
is
begin

insert into big
select object_name
from all_objects;

end;

end;
/



create or replace type action2 under actionbase
(
constructor function action2 return self as result
, overriding member procedure do_bulk
)
/


create or replace type body action2
is

constructor function action2 return self as result
is
begin
action_description := 'Delete rows that start with a C';
action_type := 'D';
return;
end;


overriding member procedure do_bulk
is
begin

delete big
where object_name like 'C%';

end;

end;
/

You can test this with:

declare
l_actionbase actionbase;
begin
l_actionbase := action1;
l_actionbase.exec;
l_actionbase := action2;
l_actionbase.exec;
end;
/

It outputs:

**
Insert rows into big from table all_objects:
41692 rows inserted
**
**
Delete rows that start with a C:
608 rows deleted
**

I like this because there is a separation between presentation (dbms_output), transaction and exception handling (member actionbase.exec) and the members action1.do_bulk and action2.do_bulk who do the actual work (the working horses).



Tom Kyte
January 24, 2008 - 5:46 pm UTC

I hardly call dbms_output "presentation", you need a client for that.

transaction and exception handling is (should be) always separate, we need but a stored procedure for that.

And in non-oo code, this example would be smaller, easier to understand, more maintainable.

actionbase.exec - that is a fairly ugly way to accomplish what seems to be more straight forward if we just called

package.do_the_delete_transaction;
package.do_the_create_transaction;


loading up some magic action1, action2 and calling "exec", I would much rather prefer to just call action1 and action2 - and have them be done with it.

This approach almost looks like "assembler" - with "push a bunch of values into a register (call action1) and then jump to some logic (exec), push a bunch of values into a register (call action2) and then jump to some logic..."


I see no value gained by this obfuscated "OO" approach in this case.


give me a good old linear, modular piece of transactional API any day.

modular

rc, January 25, 2008 - 2:28 am UTC

The test block can be changed into:

declare
l_action1 action1 := action1();
l_action2 action2 := action2();
begin
l_action1.exec;
l_action2.exec;
end;
/

I think my approach is very modular and it is maintenable.

If you create an action3 and you forget to override member do_bulk the compiler will raise an error. If you create an instance of actionbase the compiler will raise an error too because actionbase is not instantiable, it won't compile.

If you want to change the commiting, the logging, the exception handling or the interaction with the user you only have to change member actionbase.exec.
Tom Kyte
January 25, 2008 - 9:08 am UTC

My code would be:

begin
my_pkg.fire_employee( .... );
my_pkg.hire_employee( .... );
end;
/


and if you program well formed transactions (fire and hire do one thing, basic premise of modular coding, they do the transaction work), you cannot "forget anything"


You would never commit in plsql (I wish plsql could not COMMIT or ROLLBACK, it would be a much better language without it)

The client is the only one smart enough to commit or rollback - never a small bit of code that performs a database operation.



I find the "object" approach you took to be obscure, obfuscated, not at all easy to understand the "flow". You have to instantiate to tell the system "I think I might want to do something - later, not not, later - maybe" and then you have to tell it "do that thing I said before I might just want to do sometime".

Rather than just saying "hey, do this, do it now, tell me how it went, thanks"

Nothing personal, but your example is actually (in my opinion) the poster child example of why "OO" "adds not much, even takes away" in most cases.


the polymorphic surprises you would get into with such an approach (think five years from now, you have moved on, you have a monolithic code base and someone else owns it)....

procedure p ( l_obj in actionbase )
is
begin
  l_obj.exec;  <<<=== what the HECK did I just do, what code did i actually call
end


After I backtrace through all possible invocations - I discover "I have no way of knowing sitting here what I will do here at runtime"

It is like a great big "jump into the unknown"


No, I don't find this maintainable - understandable - or a good approach, as opposed to straight forward "modular coding"

bulk

rc, January 25, 2008 - 11:47 am UTC

I understand that you should not commit between fire_employee and hire_employee, the client should commit.

My example is not for one or two record operations, but for bulk actions (maybe some kind of ETL operation?). Who should do the commit in a large nightly ETL operation?

But my example is not about the commit, it is about OO. I don't consider it a large obfuscated monolithic code base. There is actually a separation, one member for logging and exception handling, the others members do the actual work.

But I will contemplate your remarks.



Tom Kyte
January 28, 2008 - 6:45 am UTC

...Who should do the commit in a large nightly ETL
operation? ...

the controller of the ETL - written in some language, which might be plsql, java, C, VB, whatever.


I find your example to be way obscure, and adding nothing to what a nice packaged solution would be.

I too would have an actual separation - we just called it MODULAR CODING in the day. Back when we had subroutines instead of "methods".

Typical OO vs. procedural discussion

Andy, January 25, 2008 - 12:07 pm UTC

This is the typical discussion between OO developers and procedural developers.

To a procedural developer, what is important in the comparison is that procedural code is straightforward and simple, and the learning curve is lower. To an OO developer, what is important in the comparison is that with OO code, even though there is a steeper learning curve (for both the language/technology and also the system being developed), once you get past that learning curve, modifications are easier, and you can add/change a lot of functionality with just one or two small changes in the code.

Both perspectives are legitimate. No matter what tool you're using, you can be very productive if you know it well. Tom obviously knows a procedural approach very well, and some developers know an OO approach well.

Another consideration: you have to consider the person who comes behind you to maintain your code 2 years from now. If you're using technology that very few people know (such as OO PL/SQL), then it may be hard for your team to find someone to replace you. However, if you're a Java developer, working with OO Java, then you're going to be much easier to find a replacement for.
Tom Kyte
January 28, 2008 - 6:46 am UTC

modifications are not easier - they have never been easier, I've never ever seen a proof point in that regards - ever. I have been, was an OO programmer. I'm reformed. I've lost the religion if you will.



chicken egg

rc, January 25, 2008 - 3:27 pm UTC

It is true that 'I' have to consider the person who comes behind me to maintain 'my' code.

But this also creates a chicken egg situation. If no one knows OO PL/SQL no one can use OO PL/SQL, and if no one uses OO PL/SQL no one can know OO PL/SQL so no one will use OO PL/SQL.

I like both, I like PL/SQL and I like C# (very OO).


OO

rc, January 28, 2008 - 8:39 am UTC

If the controller of the ETL is written in PL/SQL and you can't do a commit in PL/SQL, how can you commit?

But well provide your nice packaged solution.

I know that there is no real difference between functions, methods, subroutines, procedures and members. It is just a name. In Delphi they are called procedures and functions and Delphi is OO.
Tom Kyte
January 29, 2008 - 2:42 am UTC

ok, you see - you are talking about the 0.01% of the time when you would want to commit in plsql.

Here is my stance:

99.99999999% of the time people commit or rollback in plsql - they have completely and utterly missed the point and are doing it wrong, entirely wrong

Therefore, much like my stance on triggers (hate em, wish they would disappear), autonomous transactions (same thing), when others (go away...) - I would rather do without commit/rollback in plsql for the general "safety" of everyone else.

I would rather not have these features because they are abused, misused, applied at the wrong time in the wrong way - universally.

Something has to connect to oracle and run the procedures, let it commit.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library