Home > Archive > PostgreSQL JDBC > March 2005 > only postgresqsl in connection pool









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author only postgresqsl in connection pool
Nico

2005-03-31, 8:04 pm

Hello,
I have a servlet that runs on a postgresql-database. Or rather: that's what
I thought...
Let me start at the beginning. Originally, I made it in MySQL, but this
didn't meet my requirements: the version installed on the server didn't
support subqueries, so I switched to PostgreSQL. In the mean time, the
database was created in MySQL and in PostgreSQL.
When I get a connection, it connected to PostgreSQL. However, since I
connect to the database through a connectionpool - I think - I only get
connections from MySQL.
So here is my question:
how do I tell a java servlet I want a PostgreSQL connection - and not a
MySQL connection- from a connection pool? I changed the username & password
in the code for obvious security reasons...

Kind regards,
Nico.

Here is my code for connecting to the database:
protected final static String strDbdriver = "org.postgresql.Driver";
protected static void connect(String dbdriver, String methodname)
throws DriverNotFoundExcept
ion, ConnectionException
{
try
{
Class.forName(dbdriver);
if(dbname==null)
throw new
menus. ConnectionException(
dbdriver,"connect","dbname is null.");
if(dbname.equals(""))
throw new menus. ConnectionException(
dbdriver, "connect",
"dbname has empty string.");
if(dbname.equals("null"))
dbname="databasename";
int connattempts=0;
while(connattempts<=5)
{
try
{
if(pool==null)
{
javax.naming.Context env=(javax.naming.Context)
new
javax.naming.InitialContext().lookup("java:comp/env");
pool=(DataSource)env
.lookup("jdbc/"+dbname);
}
connection=pool.getConnection("username","password");
connattempts=8;
}
catch(NullPointerExc
eption fout)
{
disconnect();
connattempts++;
}
catch(SQLException fout)
{
disconnect();
connattempts++;
}
}
if(connattempts!=8)
{
try
{
connection =
DriverManager.getConnection("jdbc:postgresql://localhost/" +
dbname + "?user=username&password=password");
}
catch(SQLException fout)
{
throw new menus. ConnectionException(
strDbdriver,
methodname,"connection failed.");
}
}
statementM1 = connection.createStatement();
statementM2 = connection.createStatement();
statementM3 = connection.createStatement();
statementM4 = connection.createStatement();
statementE1 = connection.createStatement();
statementE2 = connection.createStatement();
statementMeta = connection.createStatement();
}
catch(ClassNotFoundE
xception exception)
{
throw new DriverNotFoundExcept
ion(dbdriver,methodn
ame);
}
catch(SQLException fout)
{
disconnect();
throw new menus. ConnectionException(
dbdriver,methodname,
"cannot
create statements");
}
catch(NamingExceptio
n fout)
{
disconnect();
throw new menus. ConnectionException(
dbdriver,
methodname,"namingexception: "+fout.getExplanation());
}
}


Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com