1、在context中定义数据源

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

2、在web.xml中配置

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>testDs</display-name>
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>test_servlet_package.test_servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

3、在servlet中使用

 package test_servlet_package;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
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 test_servlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
DataSource ds = null;
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from dim_code";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
System.out.println("name:" + rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

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

最新文章

  1. NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
  2. 使用WindowsPE破解管理员密码
  3. Unsplash.it - 实用的图片占位符,支持个性化设置
  4. specify a file path to store the seed
  5. acm常用术语
  6. HTML标签的默认样式
  7. Android Service学习之本地服务
  8. UDT: Breaking the Data Transfer Bottleneck
  9. 敏捷冲刺(Beta版本)
  10. Unity使用C++作为游戏逻辑脚本的研究
  11. django的流程和命令行工具
  12. setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)
  13. es6开发环境搭建,babel 将es6转化成es5
  14. 译:为什么使用 NoSQL 数据库
  15. struts2必备jar包(2.1.6版本)
  16. SWPU-ACM集训队周赛之组队赛(3-11) C题题解
  17. android.view.WindowManager$BadTokenException: Unable to add window
  18. windows 开启热点的命令行工具
  19. Java的学习04
  20. VC++对话框中加状态栏

热门文章

  1. log4j教程 10、PatternLayout
  2. python 读取CSV文件 中文乱码
  3. C++ 模板应用 实现一个Queue 队列
  4. Angular 学习笔记——自定义标签
  5. 单片机小白学步系列(十三) 点亮第一个LED——好的開始,成功的一半
  6. Time.fixedDeltaTime 固定增量时间
  7. Transform.Translate 平移
  8. LeetCode题目: Remove Duplicate Letters
  9. Maven学习小结
  10. 【转】GitHub入门详细讲解