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;
}
}