JavaWeb——课程管理系统(2).java---18.11.29


DaoFactory.java

package com.jaovo.msg.Util;
import com.jaovo.msg.dao.UserDaoImpl;
public class DaoFactory {
 public static UserDaoImpl getDaoImpl() {
  return new UserDaoImpl();
 }
}
 
DBUtil.java
 
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 = "root";
  String url = "jdbc:mysql://localhost:3306/t_user?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
  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();
  }
 }
 
}
 
UserException.java
 
package com.jaovo.msg.Util;
@SuppressWarnings("serial")
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
 }
 
}
 
ValidateUtil.java
 
package com.jaovo.msg.Util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ValidateUtil {
 public static  boolean validateNull(HttpServletRequest request,String[] fileds) {
  boolean validate = true;
  //map对象用来装载不同的错误信息
  Map<String,String> errorMsg = new HashMap<String, String>();
  for(String filed :fileds) {
   String value = request.getParameter(filed);
   if (value == null || "".equals(value.trim())) {
    validate = false;
    errorMsg.put(filed, filed+"不能为空");
   }
   if (!validate) {
    request.setAttribute("errormsg", errorMsg);
   }
   
  }
  
  return validate;
 }
 public static String showError(HttpServletRequest request , String filed) {
  @SuppressWarnings("unchecked")
  Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
  if (errorMsg == null) {
   return "";
  }
  String msg = errorMsg.get(filed);
  if (msg == null) {
   return "";
  }
  return msg;
 }
 
}
 
User.java
 
package com.jaovo.msg.model;
public class User {
 
 private int id;
 private String  name;
 private String  time;
 private String  jieci;
 private String  college;
 private String  tid;
 private String  tname;
 private String  number;
 public int getId()
 {
  return id;
 }
 public void setId(int id)
 {
  this.id = id;
 }
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 public String getTime()
 {
  return time;
 }
 public void setTime(String time)
 {
  this.time = time;
 }
 public String getJieci()
 {
  return jieci;
 }
 public void setJieci(String jieci)
 {
  this.jieci = jieci;
 }
 public String getCollege()
 {
  return college;
 }
 public void setCollege(String college)
 {
  this.college = college;
 }
 public String getTid()
 {
  return tid;
 }
 public void setTid(String tid)
 {
  this.tid = tid;
 }
 public String getTname()
 {
  return tname;
 }
 public void setTname(String tname)
 {
  this.tname = tname;
 }
 public String getNumber()
 {
  return number;
 }
 public void setNumber(String number)
 {
  this.number = number;
 }
 
}
 
CharFilter.java
package com.jaovo.msg.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharFilter implements Filter{
 String encoding = null;
 @Override
 public void init(FilterConfig filterConfig) throws ServletException{
  encoding = filterConfig.getInitParameter("encoding");
 }
 
 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
   throws IOException, ServletException {
//   request.setCharacterEncoding("utf-8");
   request.setCharacterEncoding(encoding);
   chain.doFilter(request, response);
 }
 public void destroy() {
  // TODO Auto-generated method stub
  System.out.println();
 }
}
 
IUserDao.java
 
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(int id);
 public void update(User user);
 public User load(int id);
 public User load(String name);
 public List<User> load();
 
}
 
