• tomcat6下配置连接池2


    以前配置过一次,感觉不是太好用,现在又试了一次,效果还不错,记录一下,以备后用

    1.WEB项目的META-INF目录(不是WEB-INF)下新建XML配置文件context.xml,内容如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <Context path="/jrtSrv" docBase="jrtSrv" reloadable="true" crossContext="true"> 

        <Resource name="jdbc/MSSql" auth="Container" type="javax.sql.DataSource"   
        maxActive="100"   
        maxIdle="30" 
        maxWait="5000" 
        username="sa" 
        validationQuery="select 1"
        password="youpassword" 
        driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
        url="jdbc:microsoft:sqlserver://192.168.56.101:1433;DatabaseName=MYDB"/> 
    </Context> 

    注意validationQuery="select 1" 这句,非常重要,对于网络异常或者数据库重启,可以对connection 进行重新建立

    2.在项目WEB-INF下的WEB.xml中添加如下内容:

     <resource-ref>
      <description>DBConnection</description>
      <res-ref-name>jdbc/MSSql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
     </resource-ref>

    3.OK 收工,写个t.jsp页面测试下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <%@page contentType="text/html; charset=GB2312"%>
    <%@page import="java.sql.*"%>
    <%@page import="javax.naming.*"%>
    <%@page import="javax.sql.DataSource"%>

    <%
     String path = request.getContextPath();
     String basePath = request.getScheme() + "://"
       + request.getServerName() + ":" + request.getServerPort()
       + path + "/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">

    <title>My JSP 'MyJsp.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

    </head>

    <body>

     <%
      String tablename = "t_beds";
      String sqlstr;
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;
      Context ctx = null;
      try {
       ctx = new InitialContext();
       DataSource ds = (DataSource) ctx
         .lookup("java:comp/env/jdbc/MSSql");

       con = ds.getConnection();

       stmt = con.createStatement();
       sqlstr = "select * from " + tablename;
       rs = stmt.executeQuery(sqlstr);
       ResultSetMetaData rsmd = rs.getMetaData();//获取元数据
       int j = 0;
       j = rsmd.getColumnCount();//获得结果集的行数
       for (int k = 0; k < j; k++) {
        out.print(rsmd.getColumnName(k + 1));//显示表中字段属性
        out.print("\t");
       }
       out.print("<br>");
       /*3.读取查询结果 */
       while (rs.next()) {

        out.print(rs.getString("id"));
        out.print("\t");
        out.println(rs.getString("stateId"));
        out.print("\t");
        out.print(rs.getString("updateTime"));
        out.print("\t");
        out.print("<br>");
       }
      }

      /*4.异常处理 */
      catch (NamingException e1) {
       out.println(e1.toString());
       out.println("驱动程序没有找到!");
      } catch (SQLException e2) {
       out.println(e2.toString());
       out.println("数据库异常!");
      }

      /*5.关闭数据库 */
      finally {
       try {
        if (rs != null)
         rs.close();
        if (stmt != null)
         stmt.close();
        if (con != null)
         con.close();

       } catch (Exception e) {
        out.println(e.toString());

       }
      }
     %>
    </body>

    </html>

    ----------------------------------------------------------

    参考文章:

    http://blog.sina.com.cn/s/blog_570f6d940100sor4.html

    还有一篇百度文库上的文章,找不到出处了

  • 相关阅读:
    The user specified as a definer ('root'@'%') does not exist
    Linux安装卸载查看vsftpd
    【数据分析】算法+Echarts小练
    【leecode】小练习(简单8题)
    路飞学城Python-Day171
    【路飞学城Day170】算法小结
    【路飞学城第七模块考核】
    【leecode】独特的电子邮件地址
    【leecode】宝石与石头
    路飞学城Python-Day153
  • 原文地址:https://www.cnblogs.com/ventry/p/2646488.html
Copyright © 2020-2023  润新知