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();
}
}
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;
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();
}
}
}
//连接数据库
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 class UserException extends RuntimeException{
public UserException() {
super();
// TODO Auto-generated constructor stub
}
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
}
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public UserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
super(message, cause);
// TODO Auto-generated constructor stub
}
public UserException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
super(message);
// TODO Auto-generated constructor stub
}
public UserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
super(cause);
// TODO Auto-generated constructor stub
}
}
ValidateUtil.java
package com.jaovo.msg.Util;
import java.util.HashMap;
import java.util.Map;
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;
}
}
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;
}
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;
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");
}
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 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();
}
}
// 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();
}
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 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 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;
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);
}
}
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 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;
}
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;
}
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;
}
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;
}
}
这就是完整的课程管理系统,增删改查