当前位置:   article > 正文

JAVAWEB(四)SMBMS 期末大作业_javaweb期末大作业简单项目

javaweb期末大作业简单项目

SMBMS javaweb期末大作业

1、项目部署准备

  1. 创建一个新的maven,选中maven的webapp,然后创建

  2. 新建项目结构
    在这里插入图片描述

  3. 连接数据库并且创建实体类

在这里插入图片描述

  1. 配置pom.xml文件信息

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  2. 配置数据库链接驱动db.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
    user=root
    password=root
    
    • 1
    • 2
    • 3
    • 4
  3. 写一个BaseDao来获取数据库的连接信息

    public class BaseDao {
         
        private static String driver;
        private static String url;
        private static String user;
        private static String password;
        //静态代码块,类加载的时候就初始化了
        static {
         
            //通过类加载读取对应资源
            Properties properties = new Properties();
            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
            try{
         
                properties.load(is);
            } catch (IOException e) {
         
                e.printStackTrace();
            }
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            user=properties.getProperty("user");
            password=properties.getProperty("password");
        }
        //获取数据库的连接
        public static Connection getConnection(){
         
            Connection connection=null;
            try{
         
                Class.forName(driver);
                connection= DriverManager.getConnection(url,user,password);
            } catch (ClassNotFoundException e) {
         
                e.printStackTrace();
            } catch (SQLException e) {
         
                e.printStackTrace();
            }
            return connection;
        }
    
        //编写查询的公共类
        public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
         
            //预编译sql,在后面直接执行即可
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
         
                preparedStatement.setObject(i + 1, params[i]);//占位符是从1开始的
            }
            resultSet = preparedStatement.executeQuery(sql);
            return resultSet;
        }
        public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException {
         
            //增删改查公共方法
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
         
                preparedStatement.setObject(i + 1, params[i]);//占位符是从1开始的
            }
            int updateRows=preparedStatement.executeUpdate();
            return updateRows;
        }
        //释放资源
        public static boolean closeResources(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement){
         
            boolean flag=true;
            //回收资源
            if(resultSet!=null){
         
                try{
         
                    resultSet.close();
                } catch (SQLException e) {
         
                    e.printStackTrace();
                    flag=false;
                }
            }
            if(connection!=null){
         
                try{
         
                    connection.close();
                } catch (SQLException e) {
         
                    e.printStackTrace();
                    flag=false;
                }
            }
            if(preparedStatement!=null){
         
                try{
         
                    preparedStatement.close();
                } catch (SQLException e) {
         
                    e.printStackTrace();
                    flag=false;
                }
            }
            return flag;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
  4. 过滤器characterEndingFile

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
    
        chain.doFilter(request,response);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  5. 配置web.xml文件

    <!--    字符编码过滤器-->
        <filter>
            <filter-name>characterEndingFile</filter-name>
            <filter-class>com.kong.filter.characterEndingFile</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>characterEndingFile</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

2、登录实现

1、控制层user

  • UserDao

    public interface UserDao {
         
        //得到需要的用户
        public User getLoginUser(Connection connection,String userCode) throws SQLException;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • UserDaoImpl

    ublic class UserDaoImpl implements UserDao {
         
        public User getLoginUser(Connection connection, String userCode) throws SQLException {
         
            PreparedStatement pstm=null;
            ResultSet rs=null;
            User user=null;
    
            if(connection!=null) {
         
                String sql = "select * from smbms_user where userCode=?";
                Object[] params = {
         userCode};
                    rs = BaseDao.execute(connection, pstm, rs, sql, params);
                    if (rs.next()) {
         
                        user 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/677361
推荐阅读
相关标签
  

闽ICP备14008679号