Tomcat - DBCP 配置
2015-09-04 10:49
561 查看
1. Database configuration
Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password.
2. Context configuration
Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context.
3. web.xml configuration
Now create a
4. Test code
Now create a simple
Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password.
GRANT ALL PRIVILEGES ON *.* TO javauser@localhost IDENTIFIED BY 'javadude' WITH GRANT OPTION; CREATE DATABASE javatest; USE javatest; CREATE TABLE testdata ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, foo VARCHAR(25), bar INT ); INSERT INTO testdata VALUES(NULL, 'hello', 12345);
2. Context configuration
Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context.
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest"/>
3. web.xml configuration
Now create a
WEB-INF/web.xmlfor this test application.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
4. Test code
Now create a simple
test.jsppage for use later.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <sql:query var="rs" dataSource="jdbc/TestDB"> select id, foo, bar from testdata </sql:query> <html> <head> <title>DB Test</title> </head> <body> <h2>Results</h2> <c:forEach var="row" items="${rs.rows}"> Foo ${row.foo}<br /> Bar ${row.bar}<br /> </c:forEach> </body> </html>
相关文章推荐
- MyEclipse下Tomcat无法部署项目 finish按钮无法点击
- Tomcat的session集群和session服务器
- console监控tomcat 的JVM内存(远程、linux、windows)
- Tomcat6配置日志
- CentOS5下配置JDK1.6+TOMCAT6
- 高性能Tomcat:漫谈行走在sendfile之上的Tomcat
- [Ubuntu下java开发环境搭建]Ubuntu14.04+jdk+myeclipse+tomcat及问题总结
- nagios插件之监控多个tomcat日志
- IDEA 14 tomcat
- tomcat中文乱码解决方案
- Tomcat - JNDI 配置
- 基于apache实现tomcat集群的负载均衡
- Eclipse中web项目部署至Tomcat步骤
- UI:tomcat(说话小程序)、相框动画、UISgmentcontrol、UISwitch
- tomcat异常之org.apache.catalina.LifecycleException:
- Windows下(Windows 10 & Windows Server 2012)部署Tomcat(不定时更新)
- Tomcat剖析(一):一个简单的Web服务器
- eclipse和tomcat整合之后每次发布server.xml被修改(转)
- tomcat 优化处理
- Tomcat启动报Error listenerStart错误