Re: DriverManager vs. DataSource?
From: Oscar Kind (oscar_at_danwa.net)
Date: 01/21/04
- Previous message: Lee Fesperman: "Re: DriverManager vs. DataSource?"
- In reply to: hank barta: "DriverManager vs. DataSource?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Jan 2004 09:25:09 GMT
hank barta <hbarta@comcast.net> wrote:
> 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.
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
-- No trees were harmed in creating this message. However, a large number of electrons were terribly inconvenienced.
- Previous message: Lee Fesperman: "Re: DriverManager vs. DataSource?"
- In reply to: hank barta: "DriverManager vs. DataSource?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|