zoukankan      html  css  js  c++  java
  • 软件工程概论01

    1.java语言

    从WEB项目应用角度讲有JSP,Servlet,JDBC,JavaBean(Application)四部分技术。

    JDBC可做三件事情:与数据库建立连接,发送SQL语句,处理结果。

    Servlet从客户端(通过WEB服务器)接受请求,执行某种操作,然后返回结果。

    JSP是从Servlet上分离出来的一小部分,简化了开发,加强了页面设计。

    JavaBean能提供常用功能并且可以重复使用,这使得开发人员可以把某些关键功能和核心算法提取出来封装成为一个组件对象,这样就增加了代码的重用率和系统的安全性。

    2.XML语言

    在服务器和设计模式结构中会应用到自定义文件,而且在应用高级设计时也会定义自用的标签,现在流行的是用XML去定义配置,所以XML语言应该有一定掌握。XML大致可以分为3类,分别是简单数据的表示和交换,面向消息的计算,用户界面相关。

    3.脚本语言

    javascript、php.

    4.开发工具

    数据库,Web服务器,集成开发环境.

    源代码:

    package com.jaovo.msg.dao;
    
    import java.util.List;
    
    import com.jaovo.msg.model.User;
    
    public interface IUserDao {
        public void login(User user);
        public void add(User user);
        public void delete(int id);
        public void update(User user);
        public User load(int id);
        public User load(String username);
        public List<User> load();
        
    }
    package com.jaovo.msg.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    
    import sun.net.www.content.text.plain;
    
    public class UserDaoImpl implements IUserDao {
        
    
        @Override
        public void login(User user)
        {
            //获得链接对象
                    Connection connection = DBUtil.getConnection();
                    //准备sql语句
                    String sql = "select count(*) from t_user where username = ?";
                    //创建语句传输对象
                    PreparedStatement preparedStatement = null;
                    ResultSet resultSet = null;
                    try {
                        preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setString(1, user.getUsername());
                        //接收结果?
                        resultSet = preparedStatement.executeQuery();
                        //遍历结果?
                        while(resultSet.next()) {
                            if (resultSet.getInt(1) > 0) {
                                System.out.println("登陆成功!!!");
                            }
                            else
                                throw new UserException("用户不存在") ;
                        
        }
                    }catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally {
                        //关闭资源
                        DBUtil.close(resultSet);
                        DBUtil.close(preparedStatement);
                        DBUtil.close(connection);
                    }
        }
        
        @Override
        public void add(User user) {
            //获得链接对象
            Connection connection = DBUtil.getConnection();
            //准备sql语句
            String sql = "select count(*) from t_user where username = ?";
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getUsername());
                //接收结果?
                resultSet = preparedStatement.executeQuery();
                //遍历结果?
                while(resultSet.next()) {
                    if (resultSet.getInt(1) > 0) {
                        throw new UserException("用户已存在") ;
                    }
                }
                
                sql = "insert into t_user(username,password,nickname) value (?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getUsername());
                preparedStatement.setString(2, user.getPassword());
                preparedStatement.setString(3, user.getNickname());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                //关闭资源
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
        @Override
        public void delete(int id) {
            Connection connection = DBUtil.getConnection();
            String sql = "delete from t_user where id = ?";
            PreparedStatement preparedStatement = null;
            
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, id);
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
        @Override
        public void update(User user) {
            Connection connection = DBUtil.getConnection();
            //准备sql语句
            String sql = "update t_user set password = ? , nickname=? where id = ?";
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getPassword());
                preparedStatement.setString(2, user.getNickname());
                preparedStatement.setInt(3, user.getId());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
        }
    
        @Override
        public User load(int id) {
            Connection connection = DBUtil.getConnection();
            //准备sql语句
            String sql = "select * from t_user  where id = ?";
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            User user = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, id);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()) {
                    user = new User();
                    user.setId(id);
                    user.setUsername(resultSet.getString("username"));
                    user.setPassword(resultSet.getString("password"));
                    user.setNickname(resultSet.getString("nickname"));
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return  user;
        }
    
        @Override
        public User load(String username) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public List<User> load() {
            Connection connection = DBUtil.getConnection();
            //准备sql语句
            String sql = "select * from t_user ";
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            //集合中只能放入user对象
            List<User> users = new ArrayList<User>();
            User user = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()) {
                    user = new User();
                    user.setId(resultSet.getInt("id"));
                    user.setUsername(resultSet.getString("username"));
                    user.setPassword(resultSet.getString("password"));
                    user.setNickname(resultSet.getString("nickname"));
                    users.add(user);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return  users;
        }
    
    }
    package com.jaovo.msg.model;
    
    public class User {
        
        private int id;
        private String  username;
        private String  nickname;
        private String  password;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getNickname() {
            return nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
    package com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        
        public  static  Connection getConnection() {
            try {
                //1 加载驱动
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "root";
            String password = "";
            String url = "jdbc:mysql://localhost:3306/jaovo_msg";
            Connection connection = null;
            try {
                //2 创建链接对象connection
                 connection = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
        //关闭资源的方�?
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    package com.jaovo.msg.Util;
    
    public class UserException extends RuntimeException{
    
        public UserException() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
        
    }
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>用户登录页面</title>
    </head>
    <body bgcolor="pink">
        <%=request.getAttribute("error") %>
        
        <form action="logincheck.jsp" method="get">
            <table align="center" border="1" width="500">
                <tr>
                    <td>用户名称 : </td>
                    <td>
                        <input type="text" name="username" />
                    </td>
                </tr>
                    <tr>
                    <td>用户密码:</td>
                    <td>
                        <input type="password" name="password" />
                    </td>
                </tr>
                <tr>
                    <td>用户昵称:</td>
                    <td>
                        <input type="text" name="nickname" />
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="登录" />
                        <input type="reset" value="重置" />
                        <input type="button" value="注册" onclick="window.location.href='addInput.jsp'">
                    </td>
                </tr> 
            </table>
        </form>
    </body>
    </html>
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <%
        //接收客户端传递过来的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        if(username == null || "".equals(username.trim())){
            request.setAttribute("error", "用户名不能为空");
        
    %>
        <jsp:forward page="login.jsp"></jsp:forward>
    <%
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        UserDaoImpl userDao = new UserDaoImpl();
        try{
            
        userDao.login(user);
        
        //重定向
        //response.sendRedirect("list.jsp");
            %>
        登录成功!!!<br>
    
        <a href="addInput.jsp">登录</a><br>
        <a href="list.jsp">用户列表</a>;
    
    
        
        
    <%
        }catch(UserException e){
    %>
        <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
        }
        %>
    </html>
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <%
        //接收客户端传递过来的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        if(username == null || "".equals(username.trim())){
            request.setAttribute("error", "用户名不能为空");
        
    %>
        <jsp:forward page="addInput.jsp"></jsp:forward>
    <%
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        UserDaoImpl userDao = new UserDaoImpl();
        try{
            
        userDao.add(user);
        
        //重定向
        //response.sendRedirect("list.jsp");
            %>
        注册成功!!!<br>
    
        <a href="addInput.jsp">注册</a><br>
        <a href="list.jsp">用户列表</a>;
    
    
        
        
    <%
        }catch(UserException e){
    %>
        <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
        }
        %>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>用户注册页面</title>
    </head>
    <body bgcolor="pink">
        <%=request.getAttribute("error") %>
        <form action="add.jsp" method="get">
            <table align="center" border="1" width="500">
                <tr>
                    <td>用户名称 : </td>
                    <td>
                        <input type="text" name="username" />
                    </td>
                </tr>
                    <tr>
                    <td>用户密码:</td>
                    <td>
                        <input type="password" name="password" />
                    </td>
                </tr>
                <tr>
                    <td>用户昵称:</td>
                    <td>
                        <input type="text" name="nickname" />
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="提交" />
                        <input type="reset" value="重置" />
                    </td>
                </tr> 
            </table>
        </form>
    </body>
    </html>
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@page import=" java.util.ArrayList" %>
    <%@page import=" java.util.List" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    
    <title>显示用户</title>
    <% 
    UserDaoImpl userDao = new UserDaoImpl();
    List<User> list=null;
    try{
         list = new ArrayList<User>();
        list=userDao.load();
    }catch(UserException e){
    %>
        <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        
    <%
    }
    %>
    <div align="center">用户展示页面</div>
    <table align="center" border="1" width="500">
      <form action="delete.jsp" method="get"> 
        <tr>
             <td>
                 id
             </td>
                    <td>
                                                           用户名
                    </td>
                    <td>
                                                             密码
                    </td>
              <td>
                                                    昵称
             </td>
              <td>
                                                删除
              </td>
               <td>
                                                  修改
               </td>
             
        </tr>
        
        <%
        for(User user:list){
        %>
              <tr>
                   <td>
                      <%=user.getId() %>
                   </td>
                         <td>
                             <%=user.getUsername() %>
                         </td>
                                <td>
                                <%=user.getPassword() %>
                                </td>
                         <td>
                                <%=user.getNickname() %>
                         </td>
                          <td>
                               <a href="delete.jsp?userid=<%=user.getId() %>">删除</a>
                          </td>
                     <td>
                          <a href="updateinput.jsp?userid">修改</a>
                     </td>
              </tr>
             <%
        }
             %>
             </table>>
    </html>

    运行结果:

    总结:

    我希望通过软件工程概论的学习,可以独立完成数据库的增删改查,同时能够学会与团队的沟通、合作,在期末完成一个小型矽统的架构。

    因此,我会利用课余时间勤加练习,争取每天编程2小时以上,找bug,改bug,做到程序的程序的健壮性!

  • 相关阅读:
    leetcode-----16. 最接近的三数之和
    leetcode-----15. 三数之和
    leetcode-----14. 最长公共前缀
    leetcode-----13. 罗马数字转整数
    leetcode-----12. 整数转罗马数字
    leetcode-----11. 盛最多水的容器
    leetcode-----10. 正则表达式匹配
    leetcode-----9. 回文数
    leetcode-----8. 字符串转换整数 (atoi)
    leetcode-----7. 整数反转
  • 原文地址:https://www.cnblogs.com/xiaohaigege666/p/7884520.html
Copyright © 2011-2022 走看看