Based on the how-to and modified for your app:

package yourpackage;

import java.sql.*;

import javax.naming.*;

import javax.sql.*;

public class Conn {

/**Takes desired database as a string and returns a connection.

*/

public static Connection getConn(String dBase) {

Connection connection = null;

String osName = System.getProperty("os.name");

try {

//Start of Tomcat connect

Context ctx = new InitialContext();

if (ctx == null) {

System.err.println("Conn.getConn ctx is null");

throw new Exception("Boom - No Context");

DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + dBase);

if (ds != null)

connection = ds.getConnection();

//End of Tomcat connect

} catch (Exception e) {

System.err.println("Conn.getConn " + e);

}

return connection;

}

}



> I think this link over here, might give you a hand.
>
>
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
>
> There are samples there for databases like mysql, but I think you should
> be ok.
>
>
> Todd O'Bryan wrote:
>
> > This may not be the right place to ask this, but if you can direct me
> > to the right place, I'd appreciate it.
> >
> > I'm looking for a design pattern that someone must have already
> > thought through so that my head can stop hurting. Here's the problem:
> >
> > I'm designing a webapp that has several servlets that all access a
> > database (or a couple of databases, actually) to either update or
> > retrieve information.
> >
> > Rather than initializing these connections multiple times, dealing
> > with SQLExceptions in every class that uses JDBC queries, and
> > generally doing things multiple times, I thought I'd put all the
> > database stuff into a single class. I created a setUp() method that
> > initialized the database connection and then used static methods so I
> > could do something like
> >
> > SQLUtils.executeQuery("a SQL statement");
> > SQLUtils.executeUpdate("another one");
> >
> > anywhere in my webapp. If the database connection had not been
> > created, it got created before executing the SQL statement. If it was
> > already created, it just got done. I handled all the nastiest
> > exceptions in the SQLUtils class, so I didn't have to deal with them
> > elsewhere.
> >
> > You can probably guess the next part. I've discovered I need to
> > connect to more than one database, and this design does not support
> > that. Creating instances of a SQLUtil class would be a big pain,
> > because then I have to pass those around between my servlets and I
> > lose one of the huge advantages of this approach, namely a single,
> > globally visible interface to the database.
> >
> > I thought about multiple classes, one for each database I'm connecting
> > to, but I know that can't be right on so many levels. Meanwhile, I'm a
> > little stumped.
> >
> > How do people handle this elegantly? The requirements are: a single,
> > globally visible (within a webapp) database interface and the ability
> > to access multiple databases easily.
> >
> > Thanks in advance for any ideas,
> > Todd
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to