package cn.zut.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cn.zut.dao.LoginDao;
import cn.zut.model.Employees;
import cn.zut.model.User;
import cn.zut.mysql.DBUtil;
public class LoginDaoImpl implements LoginDao {
// 登录
@Override
public User findUserByLogin(String username, String password) throws SQLException {
User user = null;
Connection connection = null;
//操作数据库的对象
PreparedStatement preparedStatement = null;
//储存结果集
ResultSet resultSet = null;
//连接数据库
connection = DBUtil.connectionMysql();
String sql = "select * from login_info WHERE username= ? and password = ?;";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
//执行SQL语句返回结果,若查到,返回结果,查不到,返回null
resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
while (resultSet.next()) {
user = new User();
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setName(resultSet.getString("name"));
user.setEmail(resultSet.getString("email"));
user.setAddress(resultSet.getString("address"));
user.setPhone_number(resultSet.getString("phone_number"));
}
DBUtil.closeAll(connection, preparedStatement, resultSet);
return user;
}
@Override
public List<Employees> getEmployeesInfo(String name) throws SQLException {
Employees employees = null;
List<Employees> list = new ArrayList<Employees>();
String sql = "select * from employees where name ='" + name + "';";
ResultSet resultSet = DBUtil.getResult(sql);
while (resultSet.next()) {
employees = new Employees();
employees.setId(resultSet.getString(1));
employees.setDepartment(resultSet.getString(2));
employees.setName(resultSet.getString(3));
employees.setSex(resultSet.getString(4));
employees.setSalary(resultSet.getString(5));
employees.setEmail(resultSet.getString(7));
employees.setPhoneNumber(resultSet.getString(6));
list.add(employees);
}
return list;
}
@Override
public List<Employees> getAllEmployeesInfo() throws SQLException {
List<Employees> list = new ArrayList<Employees>();
Employees employees = null;
ResultSet resultSet = null;
String sql = "select * from employees;";
resultSet = DBUtil.getResult(sql);
while (resultSet.next()) {
employees = new Employees();
employees.setId(resultSet.getString(1));
employees.setDepartment(resultSet.getString(2));
employees.setName(resultSet.getString(3));
employees.setSex(resultSet.getString(4));
employees.setSalary(resultSet.getString(5));
employees.setEmail(resultSet.getString(7));
employees.setPhoneNumber(resultSet.getString(6));
list.add(employees);
}
return list;
}
@Override
public void deleteEmployeesInfo(String id) {
String sql = "delete from employees where id='" + id + "';";
Connection connection = DBUtil.connectionMysql();
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新
public Employees update(String id) throws SQLException {
Employees employees = null;
System.out.println("update : id" + id);
String sql = "select * from employees where id = '" + id + "';";
ResultSet resultSet = DBUtil.getResult(sql);
while (resultSet.next()) {
employees = new Employees();
employees.setId(resultSet.getString(1));
employees.setDepartment(resultSet.getString(2));
employees.setName(resultSet.getString(3));
employees.setSex(resultSet.getString(4));
employees.setSalary(resultSet.getString(5));
employees.setEmail(resultSet.getString(7));
employees.setPhoneNumber(resultSet.getString(6));
}
System.out.println("update employees:" + employees);
return employees;
}
@Override
public boolean doUpdate(Employees e) throws SQLException {
Connection connection = DBUtil.connectionMysql();
PreparedStatement preparedStatement = null;
// preparedStatement 的sql 语句中不能用单引号
String sql = " update employees set department = ? , name=? , sex=? , salary=? , phoneNumber=?,email=? where id = ?;";
boolean flag = false;
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, e.getDepartment());
preparedStatement.setString(2, e.getName());
preparedStatement.setString(3, e.getSex());
preparedStatement.setString(4, e.getSalary());
preparedStatement.setString(5, e.getPhoneNumber());
preparedStatement.setString(6, e.getEmail());
preparedStatement.setString(7, e.getId());
preparedStatement.executeUpdate();
DBUtil.closeAll(connection, preparedStatement, null);
return flag;
}
// 插入
public void insertEmployeesInfo(Employees e) throws SQLException {
Connection connection = DBUtil.connectionMysql();
PreparedStatement preparedStatement = null;
String sql = "insert into employees values( ?, ?, ?, ?, ?, ?,?);";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, e.getId());
preparedStatement.setString(2, e.getDepartment());
preparedStatement.setString(3, e.getName());
preparedStatement.setString(4, e.getSex());
preparedStatement.setString(5, e.getSalary());
preparedStatement.setString(6, e.getPhoneNumber());
preparedStatement.setString(7, e.getEmail());
preparedStatement.execute();
} catch (Exception e1) {
e1.printStackTrace();
}
DBUtil.closeAll(connection, preparedStatement, null);
}
@Override
public void addAdmin(User user) {
Connection connection = DBUtil.connectionMysql();
PreparedStatement preparedStatement = null;
String sql = "insert into login_info values( ?, ?, ?, ?, ?, ?);";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getName());
preparedStatement.setString(4, user.getEmail());
preparedStatement.setString(5, user.getPhone_number());
preparedStatement.setString(6, user.getAddress());
preparedStatement.execute();
} catch (Exception e1) {
e1.printStackTrace();
}
DBUtil.closeAll(connection, preparedStatement, null);
}
@Override
public List<User> getAllAddminInfo() throws SQLException {
List<User> list = new ArrayList();
User user = null;
String sql = "select * from login_info";
ResultSet resultSet = DBUtil.getResult(sql);
while (resultSet.next()) {
user = new User();
user.setUsername(resultSet.getString(1));
user.setPassword(resultSet.getString(2));
user.setName(resultSet.getString(3));
user.setEmail(resultSet.getString(4));
user.setPhone_number(resultSet.getString(5));
user.setAddress(resultSet.getString(6));
list.add(user);
}
return list;
}
@Override
public User getAdmin(String username, String password) throws SQLException {
User user = null;
String sql = "select * from login_info where username = '" + username + "' and password = '" + password + "';";
ResultSet resultSet = DBUtil.getResult(sql);
while (resultSet.next()) {
user = new User();
user.setUsername(resultSet.getString(1));
user.setPas