aspose file tools*
The moose likes JDBC and the fly likes 2 ways to connect to a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "2 ways to connect to a database" Watch "2 ways to connect to a database" New topic
Author

2 ways to connect to a database

chihwah li
Greenhorn

Joined: Apr 23, 2010
Posts: 25


Hello all,

I have a question about 2 ways to connect to a database. The following link says that using a dataSource is the preferred way. What is the other way then?
The can't extract that from the text. The text is confusing.

http://docs.oracle.com/javase/tutorial/jdbc/overview/index.html

the part of text which says 2 ways:


JDBC Driver Manager — The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager has traditionally been the
backbone of the JDBC architecture. It is quite small and simple.

The Standard Extension packages javax.naming and javax.sql let you use a DataSource object registered with a Java Naming and Directory Interface™ (JNDI) naming
service to establish a connection with a data source. You can use either connecting mechanism, but using a DataSource object is recommended whenever possible.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
You see this part in the article



Here a DriverManager is being used to create a connection. The DriverManager will take care of loading the jdbc driver classes and creating the connection instance. This was the way to go about until Java 1.4.

Starting from Java 1.4 however, the preferred way is to put your connection details in a configuration file(xml file usually), and then use the JNDI api to create a DataSource object. The JNDI api is used to lookup the details in a config file and make them available in your runtime workspace. Through the DataSource instance you would then be creating a Connection instance. These are the 2 ways of db connection that the article is talking about.
chihwah li
Greenhorn

Joined: Apr 23, 2010
Posts: 25
Aah thanks!

Let me make a guess why XML. By using a XML file, the program does not need to be re-compiled, changes are only made in the XML file.
The file is easier to change Right?

What would be a good place to read the Java API changes? Oracle has API pages, but it's not that you would look very time you need certain code. I prsonally only look if I forgot about
the syntax etc....
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
chihwah li wrote:
Let me make a guess why XML. By using a XML file, the program does not need to be re-compiled, changes are only made in the XML file.
The file is easier to change Right?


Yes...Look up dependency injection, you'll get a good idea.

chihwah li wrote:
What would be a good place to read the Java API changes? Oracle has API pages, but it's not that you would look very time you need certain code. I prsonally only look if I forgot about
the syntax etc....


API changes from one version to another? Not sure, but perhaps you can look through the JLS.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2 ways to connect to a database