package com.system.dao;
import com.system.entity.AnalyzeResult;
import com.system.entity.Student;
import com.system.entity.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Vector;
public class JdbcHelper implements JdbcConfig {
//定义连接数据库所需要的对象
private PreparedStatement ps = null;
private ResultSet rs = null;
private Connection ct = null;
//获得数据库的连接
private void init() {
try {
Class.forName(DRIVER);
ct = DriverManager.getConnection(URL, USERNAME, PASSWORD);// 获得数据库连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public JdbcHelper() {
this.init();
}
//获取用户对象,根据传入的用户名,获取对应的用户,并返回用户对象
public User getUser(User user) {
User newUser = new User();
try {
ps = ct.prepareStatement("select * from tb_User where User_name=?");
ps.setString(1, user.getUsername());
rs = ps.executeQuery();
if (rs.next()) {
newUser.setUsername(rs.getString(1)); //设置用户名
newUser.setPassword(rs.getString(2)); //设置密码
}
} catch (SQLException e) {
e.printStackTrace();
}
return newUser;
}
//注册并判断是否成功
public boolean register(User user) {
boolean isSuccess = true;
try {
ps = ct.prepareStatement("insert into tb_User(User_name,Password_) values(?,?)");
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
if (ps.executeUpdate() != 1) {
isSuccess = false;
}
} catch (SQLException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//修改密码并判断是否成功
public boolean update_Password(User user, String new_Password) {
boolean isSuccess = true;
try {
ps = ct.prepareStatement("update tb_User set Password_=? where User_name=?");
ps.setString(1, new_Password);
ps.setString(2, user.getUsername());
if (ps.executeUpdate() != 1) { //执行sql语句
isSuccess = false;
}
} catch (SQLException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//返回所有院系的HashMap集合
public HashMap<String, String> getAllDepartment() {
HashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("", "");//添加一个空的元素
try {
ps = ct.prepareStatement("select * from tb_Department order by Department_ID");
rs = ps.executeQuery();
while (rs.next()) {
map.put(rs.getString(2), rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
//获得对应院系的专业
public Vector<String> getMajor(String department_ID) {
Vector<String> vector = new Vector<String>();
vector.add("");//添加一个空的元素
try {
ps = ct.prepareStatement("select * from tb_Major where Department_ID=? order by Major_ID");
ps.setString(1, department_ID);
rs = ps.executeQuery();
while (rs.next()) {
vector.add(rs.getString(2)); //获得专业名称
}
} catch (SQLException e) {
e.printStackTrace();
}
return vector;
}
//获得所有专业
public HashMap<String, String> getAllMajor() {
HashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("", "");//添加一个空的元素
try {
ps = ct.prepareStatement("select * from tb_Major order by Major_ID");
rs = ps.executeQuery();
while (rs.next()) {
map.put(rs.getString(2), rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
//添加学生并判断是否添加成功
public boolean addStudent(Student student) {
boolean isSuccess = true;
try {
ps = ct.prepareStatement("insert into tb_Student(Student_Id,Student_Name,Student_Sex,Classe,Grade,Major_ID,Department_ID,Major_Name,Department_Name) values(?,?,?,?,?,?,?,?,?)");
ps.setString(1, student.getStudent_ID());
ps.setString(2, student.getStudent_Name());
ps.setString(3, student.getSex());
ps.setString(4, student.getClasse());
ps.setString(5, student.getGrade());
ps.setString(6, student.getMajor_ID());
ps.setString(7, student.getDepartment_ID());
ps.setString(8, student.getMajor_Name());
ps.setString(9, student.getDepartment_Name());
if (ps.executeUpdate() != 1) {
isSuccess = false;
}
} catch (SQLException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//修改学生信息并判断是否修改成功
public boolean updateStudent(Student newStudent, String oldStudentID) {
boolean isSuccess = true;
try {
//update
ps = ct.prepareStatement("update tb_Student set Student_Id=?, Student_Name=?, Student_Sex=? ,Classe=? ,Grade=? ,Major_ID=? ,Department_ID=? ,Major_Name=? ,Department_Name=? where Student_Id=?");
ps.setString(1, newStudent.getStudent_ID());
ps.setString(2, newStudent.getStudent_Name());
ps.setString(3, newStudent.getSex());
ps.setString(4, newStudent.getClasse());
ps.setString(5, newStudent.getGrade());
ps.setString(6, newStudent.getMajor_ID());
ps.setString(7, newStudent.getDepartment_ID());
ps.setString(8, newStudent.getMajor_Name());
ps.setString(9, newStudent.getDepartment_Name());
ps.setString(10, oldStudentID);
if (ps.executeUpdate() != 1) {
isSuccess = false;
}
} catch (SQLException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//根据学号删除学生并返回是否删除成功
public boolean deleteStudent(String studentID) {
boolean isSuccess = true;
try {
ps = ct.prepareStatement("delete from tb_Student where Student_Id=?");
ps.setString(1, studentID);
if (ps.executeUpdate() != 1) {
isSuccess = false;
}
} catch (SQLException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//根据sql语句返回特定的学生集合
public Vector<Student> getStudent(String sql) {
Vector<Student> students = new Vector<Student>();
try {
ps = ct.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setStudent_ID(rs.getString(1));
student.setStudent_Name(rs.getString(2));
student.setSex(rs.getString(3));
student.setGrade(rs.getString(4));
student.setClasse(rs.getString(5));
student.setMajor_ID(rs.getString(6));
student.setMajor_Name(rs.getString(7));
student.setDepartment_ID(rs.getString(8));
student.setDepartment_Name(rs.getString(9));
students.add(student);
}
} catch (SQLException e) {
e.print