MVC
web服务器的MVC模式视图,窃用小弟的图:
- Javabean用于封装数据和处理数据;
- Servlet负责接收请求和控制程序流转;
- JSP负责展示数据。
常用的分包方式:
- cn.com.domain 实体;
- cn.com.service 业务接口;
- cn.com.service.impl 业务接口的实现;
- cn.com.dao 数据访问接口;
- cn.com.dao.impl 数据访问接口的实现;
- cn.com.servlet 接收请求和控制程序流转。
数据库连接池
数据库连接池,我们可以联想一下线程池,他们的作用是一样的。在同一时间段有大量用户访问服务器,需要用到数据库连接池。
数据库连接池负责分配、管理和释放数据库连接。它允许程序重复使用一个现有的数据库连接,而不是再重新建立一个。数据库连接池可自动释放闲置时间超过最大空闲时间的数据库连接从而避免因为没有释放数据库连接而引起的数据库连接遗漏。
数据库连接池一般包含两种:DBCP和C3P0。
DBCP
DBCP(DataBase Connection Pool)由Apache研发,而且Tomcat的连接池也正是采用DBCP实现的,该数据库连接池既可与应用服务器整合使用,也可由应用程序独立使用。
1.添加jar包
添加jar包到项目环境变量中去。
commons-dbcp.jar
commons-pool.jar
mysql-connector-java-5.0.8-bin.jar
2.编写DBCP的配置文件dbcpconfig.properties
直接在”src”目录下新建dbcpconfig.properties文件。
#<!-- 连接设置 -->
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydatabase
username=root
password=root
#<!-- 初始化连接 -->
initialSize=10
#<!-- 最大连接数量 -->
maxActive=50
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间(单位毫秒) -->
maxWait=50000
#<!-- 编码方式 -->
connectionProperties=useUnicode=true;characterEncoding=utf8
##<!-- 指定由连接池所创建的连接自动提交 -->
defaultAutoCommit=true
#<!-- 指定由连接池所创建的连接的事务级别 -->
defaultTransactionIsolation=REPEATABLE_READ
3.写DBCP工具类
DBCPUtil.class
package dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DBCPUtil {
private static DataSource dataSource = null;
static{
try {
Properties p = new Properties();
ClassLoader classLoader = DBCPUtil.class.getClassLoader();
p.load(classLoader.getResourceAsStream("dbcpconfig.properties"));
dataSource = BasicDataSourceFactory.createDataSource(p);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建数据库连接
* @Description: TODO
* @author: qdl
* @CreateTime: 2017-6-23 下午2:37:38
* @return
* @throws
*/
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("数据库连接错误");
}
}
/**
* 销毁数据库连接
* @Description: TODO
* @author: qdl
* @CreateTime: 2017-6-23 下午2:37:52
* @param conn
* @param stmt
* @param res
* @throws
*/
public static void destroyConnect(Connection conn, Statement stmt, ResultSet res){
try {
if( res!=null ){
res.close();
res = null;
}
if( stmt!=null ){
stmt.close();
stmt = null;
}
if( conn!=null ){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.调用
伪代码
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pStmt = null;
ResultSet res = null;
conn = DBCPUtil.getConnection();
try {
pStmt = conn.prepareStatement("select * from person");
res = pStmt.executeQuery();
while( res.next() ){
System.out.println(
"name:" + res.getString("pname") +
",age:" + res.getInt("page") +
",city" + res.getString("pcity"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBCPUtil.destroyConnect(conn, pStmt, res);
}
}
代码
C3P0
C3P0是一个开源的JDBC连接池,目前有hibernate,spring等框架也使用该数据库连接池。
1.添加jar包
c3p0-0.9.5.1.jar
mchange-commons-java-0.2.10.jar
mysql-connector-java-5.0.8-bin.jar
2.C3P0配置文件c3p0.properties
文件直接放入到”src”目录下。
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.password=root
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5
3.写C3P0工具类
我们不用手动指定c3p0.properties文件,会自动识别。
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Util {
private static DataSource dataSource = new ComboPooledDataSource();
/**
* 创建连接
*/
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("获取数据库连接失败");
}
}
/**
* 释放连接
*/
public static void destroyConnection(Connection conn, PreparedStatement pStmt, ResultSet res){
try {
if( res!=null ){
res.close();
res = null;
}
if( pStmt!=null ){
pStmt.close();
pStmt = null;
}
if( conn!=null ){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.调用
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pStmt = null;
ResultSet res = null;
try {
conn = C3P0Util.getConnection();
pStmt = conn.prepareStatement("select * from person");
res = pStmt.executeQuery();
while( res.next() ){
System.out.println(
"name:" + res.getString("pname") +
",age:" + res.getInt("page") +
",city" + res.getString("pcity"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
其结果和DBCP一致。