zoukankan      html  css  js  c++  java
  • [javaEE] tomcat内部连接池

    META-INF的目录下,新建context.xml

    在程序中获取数据源,通过jndi,这个jndi必须在Servlet中才能获取,并且需要配置web.xml使servlet一启动就拿到数据源

    context.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Context>
        <Resource
        name="mySource"
        type="javax.sql.DataSource"
        username="root"
        password="root"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql:///java"
        maxActive="8"
        maxIdle="4"
        />
    </Context>
    package com.tsh.web;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;
    
    
    
    
    /**
     */
    public class DataSourceTest extends HttpServlet {
           
        /**
         */
        public DataSourceTest() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
        /**
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        }
        /**
         * 创建后就会启动
         */
        @Override
        public void init() throws ServletException {
            try {
                //注意导包javax.naming.Context;
                Context context= new InitialContext();
                Context jndi=(Context) context.lookup("java:comp/env");
                DataSource source =(DataSource) jndi.lookup("mySource");
                //注意导包 java.sql.Connection;
                Connection conn=source.getConnection();
                //获取传输器对象
                Statement statement=conn.createStatement();
                //获取结果集对象
                ResultSet resultSet=statement.executeQuery("select * from user");
                //遍历
                while(resultSet.next()){
                    String username=resultSet.getString("username");
                    System.out.println(username);
                }
                //关闭资源
                resultSet.close();
                statement.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    web.xml

        <servlet>
            <servlet-name>DataSourceTest</servlet-name>
            <servlet-class>com.tsh.web.DataSourceTest</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>DataSourceTest</servlet-name>
            <url-pattern>/Servlet/DataSourceTest</url-pattern>
    
        </servlet-mapping>
  • 相关阅读:
    PeaZip 4.7.3 发布,跨平台压缩工具
    Liferea 1.8.10 发布,Linux的RSS阅读
    PyParticles 0.2.1 发布,粒子模拟工具箱
    微软公布 Windows Phone 8 多项新特性
    SecureCRT 7.0.2 发布,支持 Windows 8 系统
    Qore PostgreSQL Module 2.0 发布
    libquickmail 0.1.6 发布,邮件发送包
    Mobile Lua 6.5 发布,MoSync 的 Lua 移植版本
    企业用户缘何抓住 Windows XP 不放
    Knockout.js 2.2 发布,JavaScript UI 库
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5655607.html
Copyright © 2011-2022 走看看