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

T

tenxian

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()?
 
D

Daniel Pitts

tenxian 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()?
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.
 
A

Arne Vajhøj

tenxian 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()?

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
 
R

Roedy Green

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.
 
T

tenxian

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.

What is a Factory in Java?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top