zoukankan      html  css  js  c++  java
  • 软件工程概论第一次课后作业

    一.开发网站系统需要的技术:

    Javascript技术,HTML技术,SQL语句。

    二.源程序代码:

    package com.jaovo.msg.dao;
    
    import java.util.List;
    
    import com.jaovo.msg.model.User;
    
    public interface IUserDao {
    public void add(User user);
    public void delete(User user);
    public void update(User user);
    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;
    
    public class UserDaoImpl implements IUserDao {
    
    @Override
    public void add(User user) {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select count(*) from table_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 table_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();
    System.out.println("存储成功");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //关闭资源
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    
    }
    }
    
    @Override
    public void delete(User user) {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "delete from table_user where Username= ?";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    try {
    preparedStatement= connection.prepareStatement(sql);
    preparedStatement.setString(1, user.getUsername());
    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 table_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(String Username) {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from table_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.setPassword(resultSet.getString("Password"));
    user.setUsername("Username");
    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() {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from table_user";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet=null;
    List<User> users=new ArrayList<User>();
    User user=null;
    try {
    preparedStatement=connection.prepareStatement(sql);
    resultSet=preparedStatement.executeQuery();
    while(resultSet.next())
    {
    user=new User();
    user.setPassword(resultSet.getString("Password"));
    user.setUsername(resultSet.getString("Username"));
    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;
    }
    public void bianli(User user)
    {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select count(*) from table_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("登陆失败,用户不存在") ;
    }
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //关闭资源
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    
    }
    }
    public int empty(User user)
    {
    int m=0;
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select count(*) from table_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)
    {
    m=1;
    }
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //关闭资源
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    
    }
    return m;
    }
    }
    
    package com.jaovo.msg.model;
    
    public class User {
    private String Username;
    private String Nickname;
    private String Password;
    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 {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    } catch (InstantiationException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IllegalAccessException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String user = "sa";
    String password = "zlq521415";
    String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Users";// 数据源
    
    Connection con =null;
    try {
    con = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }// 连接数据库对象
    
    return con;
    }
    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 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="userInput.jsp"></jsp:forward>
    <%
    }
    User user = new User();
    
    user.setUsername(username);
    user.setPassword(password);
    user.setNickname(nickname);
    
    UserDaoImpl userDao = new UserDaoImpl();
    try{
    userDao.add(user);
    %>
    
    
    注册成功!!<br>
    <a href="enterInput.jsp">返回登录</a><br>
    <%
    }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");
    	if(username == null || "".equals(username.trim())){
    		request.setAttribute("error", "用户名不能为空");
    	
    %>
    	<jsp:forward page="deleteInput.jsp"></jsp:forward>
    <%
    }
    	User user = new User();
    	
    	user.setUsername(username);
    	user.setPassword(password);
    	
    	UserDaoImpl userDao = new UserDaoImpl();
    	if(userDao.empty(user)==0)
    	{
    		request.setAttribute("error1", "用户不存在");
    %>
    <jsp:forward page="deleteInput.jsp"></jsp:forward>
    <%
    	}
    	try{
    		
    	userDao.delete(user);
    %>
    	注销成功!!<br>
    	<a href="enterInput.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>
    	<title>用户注销页面</title>
    </head>
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    	<%=request.getAttribute("error") %>
    	<%=request.getAttribute("error1") %>
    	 <form action="delete.jsp" method="get">
    		<table align="center" border="1" width="500">
    		<h2  align="center">用户注销界面</h2>
    			<tr>
    				<td>用户名称 : </td>
    				<td>
    					<input type="text" name="username" />
    				</td>
    			</tr>
    				<tr>
        			<td>用户密码:</td>
        			<td>
        				<input type="password" name="password" />
        			</td>
        		</tr>
        		<tr align="center">
        			<td colspan="2">
        				<input type="submit" 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 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>
      <%
    	//接收客户端传递过来的参数
    	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="userInput.jsp"></jsp:forward>
    	<%
    }
    	User user = new User();
    	
    	user.setUsername(username);
    	user.setPassword(password);
    	user.setNickname(nickname);
    	
    	UserDaoImpl userDao = new UserDaoImpl();
    	try{
    	userDao.bianli(user);
    %>
    	登陆成功!!<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>
    	<title>用户登陆页面</title>
    </head>
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    	<%=request.getAttribute("error") %>
    	 <form action="enter.jsp" method="get">
    		<table align="center" border="1" width="500">
    		<h2  align="center">登录界面</h2>
    			<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>
        		<script>
                 function a(){
                           window.open("userInput.jsp",'_blank') 
                               }
                </script>
                <script>
                 function b(){
                           window.open("deleteInput.jsp",'_blank') 
                               }
                </script>
                 <script>
                 function c(){
                           window.open("updateInput.jsp",'_blank') 
                               }
                </script>
                 <script>
                 function d(){
                           window.open("list.jsp",'_blank') 
                               }
                </script>
        		<tr align="center">
        			<td colspan="2">
        				<input type="submit" value="登陆" />
        				<input type="button" value="注册" onclick="a()"/>
        			</td>
        		</tr>
        		<tr align="center">
        		     <td  colspan="2">
        		     <input type="button" value="注销用户" onclick="b()"/>
        		     <input type="button" value="修改密码" onclick="c()"/>
        		     </td>
        		</tr>
        		<tr align="center">
        		     <td  colspan="2">
        		     <input type="button" value="显示所有用户" onclick="d()"/>
        		     </td>
        		</tr>
        		     
    		</table>
    	</form>
    </body>
    </html>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@page import="java.util.List"%>
    <%@page import="com.jaovo.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>用户展示界面</title>
    </head>
    <%
    	UserDaoImpl userDao = new UserDaoImpl();
    	List<User> users = userDao.load();
    %>
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    <form action="delete.jsp" method="get">
    	<table align="center" border="1" width="500">
    		<tr>
    			<td>用户名称</td>
    			<td>用户密码</td>
    			<td>用户昵称</td>
    		</tr>
    		<%
    			for( User user : users ){
    	           
    		%>
    		<tr>
    			<td> <%=user.getUsername() %></td>
    			<td> <%=user.getPassword() %></td>
    			<td> <%=user.getNickname() %></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="updateInput.jsp"></jsp:forward>
    <%
    }
    	User user = new User();
    	
    	user.setUsername(username);
    	user.setPassword(password);
    	user.setNickname(nickname);
    	
    	UserDaoImpl userDao = new UserDaoImpl();
    	if(userDao.empty(user)==0)
    	{
    		request.setAttribute("error1", "用户不存在");
    %>
    <jsp:forward page="updateInput.jsp"></jsp:forward>
    <%
    	}
    	try{
    		
    	userDao.delete(user);
    	userDao.add(user);
    %>
    	修改成功!!<br>
    	<a href="enterInput.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>
    	<title>用户修改页面</title>
    </head>
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    	<%=request.getAttribute("error") %>
    	<%=request.getAttribute("error1") %>
    	 <form action="update.jsp" method="get">
    		<table align="center" border="1" width="500">
    		<h2  align="center">用户修改界面</h2>
    			<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 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 background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    	<%=request.getAttribute("error") %>
    	 <form action="add.jsp" method="get">
    		<table align="center" border="1" width="500">
    		<h2  align="center">注册界面</h2>
    			<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>
    

      结果截图:

     三.希望和目标:

    当完成这门课程之后希望达到老师说的那样能独立完成一个系统,连接数据库并实现增删改查,并对数据库进行更多的操作,学习更多有关网页设计方面的知识,能让系统页面更加美观。并学习好如何设计一款安卓端软件 ,在寒假时争取为家人开发一款软件。我自觉自己在计算机方面的脑筋转的不太快,所以在软件工程概论这门课的学习中,希望能够跟上建民老师的进度,并在此基础上多学习一些其他知识,提高技术水平。

    计划花费的时间:如果有空的话,每周都要有5天中花费时间在练习学过的代码,或者学习其他东西。

  • 相关阅读:
    快排原理讲解
    Kafka原理详解
    java中的基本数据类型转换
    centos7关闭防火墙
    安装Linux基本工具
    Kibana笔记
    虚拟机配置net模式
    2019-10-12,html+php+mysql简单留言板,作业
    2019-10-11:渗透测试,基础学习,php+mysql连接,笔记
    2019-10-10:渗透测试,基础学习,mysql语法基础,笔记
  • 原文地址:https://www.cnblogs.com/zhangliqiangvictory/p/7883805.html
Copyright © 2011-2022 走看看