Re: DriverManager vs. DataSource?
From: Lee Fesperman (firstsql_at_ix.netcom.com)
Date: 01/21/04
- Next message: Oscar Kind: "Re: DriverManager vs. DataSource?"
- Previous message: Noons: "Re: choices regarding where to place code - in the database or middle tier"
- In reply to: hank barta: "DriverManager vs. DataSource?"
- Next in thread: Oscar Kind: "Re: DriverManager vs. DataSource?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 21 Jan 2004 09:21:32 GMT
hank barta 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.
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.
-- Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com) ============================================================== * The Ultimate DBMS is here! * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
- Next message: Oscar Kind: "Re: DriverManager vs. DataSource?"
- Previous message: Noons: "Re: choices regarding where to place code - in the database or middle tier"
- In reply to: hank barta: "DriverManager vs. DataSource?"
- Next in thread: Oscar Kind: "Re: DriverManager vs. DataSource?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|