Javaweb高级第四次课
-
什么是连接池
如何使用连接池
package com.direct.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBUtil {
//静态放获取连接
public static Connection getConn(){
try {
//创建内容对象
Context ct=new InitialContext();
//调用lookup()关联连接池得到数据源对象
DataSource ds = (DataSource)ct.lookup("java:comp/env/testJNDI");
return ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
package com.direct.util.dbcp;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
import org.apache.commons.dbcp.BasicDataSource;
public class DBUtil {
private static BasicDataSource dbs=null;
//提供静态方法获取连接
public static Connection getConn(){
try {
if(dbs==null){
//创建基础数据源对象
dbs=new BasicDataSource();
//创建资源对象
ResourceBundle rb=ResourceBundle.getBundle("dbcp");
//设置连接池相关属性
dbs.setMaxActive(Integer.parseInt(rb.getString("maxactive")));
dbs.setMaxIdle(Integer.parseInt(rb.getString("maxidle")));
dbs.setMaxWait(Integer.parseInt(rb.getString("maxwait")));
dbs.setUsername(rb.getString("user"));
dbs.setPassword(rb.getString("pwd"));
dbs.setUrl(rb.getString("url"));
dbs.setDriverClassName(rb.getString("driver"));
}
return dbs.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
连接MySQL数据库
maxactive=200
maxidle=30
maxwait=10000
user=root
pwd=123456
url=jdbc:mysql://localhost:3306/mysql?characterEncoding=utf8
driver=com.mysql.jdbc.Driver
<?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">
<!-- 引入JNDI -->
<resource-ref>
<res-ref-name>testJNDI</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.direct.util.DBUtil"%>
<%@page import="java.sql.Connection"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Connection con = DBUtil.getConn();
out.print(con);
%>
</body>
</html>
1 什么是连接池
连接池:是一个存放了多个连接的池子。当需要使用连接时则直接从连接池中获取,使用结束后关闭连接(放回连接池中)。
如果连接池中没有可用连接时,可以设置一个最大的等待时间,等待中如果空闲连接了则使用,否则返回-1。
说明:目前常规的连接池都是第三方提供(JNDI、DBCP、C3P0等)
2 如何使用连接池
JNDI:java的命名目录和规范的API(由apache提供的一套标准连接池)
如何使用:
a 将数据库驱动程序(*.jar)放在tomcat的lib目录下
b 找到tomcat的conf中的context.xml配置文件,配置连接池信息(如:名称、数据源、最大连接数、最大的空闲连接数、等待时间、驱动字符串、连接字符串等)
<Resource
name=""——连接池名称
type="javax.sql.DataSource"——数据源
auth="Container/application"——配置连接池的管理对象(web容器管理/应用程序管理)
maxActive=""——配置最大连接数
maxIdle=""——配置最大的空闲连接数
maxWait=""——配置最大的等待时间(毫秒)
username=""
password=""
url=""——连接字符串
driverClassName=""——数据库驱动字符串
/>
c 应用程序中的web.xml中引入配置的JNDI连接池
d 在DBUtil中定义方法从连接池中获取连接
DBCP:DataBase ConnectionPool,由第三方提供的一套连接池
如何使用:
a 程序中引入第三方提供的连接池驱动包
b 在src目录下创建资源文件(*.properties)指定连接池相关的属性(key=value的键值对形式存在)
c 在DBUtil中定义方法设置连接池相关属性并获取连接对象