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

时间: 2024-10-09 09:43:27

JavaWeb——课程管理系统(2).java---18.11.29的相关文章

课程管理系统

javaweb 课程管理系统代码分析 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>

基于SSM的健身俱乐部管理系统-java健身俱乐部管理系统javaweb健身房管理系统

基于SSM的健身俱乐部管理系统-java健身俱乐部管理系统javaweb健身房管理系统 1.包含源程序,数据库脚本.代码和数据库脚本都有详细注释.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善开发环境:Eclipse ,MYSQL,JDK1.7,Tomcat 7涉及技术点:MVC模式.SpringMvc.Mybatis.Spring.HTML.JavaScript.CSS.JQUERY.DWR.Ajax等系统采用Mybatis框架实现ORM对象关系映射,前台JSP实现,后台spring

如何实现最基本的课程管理系统(程序库的增删改查)

一直在听建民老师说,javaweb的精髓是Jsp+servlet+javabean.在完成这个系统之前,毕竟没有学习过javaweb,Jsp和servlet我是知道的,但不会在servlet和jsp之间相互传值以及应用,javabean是一点没有听说过.在这样的基础下,没办法逃脱测试的情况下,只能硬着头皮做了.数据库这方面,之前安装过mysql和可视化工具Navicat,但没有在eclipse中连接过,在数据库连接这方面基本也是空白.在慕课网和bilibili上找了无数视频,终于有了点头绪,下面

信息管理系统(java)

目录 摘要:.... 2 绪论.... 2 课程目的.... 2 课程研究的意义... 2 一.java课程设计目的和要求... 3 1.1课程设计目的... 3 1.2课程设计要求... 3 1.3系统功能图... 4 二.程序的设计与实现... 4 2.1登陆界面的设计与实现... 4 2.2密码注册界面的设计和实现... 5 2.3密码找回界面的设计和实现... 6 2.4信息管理主界面设计和实现... 7 2.5信息修改界面的设计和实现... 12 2.6新建文件的设计和实现... 14

选课不再纠结!10个课程搞定Java!

身在茫茫的课程海洋的童鞋们,是不是很迷茫?数百个课程我该学哪个?这个课程质量怎样啊?学院课程小管家整理出了最受欢迎的10个Java类视频课程~迷途中的童鞋们终于不再纠结了!!!10各课程包含了初中高3各阶段,小管家是不是很贴心^_^ 大家有想要的专题欢迎在评论中说明哟,管家会根据需求的情况,进行下一个专题的选择~~~~ (一)  小白进阶必学~ 1.http://edu.51cto.com/course/course_id-4191.html 刘英杰 课程名称:JavaWeb(JSP+servl

日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09

作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09   据日本政府提供的数据,日本2018年应届毕业生的"求人倍率"已经达到了1.78倍.换言之,就是100名大学生里有178个就业岗位可选择,即使所有大学毕业生都进入劳动力市场,也还有78个岗位空缺.劳动力现状是寒冷冬天,而学生们确实迎来了就业春天. 在众多岗位当中,日本的信息技术(IT)领域采用的外国人留学生最多.去年,已经在日本企业就职的留学生当中(除去制造行业),27.3%的毕业生进入了IT行业.根

[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长:

java进阶11 static final关键字

static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块. 被static修饰的成员变量和成员方法独立于该类的任何对象,也就是说. 它不依赖类的特定的实例,被类的所有实例共享. package Static; public class Static { /* * 使用的情况 * 1:在对象之间共享值时 * 2:方便访问变量 * */ public static void main(String[] args){ Sta

11.28 限定某个目录禁止解析php;11.29 限制user_agent;11.30,11.31 php相关配置(上下)

扩展: apache开启压缩  http://www.aminglinux.com/bbs/thread-5528-1-1.html apache2.2到2.4配置文件变更  http://www.aminglinux.com/bbs/thread-7292-1-1.html apache options参数  http://www.aminglinux.com/bbs/thread-1051-1-1.html apache禁止trace或track防止xss  http://www.aming