Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Why can't we "new"  a Statement object?

Thread view: 
tenxian - 29 Apr 2008 01:10 GMT
Why do we have to use the Connection object to produce a Statement
object? Similar question like, Why do we have to use the DriverManager
to produce a Connection object? Can't we create a Connection object
via Connection myConn=new Connection()?
Daniel Pitts - 29 Apr 2008 01:24 GMT
> Why do we have to use the Connection object to produce a Statement
> object? Similar question like, Why do we have to use the DriverManager
> to produce a Connection object? Can't we create a Connection object
> via Connection myConn=new Connection()?
The pattern is called "Factory"

Connection may not be a concrete type. It might not even be a class!
You don't know what type of Connection you're going to get unless you
know what Driver you're using. etc...

Same goes with Statement. Different drivers may create different
Connections, which create different Statements, but all *you* know about
are the static abstract base classes/interfaces.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Lew - 29 Apr 2008 01:38 GMT
tenxian wrote:
>> Why do we have to use the Connection object to produce a Statement
>> object? Similar question like, Why do we have to use the DriverManager
>> to produce a Connection object? Can't we create a Connection object
>> via Connection myConn=new Connection()?
> The pattern is called "Factory"

> Connection may not be a concrete type. It might not even be a class!

Indeed, Connection certainly is not a concrete type:
<http://java.sun.com/javase/6/docs/api/java/sql/Connection.html>

The whole point of programming to interfaces is to prevent you from
instantiating a class like

 Connection cxn = new VendorSpecificConnection();

The problem is that this code is now locked in permanently to this particular
connection class.

With DriverManager.getConnection(), the client code has no clue what the
actual class is.  That means it can run without recompilation or redeployment
on any class.

By preventing the client from even seeing what concrete classes are available,
the API makes it much easier to write stable, robust, efficient, portable code.

Under JPA one uses injection via annotations, and life gets sweeter still.

In theory.

Signature

Lew

tenxian - 29 Apr 2008 02:29 GMT
On Apr 29, 8:24 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > Why do we have to use the Connection object to produce a Statement
> > object? Similar question like, Why do we have to use the DriverManager
[quoted text clipped - 13 lines]
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

What is a Factory in Java?
Arne Vajhøj - 29 Apr 2008 02:43 GMT
> On Apr 29, 8:24 am, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
[quoted text clipped - 13 lines]
>
> What is a Factory in Java?

Same as in other languages.

See http://en.wikipedia.org/wiki/Factory_method_pattern !

Arne
Matt Humphrey - 29 Apr 2008 02:49 GMT
> On Apr 29, 8:24 am, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
[quoted text clipped - 4 lines]
>>
>> The pattern is called "Factory"

> What is a Factory in Java?

http://en.wikipedia.org/wiki/Factory_method_pattern

You can look these things up yourself, you know.

Matt Humphrey http://www.iviz.com/
Roedy Green - 29 Apr 2008 05:03 GMT
On Mon, 28 Apr 2008 18:29:19 -0700 (PDT), tenxian
<hi.steven.tu@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>What is a Factory in Java?

see http://mindprod.com/jgloss/factorymethod.html

It is a design pattern for constructing objects.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajhøj - 29 Apr 2008 01:56 GMT
> Why do we have to use the Connection object to produce a Statement
> object? Similar question like, Why do we have to use the DriverManager
> to produce a Connection object? Can't we create a Connection object
> via Connection myConn=new Connection()?

You can:

Connection myConn = new FoobarDatabaseSpecificConnection();

but you will hopefully never be able to get that type
of code through code review, because the code is
Foobar database specific.

It is a core concept in Java that database code should
be database independent.

DriverManager.getConnection enables you to use the
same code for different databases (the connection URL
can be put in a configuration file).

Arne
Roedy Green - 29 Apr 2008 01:56 GMT
On Mon, 28 Apr 2008 17:10:52 -0700 (PDT), tenxian
<hi.steven.tu@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>Why do we have to use the Connection object to produce a Statement
>object? Similar question like, Why do we have to use the DriverManager
>to produce a Connection object? Can't we create a Connection object
>via Connection myConn=new Connection()?

This derives from the fact that every SQL database may implement
things quite differently. All that gets nailed down is the INTERFACE,
not the classes or even an abstract class basic implementation.

The code you run for Statement.someMethod is completely different for
Oracle vs MySQL .

New presumes a common class and common implementation.  When you don't
have that you need a pluggable mechanism and a factory to get you the
objects you need.  That's one thing the driver does.

You will see the same sort of thing in JCE where you can have
different implementors.

JavaMail allows different implementations so it too likely works this
way though it has been a while since I fiddled with those details.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.