|
| « | June 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:7 评论数量:2 留言数量:-1 访问次数:48295 建立时间:2009年3月9日 |

| |
|
JDBC数据源和连接池 心得体会, 软件技术, 电脑与网络
wendyneil 发表于 2009/7/29 10:48:42 |
|
//Source:
//《精通Java Web整合开发》 刘斌 编著 电子工业出版社
// MySQL 参考手册
在JSP或者Servlet中使用JDBC时,每次使用前要建立连接、用完后又要关闭,这有点麻烦。当用户数量增加时,会在建立连接和销毁连接过程中耗费很多资源。这时我们可以采用数据源和连接池。数据库连接池在应用程序启动时就创建足够多的数据库连接,在JAVA程序需要访问数据库时就可以通过数据源获得一个空闲连接,用完后再释放到连接池中。
下面以Tomcat6.0和MySQL5.0为例讲讲数据源和连接池的配置:
将数据库JDBC Driver复制到tomcat/lib目录下;
在tomcat/conf/server.xml中配置数据源和连接池:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="/WebRoot" docBase="WebRoot" debug="0" crosscontext="true" reloadable="true">
<Resource name="jdbc/myDataSource" type="javax.sql.DataSource"
username="root" password="123" driverClassName="com.mysql.jdbc.Driver"
maxIdle="30" maxWait="10000" maxActive="100"
url="jdbc:mysql://localhost:3306/test?autoReconnect=true" />
</Context>
</Host>
添加的属性以<Context path=...>开头,置于<host> </host>之间
其中一些属性简介如下:
<!-- 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 0 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. -->
3. 在web.xml中配置对数据源连接池的引用
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app> 4. 在JSP或Servlet中通过数据源访问数据库
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%@ page import="java.sql.*"%><%@ page import="javax.sql.DataSource"%><%@ page import="javax.naming.*"%><html> <head><title>通过数据源访问数据库</title> </head> <body> <h2>通过数据源访问数据库</h2><hr> <% Connection con=null; Statement stat=null; ResultSet rs=null; //从数据源连接池取得连接 Context ctx=new InitialContext(); DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/myDataSource"); con=ds.getConnection(); //查询数据库表 stat=con.createStatement(); String sql="select * from user"; rs=stat.executeQuery(sql); while(rs.next()) { out.println("<li>账号:"+rs.getString(2).trim()); out.println("密码:"+rs.getString(3)+"</li>"); } rs.close(); stat.close(); con.close(); %> </body></html>
5. 在tomcat下,可以通过http://localhost:8080/WebRoot/dbtest.jsp访问到数据库
|
|
|