This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 ways to connect to a database

 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic