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 / Databases / January 2004

Tip: Looking for answers? Try searching our database.

DriverManager vs. DataSource?

Thread view: 
hank barta - 21 Jan 2004 04:25 GMT
   I've been working with some code that accesses a MySQL database with
   plans to use it in some web applications (either servlets or JSPs.) I
   started with some old code that uses the java.sql.DriverManager
   class to establish the connection. Looking at some code in the
   J2EE samples, it looks like (and I quote from the documentation)
   "The use of a DataSource object is the preferred means of connecting
   to a data source."

   This leaves me with some questions.

   Is it even possible to use DriverManager in a servlet or JSP? I
   started looking at the documentation because I could not figure out
   how to get my JSP to find mysql.jar.

   Can I use DataSource in a console app? It looks like it requires a
   JNDI server (again, according to the docs "An object that implements
   the DataSource interface will typically be registered with a JNDI
   service provider.") I presume that means that I need to run the
   servlet/JSP container at a minimum to use a data source (whether
   for a console app or JSP.)

   I presume that once I have a Connection, it doesn't matter whether
   it came from a DriverManager or DataSource, the behavior is pretty
   much the same. Is that correct?

   I would like to retain the capability to test my database code
   from a console application rather than from within a JSP. Will
   it be necessary to use different connection methods depending on
   the environment or can I switch to the DataSource and use that in
   both environments.

   Finally, if these are routine questions that are answered somewhere,
   please point me to that resource!

   thanks,
   hank
Lee Fesperman - 21 Jan 2004 09:21 GMT
>     I've been working with some code that accesses a MySQL database with
>     plans to use it in some web applications (either servlets or JSPs.) I
[quoted text clipped - 9 lines]
>     started looking at the documentation because I could not figure out
>     how to get my JSP to find mysql.jar.

Yes, it is possible to use the DriverManager interface. DataSources are preferred
because they make it easy for the JSP/servlet container to provide connection pooling
and other services automatically (under the covers).

JDBC driver jars go in a 'common' directory in the container. The container docs should
specify where to put those jars so that they are accessible.

>     Can I use DataSource in a console app? It looks like it requires a
>     JNDI server (again, according to the docs "An object that implements
>     the DataSource interface will typically be registered with a JNDI
>     service provider.") I presume that means that I need to run the
>     servlet/JSP container at a minimum to use a data source (whether
>     for a console app or JSP.)

You can use DataSource in a 'non-managed' (outside the container server) configuration.
You will need to have the JDBC 2.0 Standard Extension jar or the J2EE jar on the
classpath (to reference javax.sql.DataSource), unless you're using JDK 1.4.x. You don't
need JNDI. You just instantiate the appropriate javax.sql.DataSource implementation, set
connection properties and get the connection (java.sql.Connection). See the javadocs on
javax.sql.DataSource and the docs for your JDBC driver for details.

>     I presume that once I have a Connection, it doesn't matter whether
>     it came from a DriverManager or DataSource, the behavior is pretty
>     much the same. Is that correct?

Yes, that is correct for simple JSP/Servlets.

Signature

Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Oscar Kind - 21 Jan 2004 09:25 GMT
>    I've been working with some code that accesses a MySQL database with
>    plans to use it in some web applications (either servlets or JSPs.) I
[quoted text clipped - 9 lines]
>    started looking at the documentation because I could not figure out
>    how to get my JSP to find mysql.jar.

Place it in the directory WEB-INF/lib. All .jar (and IIRC also .zip) files
in that directory are automatically added to the classpath of the
application.

If it is a driver for all applications, you may instead choose to install
it in the entire application server (for Tomcat 4.x by placing it in the
directory common/lib). This has two obvious consequences:
- That version of the driver is available to all applications.
- Installation becomes more difficult than trivial (i.e. you have to
 copy files to two locations, often cannot use a web interface, etc.).

>    Can I use DataSource in a console app? It looks like it requires a
>    JNDI server (again, according to the docs "An object that implements
>    the DataSource interface will typically be registered with a JNDI
>    service provider.") I presume that means that I need to run the
>    servlet/JSP container at a minimum to use a data source (whether
>    for a console app or JSP.)

Yes, by using a context provider (the JNDI server of the application
server).

>    I presume that once I have a Connection, it doesn't matter whether
>    it came from a DriverManager or DataSource, the behavior is pretty
>    much the same. Is that correct?

Yes. Behaviour is identical. There is however, one important difference:
getting a connection from a datasource is significantly faster.

>    I would like to retain the capability to test my database code
>    from a console application rather than from within a JSP. Will
>    it be necessary to use different connection methods depending on
>    the environment or can I switch to the DataSource and use that in
>    both environments.

Since you can use a datasource from a console application, you can use the
same method for both.

>    Finally, if these are routine questions that are answered somewhere,
>    please point me to that resource!

This is a question I leave to others, since I learned this by doing.

Oscar

Signature

No trees were harmed in creating this message.
However, a large number of electrons were terribly inconvenienced.



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.