一定要用JAVA的API自己写吗?
我建议用服务器自带的比较好;真的
以MYSQL数据库为例;按如下配置就能成功用上连接池:
3、  使用Tomcat的Web管理应用配置数据源   启动Tomcat服务器,打开浏览器,输入http://localhost:8080/admin/(其中localhost可能是一台机器的IP或是服务器名称),进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,
    选择Resources-Data sources进入配置数据源界面,选择Data Source Actions ->选择Create New Data Source,进入配置详细信息界面,内容如下:            JNDI Name:    jdbc/mysql
            Data Source URL:  jdbc:mysql://192.168.0.16/SUBRDB  
            JDBC Driver Class:   org.gjt.mm.mysql.Driver 
            User Name:    root
            Password:     ********
            Max. Active Connections:    4
            Max. Idle Connections:    2
            Max. Wait for Connection:    500
            Validation Query:   要求输入的JNDI Name等信息,其中除了JDBC DriverClass之外,其他的可以根据你的需要填写。比如Data Source URL的内容可能是:jdbc:mysql:// IP或是名称/DataBaseName,其中DataBaseName是你的数据库名称,IP是你的数据库的所在的服务器的IP或是名称。最后点击Save->Commit Change.这样你的数据源的基本资料配置一半了。4、  web.xml和%TOMCAT_HOME%\conf\Catalina\localhost下对应你的引用的配置文件修改通过文件夹导航到%TOMCAT_HOME%\conf,打开web.xml,在</web-app>的前面添加以下内容:      <resource-ref>    <description>DB Connection</description>    <res-ref-name>jdbc/mysql</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>      </resource-ref>    注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。 通过文件夹导航到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web应用对应的.xml文件,如    ROOT.xml,并在此文件的下添入代码:<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/> 到这里,配置工作就基本完成了。5、  其他注意事项别忘了JDBC驱动程序mysql-connector-java-3.0.9-stable-bin.jar一定要放置到Tomcat的对应目录,你的JDBC驱动可能版比笔者高,不过只要能与所使用的MySql对应就可以了,因为我发现版本太低的JDBC驱动不能支持4.0.*版本的MySQL数据库,建议放置在%TOMCAT_HOME%\common\lib和应用的WEB-INF\lib下。两者有什么不同呢?其实一看就明白了,common\li是所有的应用都可以使用的库文件位置。重启你的Tomcat服务。 6、  编写测试代码在应用的目录下建立一个Test.jsp文件,代码如下:<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd"><%@ page import="java.sql.*"%><%@ page import="javax.sql.*"%><%@ page import="javax.naming.*"%><%@ page session="false" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title></title><%    out.print("我的测试开始");   DataSource ds = null;   try{   InitialContext ctx=new InitialContext();   ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");   Connection conn = ds.getConnection();   Statement stmt = conn.createStatement();     //提示:users必须是数据库已有的表,//这里的数据库前文提及的Data Source URL配置里包含的数据库。   String strSql = " select * from users";   ResultSet rs = stmt.executeQuery(strSql);   while(rs.next()){      out.print(rs.getString(1));                      }out.print("我的测试结束");   }   catch(Exception ex){       out.print(“出现例外,信息是:”+ex.getMessage());    ex.printStackTrace();   }%></head><body></body></html>运行结果:我的测试开始12345678我的测试结束,因为我的rs.getString(1) 在数据库就是存放12345678

