2009-11-05 100 views
0

我想在java.I中實現連接池寫了一個java代碼和一個jsp文件。當我在tomcat6.0.20上運行它時,出現以下錯誤。Java中的連接池

javax.servlet.ServletException:不能 '的連接網址 '在這一行 連接康恩= ds.getConnection空'

()創建一流的' JDBC驅動程序;

+0

你想實現連接池,或者只是配置呢? – skaffman 2010-08-23 21:34:08

回答

-1

你配置了數據源嗎?

+0

是的,我配置了tomcat的context.xml文件。我已經在那裏寫了資源信息。我創建了一個web.xml,其中我引用了context.xml中的資源聲明 – 2009-11-05 13:25:19

0

請按照步驟操作。

創建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; 

     } 
    } 
} 

電話查詢:

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.配置context.xml(使用JDBC驅動程序)