Skip to content

Commit b470bd3

Browse files
author
cicadasmile
committed
JavaEE基础(06):Servlet整合C3P0数据库连接池
1 parent 8a589bf commit b470bd3

File tree

14 files changed

+365
-0
lines changed

14 files changed

+365
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[JavaEE基础(03):Http请求详解,握手挥手流程简介](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484318&idx=1&sn=125780e20ada2f3d1451a1563a1c1e6f&chksm=fdf45726ca83de30adc5956a16d16151825b37c0adf5350214899ade39fef47f17ee35b2390d&token=1248678182&lang=zh_CN#rd)<br/>
66
[JavaEE基础(04):会话跟踪技术,Session和Cookie详解](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484325&idx=1&sn=a3c96cff45fac355d947dff9e7f5b9bf&chksm=fdf4571dca83de0b590bbc2864561c33b4b7af512d5b0dce86eca83170a23f007a1786d22f8c&token=362281359&lang=zh_CN#rd)<br/>
77
[JavaEE基础(05):过滤器、监听器、拦截器,应用详解](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484330&idx=1&sn=2754da84764ba5db8bccb1112881af3e&chksm=fdf45712ca83de04848d00451ce473275514ec8e60786c6f8db355190b0360463eb10da6c94c&token=362281359&lang=zh_CN#rd)<br/>
8+
[JavaEE基础(06):Servlet整合C3P0数据库连接池](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484335&idx=1&sn=6755f997766ceb8b81c83201ba101553&chksm=fdf45717ca83de01b39de0f3e5ca15607d12960cf5f9e2df391bc6c6af0808018dd2b1969a73&token=362281359&lang=zh_CN#rd)<br/>
89

910
持续更新中...
1011

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-ee-base-parent</artifactId>
7+
<groupId>com.java.ee.parent</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<groupId>com.node06.jdbc</groupId>
12+
<artifactId>node06-servlet-jdbc</artifactId>
13+
<packaging>war</packaging>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<jstl.version>1.2</jstl.version>
20+
<servlet-api.version>2.5</servlet-api.version>
21+
<jsp-api.version>2.0</jsp-api.version>
22+
<mysql.version>5.1.39</mysql.version>
23+
<c3p0.version>0.9.5.2</c3p0.version>
24+
</properties>
25+
26+
<dependencies>
27+
<!-- JSP相关 依赖包-->
28+
<dependency>
29+
<groupId>jstl</groupId>
30+
<artifactId>jstl</artifactId>
31+
<version>${jstl.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>javax.servlet</groupId>
35+
<artifactId>jsp-api</artifactId>
36+
<version>${jsp-api.version}</version>
37+
<scope>provided</scope>
38+
</dependency>
39+
<!-- servlet依赖包 -->
40+
<dependency>
41+
<groupId>javax.servlet</groupId>
42+
<artifactId>servlet-api</artifactId>
43+
<version>${servlet-api.version}</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>mysql</groupId>
53+
<artifactId>mysql-connector-java</artifactId>
54+
<version>${mysql.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.mchange</groupId>
58+
<artifactId>c3p0</artifactId>
59+
<version>${c3p0.version}</version>
60+
</dependency>
61+
</dependencies>
62+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.node06.servlet.bean;
2+
3+
public class UserInfo {
4+
private String userName ;
5+
private Integer userAge ;
6+
7+
@Override
8+
public String toString() {
9+
return "UserInfo{" +
10+
"userName='" + userName + '\'' +
11+
", userAge=" + userAge +
12+
'}';
13+
}
14+
15+
public UserInfo(String userName, Integer userAge) {
16+
this.userName = userName;
17+
this.userAge = userAge;
18+
}
19+
20+
public String getUserName() {
21+
return userName;
22+
}
23+
public void setUserName(String userName) {
24+
this.userName = userName;
25+
}
26+
27+
public Integer getUserAge() {
28+
return userAge;
29+
}
30+
31+
public void setUserAge(Integer userAge) {
32+
this.userAge = userAge;
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.node06.servlet.config;
2+
3+
import com.mchange.v2.c3p0.ComboPooledDataSource;
4+
5+
import javax.sql.DataSource;
6+
import java.sql.Connection;
7+
import java.sql.PreparedStatement;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
11+
public class C3P0Pool {
12+
private static DataSource dataSource = new ComboPooledDataSource();
13+
14+
public static DataSource getDataSource() {
15+
return dataSource ;
16+
}
17+
18+
/**
19+
* 获取连接
20+
*/
21+
public static Connection getConnection() throws SQLException {
22+
return dataSource.getConnection();
23+
}
24+
25+
/**
26+
* 释放连接
27+
*/
28+
public static void close(ResultSet resultSet, PreparedStatement pst, Connection connection) {
29+
if (resultSet != null) {
30+
try {
31+
resultSet.close();
32+
} catch (SQLException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
if (pst != null) {
37+
try {
38+
pst.close();
39+
} catch (SQLException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
if (connection != null) {
44+
try {
45+
connection.close();
46+
} catch (SQLException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.node06.servlet.impl;
2+
3+
import com.node06.servlet.bean.UserInfo;
4+
import com.node06.servlet.query.UserJdbcQuery;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
public class JdbcServletImpl extends HttpServlet {
13+
/**
14+
* 测试
15+
* http://localhost:6003/jdbcServletImpl?userName=LiSi
16+
* 用户信息:UserInfo{userName='LiSi', userAge=22}
17+
*/
18+
@Override
19+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
20+
throws ServletException, IOException {
21+
String userName = request.getParameter("userName") ;
22+
UserInfo userInfo = UserJdbcQuery.queryUser(userName) ;
23+
response.setContentType("text/html;charset=utf-8");
24+
response.getWriter().print("用户信息:"+userInfo);
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.node06.servlet.query;
2+
3+
import com.node06.servlet.config.C3P0Pool;
4+
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
9+
public class UserJdbcDelete {
10+
11+
public static void deleteUser (Integer id){
12+
try {
13+
Connection connection = C3P0Pool.getConnection();
14+
String sql = "DELETE FROM user_info WHERE id=?" ;
15+
PreparedStatement statement = connection.prepareStatement(sql);
16+
statement.setInt(1,id);
17+
statement.executeUpdate() ;
18+
C3P0Pool.close(null, statement, connection);
19+
} catch (Exception e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
deleteUser(5);
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.node06.servlet.query;
2+
3+
import com.node06.servlet.bean.UserInfo;
4+
import com.node06.servlet.config.C3P0Pool;
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class UserJdbcInsert {
11+
12+
public static void insertUser (UserInfo userInfo){
13+
try {
14+
Connection connection = C3P0Pool.getConnection();
15+
String sql = "INSERT INTO user_info (user_name,user_age) VALUES (?,?)" ;
16+
PreparedStatement statement = connection.prepareStatement(sql);
17+
statement.setString(1,userInfo.getUserName());
18+
statement.setString(2,userInfo.getUserAge().toString());
19+
statement.execute() ;
20+
C3P0Pool.close(null, statement, connection);
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
public static void batchInsertUser (List<UserInfo> userInfoList){
27+
try {
28+
Connection connection = C3P0Pool.getConnection();
29+
String sql = "INSERT INTO user_info (user_name,user_age) VALUES (?,?)" ;
30+
PreparedStatement statement = connection.prepareStatement(sql);
31+
for (UserInfo userInfo:userInfoList){
32+
statement.setString(1,userInfo.getUserName());
33+
statement.setString(2,userInfo.getUserAge().toString());
34+
statement.addBatch();
35+
}
36+
statement.executeBatch() ;
37+
C3P0Pool.close(null, statement, connection);
38+
} catch (Exception e) {
39+
e.printStackTrace();
40+
}
41+
}
42+
43+
public static void main(String[] args) {
44+
List<UserInfo> userInfoList = new ArrayList<>() ;
45+
userInfoList.add(new UserInfo("Wang",13)) ;
46+
userInfoList.add(new UserInfo("Liu",17)) ;
47+
batchInsertUser(userInfoList);
48+
}
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.node06.servlet.query;
2+
3+
import com.node06.servlet.bean.UserInfo;
4+
import com.node06.servlet.config.C3P0Pool;
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
9+
public class UserJdbcQuery {
10+
11+
public static UserInfo queryUser (String userName){
12+
UserInfo userInfo = null ;
13+
try {
14+
Connection connection = C3P0Pool.getConnection();
15+
String sql = "SELECT * FROM user_info WHERE user_name=?" ;
16+
PreparedStatement statement = connection.prepareStatement(sql);
17+
statement.setString(1,userName);
18+
ResultSet resultSet = statement.executeQuery() ;
19+
while (resultSet.next()){
20+
int id = resultSet.getInt("id");
21+
String name = resultSet.getString("user_name");
22+
int age = resultSet.getInt("user_age");
23+
System.out.println("ID:"+id+";name:"+name+";age:"+age);
24+
userInfo = new UserInfo(name,age) ;
25+
}
26+
C3P0Pool.close(resultSet, statement, connection);
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
return userInfo ;
31+
}
32+
33+
public static void main(String[] args) {
34+
queryUser("Liu");
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.node06.servlet.query;
2+
3+
import com.node06.servlet.config.C3P0Pool;
4+
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
8+
public class UserJdbcUpdate {
9+
10+
public static void updateUser (String name,Integer age,Integer id){
11+
try {
12+
Connection connection = C3P0Pool.getConnection();
13+
String sql = "UPDATE user_info SET user_name=?,user_age=? WHERE id=?" ;
14+
PreparedStatement statement = connection.prepareStatement(sql);
15+
statement.setString(1,name);
16+
statement.setInt(2,age);
17+
statement.setInt(3,id);
18+
statement.executeUpdate() ;
19+
C3P0Pool.close(null, statement, connection);
20+
} catch (Exception e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public static void main(String[] args) {
26+
updateUser("Zhang",23,1);
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<c3p0-config>
3+
<default-config>
4+
<!-- 核心参数配置 -->
5+
<property name="jdbcUrl">jdbc:mysql://localhost:3306/servlet-jdbc</property>
6+
<property name="driverClass">com.mysql.jdbc.Driver</property>
7+
<property name="user">root</property>
8+
<property name="password">123</property>
9+
<!-- 池参数配置 -->
10+
<property name="acquireIncrement">3</property>
11+
<property name="initialPoolSize">10</property>
12+
<property name="minPoolSize">2</property>
13+
<property name="maxPoolSize">10</property>
14+
</default-config>
15+
</c3p0-config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE `user_info` (
2+
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
3+
`user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
4+
`user_age` int(11) DEFAULT '1' COMMENT '年龄',
5+
PRIMARY KEY (`id`)
6+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns="http://java.sun.com/xml/ns/javaee"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6+
id="WebApp_ID" version="3.0">
7+
8+
<display-name>cicada_servlet</display-name>
9+
<context-param>
10+
<param-name>encoding</param-name>
11+
<param-value>UTF-8</param-value>
12+
</context-param>
13+
14+
<servlet>
15+
<servlet-name>jdbcServletImpl</servlet-name>
16+
<servlet-class>com.node06.servlet.impl.JdbcServletImpl</servlet-class>
17+
</servlet>
18+
<servlet-mapping>
19+
<servlet-name>jdbcServletImpl</servlet-name>
20+
<url-pattern>/jdbcServletImpl</url-pattern>
21+
</servlet-mapping>
22+
23+
</web-app>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h2>JDBC!</h2>
4+
</body>
5+
</html>

java-ee-base-parent/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<module>node02-servlet-api</module>
1717
<module>node03-servlet-http</module>
1818
<module>node04-servlet-session</module>
19+
<module>node05-servlet-filter</module>
20+
<module>node06-servlet-jdbc</module>
1921
</modules>
2022

2123
</project>

0 commit comments

Comments
 (0)