2009-11-05 4 views
0

J'essaye d'implémenter le pool de connexion dans java. J'ai écrit un code de java et un fichier de jsp. Quand je l'ai exécuté sur tomcat6.0.20, j'ai eu l'erreur suivante.Connexion Pooling en Java

javax.servlet.ServletException: Impossible de créer pilote JDBC de la classe '' pour l'URL de connexion 'null'

sur cette ligne Connection conn = ds.getConnection();

+0

Vous essayez de mettre en œuvre la mise en commun de connexion, ou tout simplement le configurer? – skaffman

Répondre

-1

Avez-vous configuré la source de données?

+0

oui, j'ai configuré le fichier context.xml de Tomcat.J'ai écrit les informations de la ressource là-bas.J'ai créé un web.xml dans lequel j'ai référencé la ressource declare dans context.xml –

0

Veuillez suivre les étapes.

1. Créer une ConnectionManager classe:

public class ConnectionManager { 
Vector connectionPool = new Vector(0, 1); 
private static ConnectionManager obConnectionManager = new ConnectionManager(); 

    /** 
    * TX Data Source. 
    */ 
    DataSource obTXDataSource = null; 

    /** 
    * Non TX Data Source. 
    */ 
    DataSource obNonTXDataSource = null; 

    /** 
    * A non tx data source created for using it in reports. 
    * This has been created for using it in reports with non transactional connection. 
    * 
    * The existing non- transaction connection attribute and function is being called from 
    * many modules. In order to have lesser impact this function and attribute is being introduced. 
    * This has been done after the conference call had with Retesh - SMS 
    */ 

    DataSource reportsNonTxDataSource = null; 

    /** 
    * Constuctor for the class. 
    */ 
    private ConnectionManager() { 
    } 

    /** 
    * Returns instance of the class. 
    * @return Instance of the class 
    */ 
    public static ConnectionManager getInstance() { 
     return obConnectionManager; 
    } 

    /** 
    * Returns the database connection using a transactional Datasource. 
    * @return The database connection using a transactional Datasource 
    * @exception PersistenceException 
    */ 
    public Connection makeTXConnection() 
     throws PersistenceException { 
     try { 
      if (obTXDataSource == null) { 
       obTXDataSource = ServiceLocator.getTXDataSource(); 
      } 
      Connection obConnection = obTXDataSource.getConnection(); 
      connectionPool.add(obConnection); 
      return obConnection; 
     } catch (ServiceLocatorException se) { 
      throw new PersistenceException("PE002", 
       "Error making database connection: " + se.getMessage()); 
     } catch (SQLException se) { 
      throw new PersistenceException("PE002", 
       "Error making database connection: " + (new Integer(se.getErrorCode())).toString() + 
       se.getMessage()); 
     } 
    } 
} 

2. Créer une classe QueryManager:

public class QueryManager { 
    /** 
    * This is an object of Debug class for locating error in the process. 
    */ 
    private Debug debug = new Debug(QueryManager.class); 

    /** 
    * The PreparedStatement instance. 
    */ 
    private PreparedStatement obPreparedStatement = null; 

    /** 
    * The Connection instance. 
    */ 
    private Connection obConnection = null; 

    private int procedureTimeOut = 0;//V3.2 

    /** 
    * Constuctor for the class. 
    */ 
    public QueryManager() { 
    } 

    /** 
    * Executes the SELECT statement. 
    * 
    * @param sql 
    *   The SELECT statement 
    * @param params 
    *   The parameters passed to the SELECT statement 
    * @return the Array of Data fetched after executing the SELECT statement 
    * @exception PersistenceException 
    */ 
    public ArrayList executeSQL(String sql, Object[] params) 
      throws PersistenceException { 
     java.util.Date startDate = new java.util.Date(); 
     java.util.Date endDate = null; 
     try { 
      obConnection = ConnectionManager.getInstance() 
        .makeNonTXConnection(); 
      obPreparedStatement = obConnection.prepareStatement(sql); 
      int length = 0; 
      if (params != null) { 
       length = params.length + 1; 
      } 
      for (int i = 1; i < (length); i++) { 
       if (params[i - 1] == null) { 
        obPreparedStatement.setNull(i, java.sql.Types.VARCHAR); 
       } else { 
        obPreparedStatement.setObject(i, params[i - 1]); 
       } 
      } 
      ArrayList result = getResult(); 
      endDate = new java.util.Date(); 

      return result; 

     } catch (SQLException se) { 
      debug.log(Debug.ERROR, "SQL Exception", se); 
      se.printStackTrace(); 
      throw new PersistenceException("PE003", createErrString(
        se.getErrorCode(), se.getMessage())); 

     } finally { 
      closeConnection(obConnection, obPreparedStatement); 
      if (endDate == null) { 
       endDate = new java.util.Date(); 
      } 
      debug.log(Debug.INFO, constructLogString(startDate, endDate, sql)); 
      startDate = endDate = null; 

     } 
    } 
} 

3. Appelez votre requête:

ArrayList result = null; 
String query = "Select * From emp where empID=?"; 

try { 
    Object[] params={1}; 
    result = queryManager.executeSQL(query,params); 
} catch (PersistenceException e) { 

} 
return result; 

4. Config context.xml (utilisation du pilote JDBC)