关于tomcat 和sqlsever2005链接池的问题

各位大家好,我的关于tomcat 和sqlsever2005链接池总是搞不定,困扰我好几天了,请大家给看一下代码如下:
项目目录META-INF下的context.xlm的如下:
<?xml version="1.0" encoding="UTF-8" ?>

WEB-INF/web.xml

maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="123"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=wxshop"/>

WEB-INF目录下的web.xml的代码内容如下:

wxshop


SQL Server Datasource
jdbc/sqlserver
javax.sql.DataSource
Container

java 链接类的代码如下:

package com.wxshop.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.wxshop.common.Constants;

/**

  • 数据库连接类
  • @author roy * */

public class DbUtil {
private static DbUtil db = null;
private static DataSource ds = null;

private DbUtil()
{

}

/**
 * 获取数据库连接对象的实例
 * @return
 */
public static DbUtil getInstance(){
    if(db == null)
    {
        db = new DbUtil();
    }
    return db;
}

    /**
     * 获得数据库连接
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Context initCtx = new InitialContext();
            if (ds == null) {
                ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/"+Constants.DB_POOL_NAME);
                if(null != ds)
                {
                    System.out.println("获得数据源成功!");
                }else
                {
                    System.out.println("获得数据源失败!");
                }
            }

            if(conn == null)
            {
                conn  = ds.getConnection();
                System.out.println("获取数据库连接成功!");
            }


        } catch (NamingException e) {
            System.out.println("【DbUtil-getConnection()】命名错误!");
            e.printStackTrace();

        }catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println("【DbUtil-getConnection()】获取数据库连接发生错误!");
        }
        return conn;
    }


   /**
    * 关闭数据库连接
    * @param conn
    * @param stmt
    * @param rs
    * @return
    */
    public static boolean  close(Connection conn,Statement stmt,ResultSet rs)
    {
        boolean isClose=false;

        //关闭ResultSet
        try{
            if(rs != null){
                rs.close();
                rs=null;
            }
        }catch(SQLException ex)
        {
            rs=null;
            isClose=false;
            System.out.println(ex.getMessage());
            System.out.println("关闭结果集发生错误!");
        }

        //关闭Statement
        try{
            if(stmt!=null){
                stmt.close();
                stmt=null;
            }
        }catch(SQLException ex)
        {
            stmt=null;
            isClose=false;
            System.out.println(ex.getMessage());
            System.out.println("关闭stmt发生错误!");
        }

        //关闭Connection
        try{
            if(conn!=null){
                conn.close();
                conn=null;
            }
        }catch(SQLException ex)
        {
            conn=null;
            isClose=false;
            System.out.println(ex.getMessage());
            System.out.println("关闭数据库连接发生错误!");
        }
        return isClose;
    }

}
还有一个java类的代码如下:
package com.wxshop.common;

/**

  • @author Administrator
  • 2012-09
    /
    public class Constants {
    /
    ** 缩略字段个数 */
    public static final int CONTENT_PRE_NUMS = 168;
    public static final String DEFAULT_ADVISORY_DOMAIN_CLASS = "747";

    public static final String path="/wxshop";

    public static final String DB_POOL_NAME = "wxshop";

    public static final String CSS_NAME = "css";

    public static final int MAX_ROW = 14;

    public static final int MAX_ROW_NORAML = 10;

    public static final int MAX_ROW_ZYX = 14;

    public static final int COUNT_ROW = 12;

    public static final String dirverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static final String dburl="jdbc:sqlserver://localhost:1433;DatabaseName=wxshop?&useUnicode=true&characterEncoding=UTF-8";

    public static final String userName="sa";

    public static final String userPwd="123";

    public static final String LOTSYS_CHARSET = "UTF-8";

    public static final String LOTSYS_VERSION = "1.0";

    public static final int HSEDU_RELEASE = 20120922;

    public static final String UPLOAD_PATH="upload";

    //邮件
    public static final String SMTP_HOST = "mail.126.com";
    public static final String MAIL_USER = "sun@126.com";
    public static final String MAIL_PWD = "111111";

    /**

    • 日期格式 */ public static final String DATE_FORMAT = "yyyy-mm-dd";

    public static final String MONTH_DAY = "M/d";

    public static final String DATE_TIME_FULL = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_TIME_NO_SECOND = "yyyy-MM-dd HH:mm";

    public static final String DATE_TIME_FULL_2 = "yyMMddHHmmss";

    public static final String DATE_PART = "yyMMdd";

    public static final String DATE_PART_2 = "yyyy-MM-dd";

    public static final String YEAR = "yyyy";

    public static final String MONTH = "MM";

    public static final String MONTH_2 = "M";

    public static final String DAY = "dd";

    public static final String DAY_2 = "d";

    public static final String HOUR = "H";

    public static final String MINUTE = "m";

    public static final String MARK_NULL = "";
    }
    测试链接池jsp的代码如下:
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <%@ page import="com.wxshop.db.DbUtil,java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



测试数据库连接池


<%
//获取连接对象
Connection conn = DbUtil.getInstance().getConnection;
//获取语句执行对象
Statement stmt = conn.createStatement();
String sql = "select * from t_user";

//执行语句,获取结果集对象
ResultSet rs = stmt.executeQuery(sql);

String id = "";
String username = "";
String password = "";

out.println("<table border=\"1\" cellspacing=\"0\" cellspadding=\"0\">");
out.println("<tr><td>编号</td><td>用户名称</td><td>用户密码</td>");
while(rs.next())
{
    id = rs.getString(1);
    username = rs.getString("username");
    password = rs.getString("password");
    out.println("<tr><td>");
    out.println(id+"</td><td>"+username+"</td><td>"+password);
    out.println("</td></tr>");

}
out.println("</table>");

DbUtil.close(conn, stmt, rs);

%>


劳驾热心的兄弟姐妹们了。

2个回答

没有人可以帮忙吗?自己顶一下。帮我解决了,给您发红包!!

你报告什么错误?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