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;
}
}
这就是完整的课程管理系统,增删改查