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 DriverNotFoundException, 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(NullPointerException 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(ClassNotFoundException exception)
{
throw new DriverNotFoundException(dbdriver,methodname);
}
catch(SQLException fout)
{
disconnect();
throw new menus.ConnectionException(dbdriver,methodname,"cannot
create statements");
}
catch(NamingException fout)
{
disconnect();
throw new menus.ConnectionException(dbdriver,
methodname,"namingexception: "+fout.getExplanation());
}
}