UserDaoImpl.java
 
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 {
 //增
 public void add(User user) {
  //获得链接对象
  Connection connection = DBUtil.getConnection();
  //准备sql语句
  String sql = "select count(*) from class where name = ?";
 
  //创建语句传输对象
  PreparedStatement preparedStatement = null;
  ResultSet resultSet = null;
  try {
   preparedStatement = connection.prepareStatement(sql);
   preparedStatement.setString(1, user.getName());
   //接收结果集
   resultSet = preparedStatement.executeQuery();
   //遍历结果集
   while(resultSet.next()) {
    if (resultSet.getInt(1) > 0) {
     throw new UserException("用户已存在") ;
    }
   }
   
   sql = "insert into class(name,time,jieci,college,tid,tname,number) value (?,?,?,?,?,?,?)";
   preparedStatement = connection.prepareStatement(sql);
   //添加
   preparedStatement.setString(1, user.getName());
   preparedStatement.setString(2, user.getTime());
   preparedStatement.setString(3, user.getJieci());
   preparedStatement.setString(4, user.getCollege());
   preparedStatement.setString(5, user.getTid());
   preparedStatement.setString(6, user.getTname());
   preparedStatement.setString(7, user.getNumber());
   preparedStatement.executeUpdate();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   //关闭资源
   DBUtil.close(resultSet);
   DBUtil.close(preparedStatement);
   DBUtil.close(connection);
  }
  
 }
 //删
 public void delete(int id) {
  //获得链接对象
  Connection connection = DBUtil.getConnection();
  String sql = "delete from class 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 class set time = ? , jieci=? , college=? , tid=? , tname=? , number=? where id = ?";
  //创建语句传输对象
  PreparedStatement preparedStatement = null;
  try {
   preparedStatement = connection.prepareStatement(sql);
   preparedStatement.setString(1, user.getTime());
   preparedStatement.setString(2, user.getJieci());
   preparedStatement.setString(3, user.getCollege());
   preparedStatement.setString(4, user.getTid());
   preparedStatement.setString(5, user.getTname());
   preparedStatement.setString(6, user.getNumber());
   preparedStatement.setInt(7, 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 class  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.setName(resultSet.getString("name"));
    user.setTime(resultSet.getString("time"));
    user.setJieci(resultSet.getString("jieci"));
    user.setCollege(resultSet.getString("college"));
    user.setTid(resultSet.getString("tid"));
    user.setTname(resultSet.getString("tname"));
    user.setNumber(resultSet.getString("number"));
   }
  } 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 name) {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
 public List<User> load() {
  Connection connection = DBUtil.getConnection();
  //准备sql语句
  String sql = "select * from class ";
  //创建语句传输对象
  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.setName(resultSet.getString("name"));
    user.setTime(resultSet.getString("time"));
    user.setJieci(resultSet.getString("jieci"));
    user.setCollege(resultSet.getString("college"));
    user.setTid(resultSet.getString("tid"));
    user.setTname(resultSet.getString("tname"));
    user.setNumber(resultSet.getString("number"));
    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;
 }
}
 
这就是完整的课程管理系统,增删改查

转载于:https://www.cnblogs.com/mitang0-0/p/10308328.html

a)构架 该系统采用表示层、业务逻辑层、数据访问层三层构架。 1、在系统主页面,用户可以通过已有的账号密码进行登录,验证成功则进入个人信息页面;也可以进行忘记密码之后的重置操作,验证成功则完成重置并返回系统主页面再次登录。 2、在个人信息页面,用户可以修改当前账号密码,验证成功则完成修改则返回系统主页面。在此之外,不同身份的用户可以有不同的操作。 管理员:查看现有帐号信息,对帐号进行增、删等操作。 教师:查看课程信息,包括预览、下载课件,查看作业情况(提交、批改意见、评分)等;修改课程信息,包括上传、重传课件,下载作业,提交作业批改意见、评分,发布新作业要求,更新系统公告等。 学生:查看课程信息,包括预览、下载课件,查看作业情况(提交、批改意见、评分)等;修改课程信息,包括上传、重传作业等。 b)系统流程 1、表示层效果采用HTML5、CSS等语言和前端框架jQuery,实现用户交互界面。交互界面包括用户登录界面,个人信息展示页面,课程、课件、作业查看页面等。 2、业务逻辑层利用Servlet,JSP来处理来自前端的请求,获取数据库数据,并作出相应的处理,将处理数据提交到前端,显示给用户。 3、数据访问层利用Hibernate架构操作数据库,通过应用程序经过Hiberante持久层来访问数据库。数据访问层的关键是确定表结构和表属性,便于业务逻辑层进行对数据库的查询,良好的数据库结构,对数据操作有很大的用处。 c)系统结构
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值