JavaWeb课程设计项目实战(04)——项目编码实践1


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

在这里插入图片描述

在本节教程中,我们完成本项目通用模块的开发。

添加jar包

请将本项目所需jar包添加至lib文件夹,图示如下:
在这里插入图片描述

C3P0配置文件

请在resources下创建C3P0配置文件c3p0-config.xml,图示如下:
在这里插入图片描述
代码如下:

<?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>

数据库工具类

请在util包下面创建数据库工具类C3P0Utils,图示如下:
在这里插入图片描述
代码如下:

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;
        }
    }
}

数据库连接性测试

请在com.cn中创建test包,并在该包下创建测试类Test。请在Test类中测试数据库的连接是否正常,图示如下:

在这里插入图片描述
代码如下:

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);
    }
}

编码过滤器

请在filter包下创建编码过滤器EncodingFilter避免中文乱码,图示如下:
在这里插入图片描述

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() {
    
    

    }
}

在完成以上Java代码之后请在web.xml中配置编码过滤器,图示如下:
在这里插入图片描述
代码如下:

<?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>

猜你喜欢

转载自blog.csdn.net/lfdfhl/article/details/131698633