DriverManager vs. DataSource?

From: hank barta (hbarta_at_comcast.net)
Date: 01/21/04


Date: Wed, 21 Jan 2004 04:25:29 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



Relevant Pages

  • Re: Connection Pooling for multiple JSPs
    ... > I'm not sure how to reap the same benefits with multiple JSP pages. ... connection pool on initialization and then provides a facade to it (i.e. ... datasource that simply call the corresponding methods from the datasource). ... You probably want the method getInstance to read a properties file that ...
    (comp.lang.java.help)
  • Re: DriverManager vs. DataSource?
    ... > Is it even possible to use DriverManager in a servlet or JSP? ... If it is a driver for all applications, you may instead choose to install ... > the DataSource interface will typically be registered with a JNDI ...
    (comp.lang.java.databases)
  • Re: JSP functions/methods page question
    ... is it possible to create a function/method jsp page. ... > seperate JSP page that contained a function to open the connection so i ... What you could do is use Servlets and using the the ...
    (comp.lang.java.programmer)
  • Re: J2ME or network programming or...what do you recommend?
    ... I started doing the servlets along with the JSP. ... Well depending on how dynamic you want the client to ... needed) to java server programs? ...
    (comp.lang.java.programmer)
  • Re: Expression Language (EL) vs. Scriptlets in JSP
    ... is using servlets and utility classes for all real code ... I gave up on taglibs, EL, ... and unit test them with mock objects. ... code in JSP, you won't know if it's even legal syntax, let alone if it's ...
    (comp.lang.java.programmer)