软件工程概论第一次课后作业

1.网站系统开发需要掌握的技术

首先要有java语言基础,其次在做网页方面需要学习html语言,为了网页美观还要学习css。另外还需要JavaScript。了解Web服务器还有学习数据库及SQL语法。最后根据java web   服务器进行开发。

2。源代码

package com.kao.msg.dao;
import java.util.List;

import com.kao.msg.model.User;

public interface IUserDao {
    public void add(User user) ;
    public void delete(String username);
    public void update(User user);
    public User load(int id);
    public User load(String username);
    public List<User> load();

}

 

package com.kao.msg.dao;

import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.kao.msg.Util.DBUtil;
import com.kao.msg.Util.UserException;
import com.kao.msg.model.User;

import sun.net.www.content.text.plain;
public class UserDaoImpl implements IUserDao{

    @SuppressWarnings("resource")
    @Override
    public void add(User user) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        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) values (?,?,?)";
            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(String username) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        String sql = "delete from t_user where username = ?";
        PreparedStatement preparedStatement = null;
        
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            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) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "update t_user set password = ? , nickname=? where username = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getNickname());
            preparedStatement.setString(3, user.getUsername());
            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) {
        // TODO Auto-generated method stub
        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
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from t_user  where username = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(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 List<User> load() {
        // TODO Auto-generated method stub
        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.kao.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) {
        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.kao.msg.Util;

import java.sql.*;

public class DBUtil {
    public  static  Connection getConnection(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "sa";
        String password = "20163433";
        String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=kaoshi";
        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.kao.msg.Util;

public class UserException extends RuntimeException{

    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
    }

    public UserException() {
        super();
        // 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body  background="D:\娱乐\电影\1.jpg">
<%if(request.getAttribute("error1")!=null){
 %>
 <h3>用户不能为空</h3>
 <%
}
 %>
 <%if(request.getAttribute("error2")!=null){
 %>
 <h3>密码不能为空</h3>
 <%
}
 %>
  <%if(request.getAttribute("error3")!=null){
 %>
 <h3>账号不存在</h3>
 <%
}
 %>
<form action="selet.jsp" method="get">
<h2 align="center">用户登录</h2>
<table align="center" border="1" width="500">
            <tr>
                <td>用户名称 : </td>
                <td>
                    <input type="text" name="username" />
                    <a href="testInput.jsp">注册</a>
                </td>
            </tr>
                <tr>
                <td>用户密码:</td>
                <td>
                    <input type="password" name="password" />
                    <a href="updateInput.jsp">修改信息</a><br>
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="登录" /><br>
                    <a href="deleteInput.jsp">注销用户</a><br>
                    
                </td>
            </tr>
        </table>

</body>
</html>
<%@page import="com.kao.msg.Util.UserException"%>
<%@page import="com.kao.msg.dao.UserDaoImpl"%>
<%@page import="com.kao.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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<% 
    //接收客户端传递过来的参数
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    UserDaoImpl userDao = new UserDaoImpl();
    User user = new User();
    if((username == null || "".equals(username.trim()))){
        request.setAttribute("error1", "用户名不能为空");
    

%>
    <jsp:forward page="login.jsp"></jsp:forward>
<%
    }
    if(password == null || "".equals(password.trim())){
        request.setAttribute("error2", "密码不能为空");
%>
    <jsp:forward page="login.jsp"></jsp:forward>
<%
    }
    try{
    user=userDao.load(username);
    if(user==null){
        request.setAttribute("error3", "账号不存在");
%>
    <jsp:forward page="login.jsp"></jsp:forward>

<%    
    }if(user.getUsername().equals(username)&&user.getPassword().equals(password))
    {
%>
        <h1>用户登录成功!!<h1><br>
        <a href="login.jsp">返回登录界面</a><br>
        <a href="list.jsp">查看用户列表</a><br>
<%
    }
    else{
%>
        <h1>用户名和密码不匹配<h1><br>
        <a href="login.jsp">返回登录界面</a><br>
<%
        }
    }catch(UserException e){
        %>
        <h2 style="color:red ; font-size:50px">用户名不存在 : <%=e.getMessage() %></h2>
        <%
        }
        %>
</html>

 

<%@page import="com.kao.msg.Util.UserException"%>
<%@page import="com.kao.msg.dao.UserDaoImpl"%>
<%@page import="com.kao.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="testInput.jsp"></jsp:forward>
<%
    }
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    user.setNickname(nickname);
    System.out.println("成功");
    UserDaoImpl userDao = new UserDaoImpl();
    try{
    userDao.add(user);
    
%>


    <h1>用户保存成功!!<h1><br>
    <a href="login.jsp">注册成功回去登录</a><br>
<%
    }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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册用户</title>
</head>
<body>
<%if(request.getAttribute("error")!=null){
 %>
 <h3>用户不能为空</h3>
 <%
}
 %>
    <form action="test.jsp" method="get">
    <h2 align="center">注册新用户</h2>
        <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="提交" />
                    
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

 

<%@page import="com.kao.msg.Util.UserException"%>
<%@page import="com.kao.msg.dao.UserDaoImpl"%>
<%@page import="com.kao.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="updateInput.jsp"></jsp:forward>
<%
    }
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    user.setNickname(nickname);
    System.out.println("成功");
    UserDaoImpl userDao = new UserDaoImpl();
    try{
    userDao.update(user);
    
%>

    <h1>用户信息修改成功!!<h1><br>
    <a href="login.jsp">返回登录界面</a><br>
<%
    }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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户信息修改</title>
</head>
<body>
<%if(request.getAttribute("error")!=null){
 %>
 <h3>用户不能为空</h3>
 <%
}
 %>
    <form action="update.jsp" method="get">
    <h2 align="center">修改信息</h2>
        <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="提交" />
                    
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

<%@page import="com.kao.msg.Util.UserException"%>
<%@page import="com.kao.msg.dao.UserDaoImpl"%>
<%@page import="com.kao.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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>删除结果</title>
</head>
<body>
<%
    //接收客户端传递过来的参数
    String username = request.getParameter("username");
    if(username == null || "".equals(username.trim())){
        request.setAttribute("error", "用户名不能为空");
    

%>
    <jsp:forward page="deleteInput.jsp"></jsp:forward>
<%
    }
    User user = new User();
    user.setUsername(username);
    UserDaoImpl userDao = new UserDaoImpl();
    try{
        if(userDao.load(username)!=null){
               userDao.delete(username);
%>
    <h1>用户注销成功!!<h1><br>
    <a href="login.jsp">返回登录</a><br>
<%
        }else{
    
%>
<h1>用户不存在!!<h1><br>
<a href="login.jsp">返回登录</a><br>
<%
        }
%>
    
<%
    }catch(UserException e){
%>
    <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
    <%
    }
    %>
</body>
</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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注销</title>
</head>
<body>
  <form action="delete.jsp" method="get"> 
<h2 align="center">用户删除</h2>
<table align="center" border="1" width="500">
             <tr>
                <td>注销的用户名称 : </td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="确定" /><br>        
                </td>
            </tr>
            </table>

</form>
</body>
</html>
<%@page import="com.kao.msg.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.kao.msg.dao.UserDaoImpl"%>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    UserDaoImpl userDao = new UserDaoImpl();
    List<User> users = userDao.load();
%>

<body>

    <table align="center" border="1" width="500">
        <tr>
            <td>用户编号</td>
            <td>用户名称</td>
            <td>用户密码</td>
            <td>用户昵称</td>
        </tr>
        <%
            for( User user : users ){
        %>
        <tr>
            <td> <%=user.getId() %></td>
            <td> <%=user.getUsername() %></td>
            <td> <%=user.getPassword() %></td>
            <td> <%=user.getNickname() %></td>
            <td> <a href="delete.jsp ? username=<%=user.getUsername() %>">删除</a></td>
            
        </tr>
        <%
            }
        %>
    </table>
</body>
</body>
</html>

 

3 运行结果截图

 

 

4,对这门课的目标

希望在结束的时候可以自己独立的完成永恒web项目的开发。

每天至少2个小时的时间需要消耗在这门课上。

posted @ 2017-11-23 11:26  星际毁灭  阅读(171)  评论(0)    收藏  举报