Copyright Notice
- The original author of this article: Brother Gu’s younger brother
- Author blog address: http://blog.csdn.net/lfdfhl
In this section of the tutorial, we complete the development of the general module of this project.
add jar package
Please add the jar package required for this project to the lib folder, as shown below:
C3P0 configuration file
Please create the C3P0 configuration file c3p0-config.xml under resources, as shown in the figure below:
the code is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!--请务必注意修改数据配置信息-->
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/studentinformationmanagement</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">15</property>
<property name="maxIdleTime">40</property>
<property name="maxPoolSize">150</property>
<property name="minPoolSize">20</property>
</default-config>
</c3p0-config>
Database tools
Please create the database tool class C3P0Utils under the util package, as shown in the figure below:
the code is as follows:
package com.cn.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class C3P0Utils {
private static DataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("获取数据库连接失败");
}
}
public static void release(Connection connection, Statement statement,ResultSet resultSet ) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement = null;
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
}
}
Database Connectivity Test
Please create a test package in com.cn, and create a test class Test under this package. Please test whether the connection to the database is normal in the Test class, as shown below:
code show as below:
package com.cn.test;
import com.cn.util.C3P0Utils;
import java.sql.Connection;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Test {
public static void main(String[] args) {
Connection connection = C3P0Utils.getConnection();
System.out.println("connection="+connection);
}
}
encoding filter
Please create an encoding filter EncodingFilter under the filter package to avoid Chinese garbled characters, as shown below:
package com.cn.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
httpServletResponse.setContentType("text/html;charset=UTF-8");
httpServletRequest.setCharacterEncoding("UTF-8");
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
@Override
public void destroy() {
}
}
After completing the above Java code, please configure the encoding filter in web.xml, as shown in the figure below:
The code is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--编码过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.cn.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>