最简单的用
局部数据库连接池
<Resource name="jdbc/mydb" type="javax.sql.DataSource" password="mypwd" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000"
validationQuery="select1" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"
maxActive="4" />

解决方案 »

  1.   

    大致
      Context   env=null;   
      try{   
          env=(Context)   new   InitialContext().lookup("java:comp/env");   
          pool=(DataSource)env.lookup("jdbc/mydb");   
          if(pool==null){   
              throw   new   ServletException("mydb   is   an   unknow   DataSource");}   
          }catch(NamingException   ne) {
              throw   new   ServletException(ne.getMessage());}   
      }   
      Connection conn=pool.getConnection();   
      

  2.   

    public class access_db

    //数据库用户名
    String userName="root";
    //数据库密码
    String userPassword="123";
        //数据库的URL,包括连接数据库所使用的编码格式
    String url="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8";
        //定义一个连接对象
    Connection dbcon;   
        Statement stmt;
    ResultSet rs;
        
        /**
    *@初始化操作,包括给变量赋初值和连接数据库
    */
        public access_db()
        {
        //初始化参数值
        stmt = null;
    rs = null;
    //连接数据库
        try
    {
    //声明所用的类包
    Class.forName("org.gjt.mm.mysql.Driver");
    //获得数据库的连接对象
    dbcon= DriverManager.getConnection(url,userName,userPassword);

    }
    catch(SQLException ex)
    {
    //打印出异常信息
    System.out.println(ex.toString());
    }
    catch(ClassNotFoundException ex)
    {
    //打印出异常信息
    System.out.println(ex.toString());
    }
        }
    }