javaweb的实例--订单管理系统--公共配置

设置数据库配置信息

在这里插入图片描述
在resource 目录下编写数据库配置文件 db.properties

drive=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?characterEncoding=UTF-8
userName=root
password=root

创建操作数据库的工具类

package com.tin.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//操作数据库的公共类
public class BaseDao {
    private static String driver;
    private static String url;
    private static String userName;
    private static String password;
    //静态代码块类加载的时候就执行,获取数据库配置信息
    static {
        //通过类加载器读取对应的资源
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver=properties.getProperty("driver");
        url=properties.getProperty("url");
        userName=properties.getProperty("userName");
        password=properties.getProperty("password");

        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //获取数据库链接
    public static Connection getconnection() throws Exception {
        Connection connection=null;
        //加载驱动
        Class.forName(driver);
        //获取数据库连接
        connection= DriverManager.getConnection(url,userName,password);
        return connection;
    }

    //编写查询公共方法
    public static ResultSet excuteQuery(Connection connection,String sql,Object[] parms) throws Exception {
        ResultSet resultSet=null;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < parms.length; i++) {
            preparedStatement.setObject(i+1,parms[i]);
        }
        resultSet=preparedStatement.executeQuery(sql);
        return  resultSet;
    }

    //编写增删改公共方法
    public static int  excuteUpdate(Connection connection,String sql,Object[] parms) throws Exception {
        int updateRows;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < parms.length; i++) {
            preparedStatement.setObject(i+1,parms[i]);
        }
        updateRows=preparedStatement.executeUpdate(sql);
        return  updateRows;
    }

    //关闭资源
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        boolean flag=true;
        if(connection!=null){
            try {
                connection.close();
                connection=null;
            }catch (Exception e){
                e.printStackTrace();
                flag=false;
            }
        }
        if(connection!=preparedStatement){
            try {
                preparedStatement.close();
                preparedStatement=null;
            }catch (Exception e){
                e.printStackTrace();
                flag=false;
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
                resultSet=null;
            }catch (Exception e){
                e.printStackTrace();
                flag=false;
            }
        }

        return flag;
    }
}

用过滤器Filter 设置网页中文编码

package com.tin.filter;

import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //设置中文编码
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {

    }
}

注册过滤器

<filter>
    <filter-name>Encoding</filter-name>
    <filter-class>com.tin.filter.EncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值