java 基于tomcat的数据源案例
2024-09-02 09:10:25
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&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();
}
}
}
总结:案例只是介绍一个数据源的简单使用、用于理解、方便后期调查使用
最新文章
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- 使用WindowsPE破解管理员密码
- Unsplash.it - 实用的图片占位符,支持个性化设置
- specify a file path to store the seed
- acm常用术语
- HTML标签的默认样式
- Android Service学习之本地服务
- UDT: Breaking the Data Transfer Bottleneck
- 敏捷冲刺(Beta版本)
- Unity使用C++作为游戏逻辑脚本的研究
- django的流程和命令行工具
- setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)
- es6开发环境搭建,babel 将es6转化成es5
- 译:为什么使用 NoSQL 数据库
- struts2必备jar包(2.1.6版本)
- SWPU-ACM集训队周赛之组队赛(3-11) C题题解
- android.view.WindowManager$BadTokenException: Unable to add window
- windows 开启热点的命令行工具
- Java的学习04
- VC++对话框中加状态栏