• java 基于tomcat的数据源案例


    1、在context中定义数据源

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <Context path="/testDs" reloadable="true" 
     3 docBase="D:eclipse-jee-mars-2-win32-x86_64work_duct-support	estDsase" 
     4 workDir="D:eclipse-jee-mars-2-win32-x86_64work_duct-support	estDswork" >
     5     <Resource 
     6         name="jdbc/mysql" 
     7         auth="Container"
     8         type="javax.sql.DataSource"
     9         maxActive="100"
    10         maxIdle="30"
    11         maxWait="10000"
    12         username="root"
    13         password="Isd@2014"
    14         driverClassName="com.mysql.jdbc.Driver"
    15         url="jdbc:mysql://172.16.10.163:3306/zhicall_hcrm?useUnicode=true&amp;characterEncoding=utf8" />
    16 </Context>
    17 
    18 <!-- 
    19 1、Resource必须配合Context配置,
    20 2、不同的部署方式,配置的Context不同,Resource配置的地方也不同,
    21 3、如果是在Catalinalocalhost下配置xml(如:testDs.xml),则xml配置参考以上配置
    22 4、把自个的WebAppProject里的/META-INF/context.xml的内容复制到$CATALINA_BASE/conf/context.xml里或者$CATALINA_BASE/conf/server.xml
    23 5、直接将自个的WebAppProject打包成WAR文件拷贝到$CATALINA_BASE/webapps目录里,tomcat启动后,自动会将WebAppProject里的/META-INF/context.xml文件内容复制到$CATALINA_BASE/conf/context.xml里
    24 6、conf的context.xml 的 <Context></Context>也可配置
    25 7、可在<CATALINA_HOME>/conf/server.xml 文件中的对应<Host>元素中如上配置<Resource>子元素以供Tomcat容器内的多个Web应用使用 
    26 -->

    2、在web.xml中配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
     6     <display-name>testDs</display-name>
     7     <servlet>
     8         <servlet-name>testServlet</servlet-name>
     9         <servlet-class>test_servlet_package.test_servlet</servlet-class>
    10     </servlet>
    11     <servlet-mapping>
    12         <servlet-name>testServlet</servlet-name>
    13         <url-pattern>/testServlet</url-pattern>
    14     </servlet-mapping>
    15     <resource-ref>
    16         <description>DB Connection</description>
    17         <res-ref-name>jdbc/mysql</res-ref-name>
    18         <res-type>javax.sql.DataSource</res-type>
    19         <res-auth>Container</res-auth>
    20     </resource-ref>
    21 </web-app>

    3、在servlet中使用

     1 package test_servlet_package;
     2 import java.io.IOException;
     3 import java.sql.Connection;
     4 import java.sql.ResultSet;
     5 import java.sql.Statement;
     6 import javax.naming.InitialContext;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 import javax.sql.DataSource;
    12 public class test_servlet extends HttpServlet {
    13 
    14     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    15         DataSource ds = null;
    16         try {
    17             InitialContext ctx = new InitialContext();
    18             ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
    19             Connection conn = ds.getConnection();
    20             Statement stmt = conn.createStatement();
    21             String strSql = "select * from dim_code";
    22             ResultSet rs = stmt.executeQuery(strSql);
    23             while (rs.next()) {
    24                 System.out.println("name:" + rs.getString(1));
    25             }
    26             rs.close();
    27             stmt.close();
    28             conn.close();
    29         } catch (Exception ex) {
    30             ex.printStackTrace();
    31         }
    32     }
    33 }

    总结:案例只是介绍一个数据源的简单使用、用于理解、方便后期调查使用

  • 相关阅读:
    Vocabulary Recitation 2020/05/06
    java set TreeSet详解
    vue快速入门~必备基础知识(一)下~过滤器
    vue快速入门~必备基础知识(一)上
    vue入门(二)基于前面的基础的一个小Demo
    Annotation注释详解~
    泛型~详解~
    Collections工具类详解
    TreeMap实现类和SortMap接口
    Map集合-主要HashMap和Hashtable
  • 原文地址:https://www.cnblogs.com/kongxianghao/p/6961695.html
Copyright © 2020-2023  润新知