解决方案 »

  1.   

    如何非想写代码自己实现的话就参考下下面的代码吧:
    具体实现DBConnectionManager.java程序清单如下:001 import java.io.*;
    002 import java.sql.*;
    003 import java.util.*;
    004 import java.util.Date;
    005
    006 /**
    007 * 管理类DBConnectionManager支持对一个或多个由属性文件定义的数据库连接
    008 * 池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.
    009 */
    010 public class DBConnectionManager {
    011 static private DBConnectionManager instance; // 唯一实例
    012 static private int clients;
    013
    014 private Vector drivers = new Vector();
    015 private PrintWriter log;
    016 private Hashtable pools = new Hashtable();
    017
    018 /**
    019 * 返回唯一实例.如果是第一次调用此方法,则创建实例
    020 *
    021 * @return DBConnectionManager 唯一实例
    022 */
    023 static synchronized public DBConnectionManager getInstance() {
    024 if (instance == null) {
    025 instance = new DBConnectionManager();
    026 }
    027 clients++;
    028 return instance;
    029 }
    030
    031 /**
    032 * 建构函数私有以防止其它对象创建本类实例
    033 */
    034 private DBConnectionManager() {
    035 init();
    036 }
    037
    038 /**
    039 * 将连接对象返回给由名字指定的连接池
    040 *
    041 * @param name 在属性文件中定义的连接池名字
    042 * @param con 连接对象
    043 */
    044 public void freeConnection(String name, Connection con) {
    045 DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    046 if (pool != null) {
    047 pool.freeConnection(con);
    048 }
    049 }
    050
    051 /**
    052 * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
    053 * 限制,则创建并返回新连接
    054 *
    055 * @param name 在属性文件中定义的连接池名字
    056 * @return Connection 可用连接或null
    057 */
    058 public Connection getConnection(String name) {
    059 DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    060 if (pool != null) {
    061 return pool.getConnection();
    062 }
    063 return null;
    064 }
    065
    066 /**
    067 * 获得一个可用连接.若没有可用连接,且已有连接数小于最大连接数限制,
    068 * 则创建并返回新连接.否则,在指定的时间内等待其它线程释放连接.
    069 *
    070 * @param name 连接池名字
    071 * @param time 以毫秒计的等待时间
    072 * @return Connection 可用连接或null
    073 */
    074 public Connection getConnection(String name, long time) {
    075 DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    076 if (pool != null) {
    077 return pool.getConnection(time);
    078 }
    079 return null;
    080 }
    081
    082 /**
    083 * 关闭所有连接,撤销驱动程序的注册
    084 */
    085 public synchronized void release() {
    086 // 等待直到最后一个客户程序调用
    087 if (--clients != 0) {
    088 return;
    089 }
    090
    091 Enumeration allPools = pools.elements();
    092 while (allPools.hasMoreElements()) {
    093 DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
    094 pool.release();
    095 }
    096 Enumeration allDrivers = drivers.elements();
    097 while (allDrivers.hasMoreElements()) {
    098 Driver driver = (Driver) allDrivers.nextElement();
    099 try {
    100 DriverManager.deregisterDriver(driver);
    101 log("撤销JDBC驱动程序 " + driver.getClass().getName()+"的注册");
    102 }
    103 catch (SQLException e) {
    104 log(e, "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());
    105 }
    106 }
    107 }
    108
    109 /**
    110 * 根据指定属性创建连接池实例.
    111 *
    112 * @param props 连接池属性
    113 */
    114 private void createPools(Properties props) {
    115 Enumeration propNames = props.propertyNames();
    116 while (propNames.hasMoreElements()) {
    117 String name = (String) propNames.nextElement();
    118 if (name.endsWith(".url")) {
    119 String poolName = name.substring(0, name.lastIndexOf("."));
    120 String url = props.getProperty(poolName + ".url");
    121 if (url == null) {
    122 log("没有为连接池" + poolName + "指定URL");
    123 continue;
    124 }
    125 String user = props.getProperty(poolName + ".user");
    126 String password = props.getProperty(poolName + ".password");
    127 String maxconn = props.getProperty(poolName + ".maxconn", "0");
    128 int max;
    129 try {
    130 max = Integer.valueOf(maxconn).intValue();
    131 }
    132 catch (NumberFormatException e) {
    133 log("错误的最大连接数限制: " + maxconn + " .连接池: " + poolName);
    134 max = 0;
    135 }
    136 DBConnectionPool pool =
    137 new DBConnectionPool(poolName, url, user, password, max);
    138 pools.put(poolName, pool);
    139 log("成功创建连接池" + poolName);
    140 }
    141 }
    142 }
    143
    144 /**
    145 * 读取属性完成初始化
    146 */
    147 private void init() {
    148 InputStream is = getClass().getResourceAsStream("/db.properties");
    149 Properties dbProps = new Properties();
    150 try {
    151 dbProps.load(is);
    152 }
    153 catch (Exception e) {
    154 System.err.println("不能读取属性文件. " +
    155 "请确保db.properties在CLASSPATH指定的路径中");
    156 return;
    157 }
    158 String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
    159 try {
    160 log = new PrintWriter(new FileWriter(logFile, true), true);
    161 }
    162 catch (IOException e) {
    163 System.err.println("无法打开日志文件: " + logFile);
    164 log = new PrintWriter(System.err);
    165 }
    166 loadDrivers(dbProps);
    167 createPools(dbProps);
    168 }
    169
    170 /**
    171 * 装载和注册所有JDBC驱动程序
    172 *
    173 * @param props 属性
    174 */
    175 private void loadDrivers(Properties props) {
    176 String driverClasses = props.getProperty("drivers");
    177 StringTokenizer st = new StringTokenizer(driverClasses);
    178 while (st.hasMoreElements()) {
    179 String driverClassName = st.nextToken().trim();
    180 try {
    181 Driver driver = (Driver)
    182 Class.forName(driverClassName).newInstance();
    183 DriverManager.registerDriver(driver);
    184 drivers.addElement(driver);
    185 log("成功注册JDBC驱动程序" + driverClassName);
    186 }
    187 catch (Exception e) {
    188 log("无法注册JDBC驱动程序: " +
    189 driverClassName + ", 错误: " + e);
    190 }
    191 }
    192 }
    193
    194 /**
    195 * 将文本信息写入日志文件
    196 */
    197 private void log(String msg) {
    198 log.println(new Date() + ": " + msg);
    199 }
    200
    201 /**
    202 * 将文本信息与异常写入日志文件
    203 */
    204 private void log(Throwable e, String msg) {
    205 log.println(new Date() + ": " + msg);
    206 e.printStackTrace(log);
    207 }
    208
    209 /**
    210 * 此内部类定义了一个连接池.它能够根据要求创建新连接,直到预定的最
    211 * 大连接数为止.在返回连接给客户程序之前,它能够验证连接的有效性.
    212 */
    213 class DBConnectionPool {
    214 private int checkedOut;
    215 private Vector freeConnections = new Vector();
    216 private int maxConn;
    217 private String name;
    218 private String password;
    219 private String URL;
    220 private String user;
    221
    222 /**
    223 * 创建新的连接池
    224 *
    225 * @param name 连接池名字
    226 * @param URL 数据库的JDBC URL
    227 * @param user 数据库帐号,或 null
    228 * @param password 密码,或 null
    229 * @param maxConn 此连接池允许建立的最大连接数
    230 */
    231 public DBConnectionPool(String name, String URL, String user, String password,
    232 int maxConn) {
    233 this.name = name;
    234 this.URL = URL;
    235 this.user = user;
    236 this.password = password;
    237 this.maxConn = maxConn;
    238 }
    239
    240 /**
    241 * 将不再使用的连接返回给连接池
    242 *
    243 * @param con 客户程序释放的连接
    244 */
    245 public synchronized void freeConnection(Connection con) {
    246 // 将指定连接加入到向量末尾
    247 freeConnections.addElement(con);
    248 checkedOut--;
    249 notifyAll();
    250 }
    251
      

  2.   

    252 /**
    253 * 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接
    254 * 数限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之,
    255 * 然后递归调用自己以尝试新的可用连接.
    256 */
    257 public synchronized Connection getConnection() {
    258 Connection con = null;
    259 if (freeConnections.size() > 0) {
    260 // 获取向量中第一个可用连接
    261 con = (Connection) freeConnections.firstElement();
    262 freeConnections.removeElementAt(0);
    263 try {
    264 if (con.isClosed()) {
    265 log("从连接池" + name+"删除一个无效连接");
    266 // 递归调用自己,尝试再次获取可用连接
    267 con = getConnection();
    268 }
    269 }
    270 catch (SQLException e) {
    271 log("从连接池" + name+"删除一个无效连接");
    272 // 递归调用自己,尝试再次获取可用连接
    273 con = getConnection();
    274 }
    275 }
    276 else if (maxConn == 0 || checkedOut < maxConn) {
    277 con = newConnection();
    278 }
    279 if (con != null) {
    280 checkedOut++;
    281 }
    282 return con;
    283 }
    284
    285 /**
    286 * 从连接池获取可用连接.可以指定客户程序能够等待的最长时间
    287 * 参见前一个getConnection()方法.
    288 *
    289 * @param timeout 以毫秒计的等待时间限制
    290 */
    291 public synchronized Connection getConnection(long timeout) {
    292 long startTime = new Date().getTime();
    293 Connection con;
    294 while ((con = getConnection()) == null) {
    295 try {
    296 wait(timeout);
    297 }
    298 catch (InterruptedException e) {}
    299 if ((new Date().getTime() - startTime) >= timeout) {
    300 // wait()返回的原因是超时
    301 return null;
    302 }
    303 }
    304 return con;
    305 }
    306
    307 /**
    308 * 关闭所有连接
    309 */
    310 public synchronized void release() {
    311 Enumeration allConnections = freeConnections.elements();
    312 while (allConnections.hasMoreElements()) {
    313 Connection con = (Connection) allConnections.nextElement();
    314 try {
    315 con.close();
    316 log("关闭连接池" + name+"中的一个连接");
    317 }
    318 catch (SQLException e) {
    319 log(e, "无法关闭连接池" + name+"中的连接");
    320 }
    321 }
    322 freeConnections.removeAllElements();
    323 }
    324
    325 /**
    326 * 创建新的连接
    327 */
    328 private Connection newConnection() {
    329 Connection con = null;
    330 try {
    331 if (user == null) {
    332 con = DriverManager.getConnection(URL);
    333 }
    334 else {
    335 con = DriverManager.getConnection(URL, user, password);
    336 }
    337 log("连接池" + name+"创建一个新的连接");
    338 }
    339 catch (SQLException e) {
    340 log(e, "无法创建下列URL的连接: " + URL);
    341 return null;
    342 }
    343 return con;
    344 }
    345 }
    346 }推荐你用上面的方法
      

  3.   

    谢谢谢谢!谢谢大家,今天保证结贴!待我看一下代码,我还剩一百分,这个问题解决了全送。赶鸭子上架,我本来是用.NET的,JAVA就JSP和CS结构编程比较熟,领导白痴,要连接池自己又不会搞。