[javaEE] tomcat內部串連池,javaeetomcat

來源:互聯網
上載者:User

[javaEE] tomcat內部串連池,javaeetomcat

在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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.