javaweb22/4/12

SMBMS

项目搭建

1.搭建maven项目
2.配置Tomcat
3.测试项目能否跑起来
4.导入项目中需要用到的jar包
5.创建项目包结构

6.连接数据库
7.编写实体类:ORM映射
8.编写基础公共类
1)数据库文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=297999

2)操作数据库的公共类

public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块,在类加载的时候就初始化了
    static{
        Properties prop = new Properties();
        //通过类加载器读取资源文件
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver=prop.getProperty("driver");
        url=prop.getProperty("url");
        username=prop.getProperty("username");
        password=prop.getProperty("password");

    }
    //获取数据库的连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection= DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    //编写查询公共方法
    public static ResultSet execute(Connection connection,String sql,Object[] params) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;

    }
    //编写增删改公共方法
    public static int executeUpdate(Connection connection,String sql,Object[] params) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        int i = preparedStatement.executeUpdate();
        return i;
    }
    //关闭资源
    public static boolean close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) {
        boolean flag = false;
        if (resultSet!=null){
            try {
                resultSet.close();
                resultSet=null;
            } catch (Exception e) {
                flag=false;
                e.printStackTrace();
            }
        }
        if (preparedStatement!=null){
            try {
                preparedStatement.close();
                preparedStatement=null;
            } catch (Exception e) {
                flag=false;
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
                connection=null;
            } catch (Exception e) {
                flag=false;
                e.printStackTrace();
            }
        }
        return flag;

    }

}
  1. 编写字符编码的过滤器
 @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");

        filterChain.doFilter(servletRequest,servletResponse);
    }
  <filter>
        <filter-name>CharacterEncoding</filter-name>
        <filter-class>com.wenping.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

9.导入静态资源

posted @   想吃坚果  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示