软件工程概论第一节

一.源程序代码:

package com.jaovo.msg.dao;

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


import java.sql.Connection;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.mobel.User;


public class UserdaoImpl implements IUserDao{

 @Override
 public void add(User user) {
  // TODO Auto-generated method stub
  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) values (?,?,?)";
  preparedStatement = connection.prepareStatement(sql);
  preparedStatement.setString(1, user.getUsername());
  preparedStatement.setString(2, user.getPasswork());
  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) {
  // TODO Auto-generated method stub
  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) {
  // TODO Auto-generated method stub
  Connection connection=DBUtil.getConnection();
  String sql = "update t_user set password = ?, nickname = ? where id = ?";
  PreparedStatement preparedStatement = null;
  try {
   preparedStatement = connection.prepareStatement(sql);
   preparedStatement.setString(1, user.getPasswork());
   preparedStatement.setString(2, user.getNickname());
   preparedStatement.setInt(3, user.getId());
   preparedStatement.executeQuery();
  } 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();
  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.setPasswork(resultSet.getString("passwork"));
    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();
  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.setUsername(username);
    user.setId(resultSet.getInt("id"));
    user.setPasswork(resultSet.getString("passwork"));
    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();
  String sql = "select * from t_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.setId(resultSet.getInt("id"));
    user.setUsername(resultSet.getString("username"));
    user.setPasswork(resultSet.getString("passwork"));
    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.dao;

import java.util.List;

import com.jaovo.msg.mobel.User;

public interface IUserDao {
 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.mobel;

public class User {
 private int id;
 private String username;
 private String nickname;
 private String passwork;
 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 getPasswork() {
  return passwork;
 }
 public void setPasswork(String passwork) {
  this.passwork = passwork;
 }
 
}

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;

import org.apache.tomcat.dbcp.dbcp2.DriverManagerConnectionFactory;

public class DBUtil {
 public static Connection getConnection() {
  //1.加载驱动
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String user = "root";
  String password = "root";
  String url = "jdbc:mysql://localhost:3306/jaovo_msg";
  Connection connection = null;
  try {
   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(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>
 <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>
     <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>
  </table>
 </form>
</body>
</html>

<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserdaoImpl"%>
<%@page import="com.jaovo.msg.mobel.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.setPasswork(password);
 user.setNickname(nickname);

 UserdaoImpl userDao = new UserdaoImpl();
 try{
 userDao.add(user);
 %>


 用户保存成功!!<br>
 <a href="addInput.jsp">继续添加</a><br>
 <a href="#">用户列表</a>
<%
 }catch(UserException e){
%>
 <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
 }
%>
</html>

二.效果截图:

原文地址:https://www.cnblogs.com/y862621115/p/7886376.html