tomcat8和mysql连接池怎么连接mysql

在Tomcat服务器下使用连接池连接Oracle数据库
下面介绍在Tomcat服务器下使用连接池来连接数据库的操作 一:修改web.xml文件:
代码如下: &?xml version="1.0" encoding="UTF-8"?& &web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"& &display-name&project&/display-name& &welcome-file-list& &welcome-file&index.html&/welcome-file& &welcome-file&index.htm&/welcome-file& &welcome-file&index.jsp&/welcome-file& &welcome-file&default.html&/welcome-file& &welcome-file&default.htm&/welcome-file& &welcome-file&default.jsp&/welcome-file& &/welcome-file-list& &resource-ref& &description&DBConnection&/description& &res-ref-name&siniteksirm&/res-ref-name& &res-type&javax.sql.DataSource&/res-type& &res-auth&Container&/res-auth& &/resource-ref& &/web-app&
在web-app之间插入&resource-ref&这段代码。指定要是用的Resource名称。 二:修改tomcat下的context.xml文件: 在Context标签之间加入如下代码。
代码如下: &Resource name="siniteksirm" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@192.168.1.196:1521:orcl" username="paxt" password="paxt" maxActive="20" maxIdle="10" maxWait="-1" testOnBorrow="true" validationQuery="select 1 from dual"/&
三:选择Oracle的数据库驱动,加入到Tomcat的lib包中。本项目中为:Ojdbc14.jar. 四:提供一个jsp页面:
代码如下: &%@ page language="java" contentType="text/ charset=UTF-8" pageEncoding="UTF-8"%& &%@ page import="javax.naming.Context" %& &%@ page import="javax.naming.InitialContext" %& &%@ page import="java.sql.*" %& &%@ page import="javax.sql.DataSource" %& &!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"& &html& &head& &meta http-equiv="Content-Type" content="text/ charset=UTF-8"& &title&Insert title here&/title& &/head& &body& &% DataSource ds = try{ Context context = new InitialContext(); ds = (DataSource)context.lookup("java:comp/env/siniteksirm"); Connection conn = ds.getConnection(); PreparedStatement pst = conn.prepareStatement("select * from sdc_fundbase where rownum &= 2"); ResultSet rs = pst.executeQuery(); while(rs.next()){ out.println(rs.getString("fund4")); out.println("&br/&"); } if(ds != null){ out.println("数据库连接"); } }catch(Exception e){ e.printStackTrace(); out.println("数据库连接失败"); } %& &/body& &/html&
启动Tomcat,这样就可以访问页面。
顶一下(0) 踩一下(0)
热门标签:Web开发中与数据库的连接是必不可少的,而数据库连接池技术很好的优化了动态页与数据库的连接,相比单个连接数据库连接池节省了很大的资源。用一个通俗的比喻:如果一个人洗澡需花一桶水,那一百个人就要花一百桶水,太浪费了.如果都在池子里洗,洗多少个人都不怕了。
1.将MySQL的JDBC驱动复制到Tomcat安装目录里的lib文件夹下。驱动可以从MySQL官网上下载,为jar包。
2.将Tomcat的配置文件Context.xml做如下修改:
&Context path="/DBTest" docBase="DBTest"&&&&&&& debug="5" reloadable="true" crossContext="true"&&&& &!-- maxActive: Maximum number of dB connections in pool. Make sure you&&&&&&&& configure your mysqld max_connections large enough to handle&&&&&&&& all of your db connections. Set to -1 for no limit.&&&&&&&& --&&&& &!-- maxIdle: Maximum number of idle dB connections to retain in pool.&&&&&&&& Set to -1 for no limit.& See also the DBCP documentation on this&&&&&&&& and the minEvictableIdleTimeMillis configuration parameter.&&&&&&&& --&&&& &!-- maxWait: Maximum time to wait for a dB connection to become available&&&&&&&& in ms, in this example 10 seconds. An Exception is thrown if&&&&&&&& this timeout is exceeded.& Set to -1 to wait indefinitely.&&&&&&&& --&&&& &!-- username and password: MySQL dB username and password for dB connections& --&&&& &!-- driverClassName: Class name for the old mm.mysql JDBC driver is&&&&&&&& org.gjt.mm.mysql.Driver - we recommend using Connector/J though.&&&&&&&& Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.&&&&&&&& --&&&& &&& &!-- url: The JDBC connection url for connecting to your MySQL dB.&&&&&&&& The autoReconnect=true argument to the url makes sure that the&&&&&&&& mm.mysql JDBC Driver will automatically reconnect if mysqld closed the&&&&&&&& connection.& mysqld by default closes idle connections after 8 hours.&&&&&&&& --&& &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?autoReconnect=true"/&&/Context&
注意代码中红色部分:DBTest 改为自己的项目路径;TestDB改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true &。
3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在&&/web-app&&之上添加如下代码:
& &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&
上步中若修改了数据源名此步中红色部分请保持与上步中的一致。
4.代码示例:
Context initContext = new InitialContext();Context envContext& = (Context)initContext.lookup("java:/comp/env");DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");Connection conn = ds.getConnection();Statement st =ResultSet rs =st = conn.createStatement();rs = st.executeQuery(yoursql);
注意红色部分与上两步中的一致;yoursql处写你的sql代码。
通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。
阅读(...) 评论() &随笔 - 256
阅读排行榜
评论排行榜使用Tomcat连接池连接MySql数据库
使用Tomcat连接池连接MySql数据库
13:55:00来源:
对于一个简单的数据库应用,由于对于数据库的访问不是很频繁。这时可以简单地在需要访问数据库时,就新创建一个连接,用完后就关闭它,这样做也不会带来什 么明显的性能上的开销。但是对于一个复杂的数据库应用,情况就完全不同了。频繁的建立、关闭连接,会极大的减低系统的性能,因为对于连接的使用成了系统性 能的瓶颈。通过建立一个数据库连接池以及一套连接使用管理策略,使得一个数据库连接可以得到高效、安全的复用,避免了数据库连接频繁建立、关闭的开销。对于共享资源,有一个很著名的设计模式:资源池。该模式正是为了解决资源频繁分配、释放所造成的问题的。把该模式应用到数据库连接管理领域,就是建立一个数据库连接池,提供一套高效的连接分配、使用策略,最终目标是实现连接的高效、安全的复用。数据库连接池的基本原理是在内部对象池中维护一定数量的数据库连接,并对外暴露数据库连接获取和返回方法。如:外部使用者可通过getConnection 方法获取连接,使用完毕后再通过releaseConnection 方法将连接返回,注意此时连接并没有关闭,而是由连接池管理器回收,并为下一次使用做好准备。数据库连接池技术带来的优势: 资源重用 由于数据库连接得到重用,避免了频繁创建、释放连接引起的大量性能开销。在减少系统消耗的基础上,另一方面也增进了系统运行环境的平稳性(减少内存碎片以及数据库临时进程/线程的数量)。 &更快的系统响应速度 。数据库连接池在初始化过程中,往往已经创建了若干数据库连接置于池中备用。此时连接的初始化工作均已完成。对于业务请求处理而言,直接利用现有可用连接,避免了数据库连接初始化和释放过程的时间开销,从而缩减了系统整体响应时间。 &新的资源分配手段。对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术,几年钱也许还是个新鲜话题,对于目前的业务系统而 言,如果设计中还没有考虑到连接池的应用,那么…….快在设计文档中加上这部分的内容吧。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据 库资源。 统一的连接管理,避免数据库连接泄漏。在较为完备的数据库连接池实现中,可根据预先的连接占用超时设定,强制收回被占用连接。从而避免了常规数据库连接操作中可能出现的资源泄漏。1、配置Tomcat。将需要使用的JDBC驱动jar包复制在Tomcat目录下lib文件夹下,本例使用mysql-connector-java-5.1.13-bin.jar2、配置context.xml文件。打开Tomcat目录下conf文件夹下的context.xml文件, 将下面代码添加到Context标签内&Resource name=”jdbc/mysql”&auth=”Container”&type=”javax.sql.DataSource”&driverClassName=”com.mysql.jdbc.Driver”&url=”jdbc:mysql://localhost:3306/test”&username=”你的数据库用户名”&password=”你的密码”&maxActive=”100″&maxIdle=”30″&maxWait=”10000″&/&参数说明:&maxIdle,最大空闲数,指定数据库连接池中处于空闲状态的数据库连接的最大数目,0表示不受限制;&MaxActive,连接池的最大数据库连接数。设为0表示无限制。&maxWait ,最大建立连接等待时间 单位为 ms。如果超过此时间将接到异常。设为-1表示无限制。&maxActive是最大激活连接数,设置成可能的并发量,这里取值为100,表示同时最多有100个数据库连接。maxIdle是最大的空闲连接数,这里取值为30,表示即使没有数据库连接时依然可以保持30空闲的连接,而不被清除,随时处于待命状态。MaxWait是最大等待秒钟数,这里取值10000,表示10秒后超时,直到超时为止,也可取值-1,表示无限等待。3、配置web应用WEB-INF\web.xml (使用JDK1.6以后版本的可以略过此步骤)下面打开web.xml文件,在其根节点下添加数据源的引用设置。&&resource-ref&&&res-ref-name&jdbc/mysql&/res-ref-name&&&res-type&javax.sql.DataSource&/res-type&&&res-auth&Container&/res-auth&&&/resource-ref&4、配置完成后我们就可以在Java代码中访问使用连接池了import java.sql.*;&import javax.sql.*;&import javax.naming.*;&try{&InitialContext ctx = new InitialContext();&DataSource ds=(DataSurce)ctx.lookup(“java:comp/env/jdbc/mysql”);&Connection conn = ds.getConnection();&Statement stmt = conn.createStatement();&String strSql = “select * from student”;&………….&}至此,应该可以顺利的使用Tomact连接池来访问数据库了。
南通seo_南通网站开发_在Tomcat下调用Servlet不能使用默认路径,每次都要重新写入web.xml的原因?
Struts2_Demo1_HelloWorld 一个完整简单的Struts2项目演示下次自动登录
现在的位置:
& 综合 & 正文
关于tomcat上运行mysql时间过长未连接引发的wait_time问题解决
最近做一个项目,部署tomcat+mysql服务器,但是每次第二天连接的时候就会无法连接数据库,查看日志,报错 ---BEGIN NESTED EXCEPTION---
查看发现是mysql的wait_time连接超时
通过show global variables like 'wait_timeout%' 知道wait_timeout 为28800也就是8小时了,8小时之后就无法连接到mysql
hibernate本身的连接池自动连接性能不好,所以改用C3PO来作为hihibernate的连接池,
将C3PO包导入到项目中
在hibernate中配置如下语句
设置C3PO作为连接池0 --&&property name="hibernate.connection.provider_class"&org.hibernate.connection.C3P0ConnectionProvider&/property&
&!--连接池的最小连接数--&
&property name="hibernate.c3p0.min_size"&5&/property&
&!--最大连接数--&
&property name="hibernate.c3p0.max_size"&30&/property&
&!--连接超时时间--&
&property name="hibernate.c3p0.timeout"&1800&/property&
&!--statemnets缓存大小--&
&property name="hibernate.c3p0.max_statements"&100&/property&
&!--每隔多少秒检测连接是否可正常使用
&property name="hibernate.c3p0.idle_test_period"&121&/property&
&!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3--&
&property name="hibernate.c3p0.acquire_increment"&1&/property&
&property name="hibernate.c3p0.validate"&true&/property&
重启服务器即可。
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 tomcat连接mysql 配置 的文章

 

随机推荐