Estou usando um exemplo de uma classe que usa Connection Pool que achei em um livro para fazer várias conexões ao banco:
import java.util.*;
import java.sql.*;
public class PoolConexao
{
private Hashtable connections = new Hashtable();
private Properties props;
public PoolConexao (Properties props, int conexaoinicial)
throws SQLException, ClassNotFoundException
{
this.props = props;
initializePool(props, conexaoinicial);
}
public PoolConexao (String driverClassname, String dbUrl, String user, String password,int conexaoinicial)
throws SQLException, ClassNotFoundException
{
props = new Properties();
props.put("connection.driver",driverClassname);
props.put("connection.url",dbUrl);
props.put("user",user);
props.put("password",password);
initializePool(props,conexaoinicial);
}
public Connection getConnection() throws SQLException
{
Connection con = null;
Enumeration cons = connections.keys();
synchronized (connections) {
while (cons.hasMoreElements()) {
con = (Connection)cons.nextElement();
Boolean b = (Boolean)connections.get(con);
if (b == Boolean.FALSE)
{
try
{
con.setAutoCommit(true);
}
catch (SQLException e)
{
connections.remove(con);
con = getNewConnection();
}
connections.put(con,Boolean.TRUE);
System.out.println("conectou - 1");
return con;
}
}
con = getNewConnection();
connections.put(con, Boolean.FALSE);
System.out.println("conectou - 2");
return con;
}
}
public void returnConnection(Connection returned)
{
if (connections.containsKey(returned))
connections.put(returned, Boolean.FALSE);
}
private void initializePool(Properties props,int conexaoinicial)
throws SQLException, ClassNotFoundException
{
Class.forName(props.getProperty("connection.driver"));
for (int i = 0; i < conexaoinicial; i++)
{
Connection con = getNewConnection();
connections.put(con, Boolean.FALSE);
}
}
private Connection getNewConnection()
throws SQLException
{
return DriverManager.getConnection(props.getProperty("connection.url"),props);
}
}
e coloquei na minha servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class ConsultaCandidato extends HttpServlet
{
private PoolConexao pool;
public void init(ServletConfig servletConfig)
throws ServletException
{
String jdbcDriver = "interbase.interclient.Driver";
String jdbcSubProtocol = "jdbc:interbase://localhost/";
String dbName = "c:/monografia/eleicao.gdb";
String dbUser = "SYSDBA";
String dbPasswd = "masterkey";
try
{
pool = new PoolConexao(jdbcDriver,jdbcSubProtocol + dbName,dbUser, dbPasswd,5);
System.out.println("Banco conectado !");
}
catch (Exception e)
{
System.out.println("Banco não conectado !!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Connection con = null;
Statement statement = null;
try
{
con = pool.getConnection();
con.setAutoCommit(false);
contador = 0;
statement = con.createStatement();
…
Mas não estou conseguindo me conectar ao banco. Onde está o erro ?
[color=“red”][size=“9”]Editado para conter o BBcode by eltonk[/size][/color]