2009-11-05 2 views
0

Я пытаюсь реализовать пул соединений в java.I написал один Java-код и один файл jsp. Когда я запустил его на tomcat6.0.20, я получил следующую ошибку.Пул соединений в Java

javax.servlet.ServletException: Невозможно создать драйвер JDBC класса '' для подключения URL 'нулевой'

на этой линии соединения сопп = ds.getConnection();

+0

Вы пытаетесь реализовать пул соединений, или просто настроить его? – skaffman

ответ

-1

Вы настроили источник данных?

+0

Да, я настроил файл context.xml Tomcat.I написал там информацию о ресурсах. Я создал один web.xml, в котором я ссылался на ресурс declare в контексте .xml –

0

Следуйте инструкциям.

1. Создать ConnectionManager класс:

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. Создать 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. Вызов ваш запрос:

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 (использование JDBC Driver)

Смежные вопросы