登录界面和连接 :
package com.artisan.dao;
import com.artisan.model.Admin;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AdminDao extends BaseDao {
/**
* 管理员登陆
*/
public Admin login(Admin admin){
String sql = "select * from s_admin where name=? and password=?";
Admin adminRst = null;
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setString(1, admin.getName());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
adminRst = new Admin();
adminRst.setId(executeQuery.getInt("id"));
adminRst.setName(executeQuery.getString("name"));
adminRst.setPassword(executeQuery.getString("password"));
adminRst.setCreateDate(executeQuery.getString("createDate"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return adminRst;
}
public String editPassword(Admin admin,String newPassword){
String sql = "select * from s_admin where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = con.prepareStatement(sql);
prst.setInt(1, admin.getId());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(!executeQuery.next()){
String retString = "旧密码错误!";
return retString;
}
id = executeQuery.getInt("id");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//把sql语句传给数据库操作对象
String retString = "修改失败";
String sqlString = "update s_admin set password = ? where id = ?";
try {
prst = con.prepareStatement(sqlString);
prst.setString(1, newPassword);
prst.setInt(2, id);
int rst = prst.executeUpdate();
if(rst > 0){
retString = "密码修改成功!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//把sql语句传给数据库操作对象
return retString;
}
}
package com.artisan.dao;
import java.sql.Connection;
import java.sql.SQLException;
import com.artisan.util.DbUtil;
/**
*
* @author llq
*创建对数据库连接对象,整个项目与数据库打交道都用这一个对象.
*/
public class BaseDao {
public Connection con = new DbUtil().getCon();
public void closeDao(){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
班级信息的代码:
package com.artisan.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.artisan.model.StudentClass;
import com.artisan.util.StringUtil;
/**
*
* 班级信息与数据库的操作
* @author llq
*
*/
public class ClassDao extends BaseDao {
public boolean addClass(StudentClass scl){
String sql = "insert into s_class values(null,?,?)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, scl.getName());
preparedStatement.setString(2, scl.getInfo());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List getClassList(StudentClass studentClass){
List retList = new ArrayList();
String sqlString = "select * from s_class";
if(!StringUtil.isEmpty(studentClass.getName())){
sqlString += " where name like '%"+studentClass.getName()+"%'";
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString);
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
StudentClass sc = new StudentClass();
sc.setId(executeQuery.getInt("id"));
sc.setName(executeQuery.getString("name"));
sc.setInfo(executeQuery.getString("info"));
retList.add(sc);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
public boolean delete(int id){
String sql = "delete from s_class where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean update(StudentClass sc){
String sql = "update s_class set name=?, info=? where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, sc.getName());
preparedStatement.setString(2, sc.getInfo());
preparedStatement.setInt(3, sc.getId());
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
课程信息的操作:
package com.artisan.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.artisan.model.Course;
import com.artisan.util.StringUtil;
/**
*
* @author llq
*课程信息操作数据库
*/
public class CourseDao extends BaseDao {
public boolean addCourse(Course course){
String sql = "insert into s_course values(null,?,?,?,?,0)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, course.getName());
preparedStatement.setInt(2, course.getTeacher_id());
preparedStatement.setInt(3, course.getMax_student_num());
preparedStatement.setString(4, course.getInfo());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List getCourseList(Course course){
List retList = new ArrayList();
StringBuffer sqlString = new StringBuffer("select * from s_course");
if(!StringUtil.isEmpty(course.getName())){
sqlString.append(" and name like '%"+course.getName()+"%'");
}
if(course.getTeacher_id() != 0){
sqlString.append(" and teacher_id ="+course.getTeacher_id());
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
Course c = new Course();
c.setId(executeQuery.getInt("id"));
c.setName(executeQuery.getString("name"));
c.setTeacher_id(executeQuery.getInt("teacher_id"));
c.setMax_student_num(executeQuery.getInt("max_student_num"));
c.setInfo(executeQuery.getString("info"));
c.setSelected_num(executeQuery.getInt("selected_num"));
retList.add(c);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
public boolean delete(int id){
String sql = "delete from s_course where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean update(Course course){
String sql = "update s_course set name=?, teacher_id=?,max_student_num=?,info=? where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, course.getName());
preparedStatement.setInt(2, course.getTeacher_id());
preparedStatement.setInt(3, course.getMax_student_num());
preparedStatement.setString(4, course.getInfo());
preparedStatement.setInt(5, course.getId());
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean selectedEnable(int course_id){
String sql = "select * from s_course where id=?";
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setInt(1, course_id);
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
int max_student_num = executeQuery.getInt("max_student_num");
int selected_num = executeQuery.getInt("selected_num");
if(selected_num >= max_student_num)return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public boolean updateSelectedNum(int course_id,int num){
String sql = "update s_course set selected_num = selected_num + ? where id = ?";
if(num < 0){
sql = "update s_course set selected_num = selected_num - ? where id = ?";
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, 1);
preparedStatement.setInt(2, course_id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
学生选课操作界面:
package com.artisan.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.artisan.model.SelectedCourse;
/**
*
* @author llq
*选课表操作
*/
public class SelectedCourseDao extends BaseDao {
public boolean addSelectedCourse(SelectedCourse selectedCourse){
String sql = "insert into s_selected_course values(null,?,?)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, selectedCourse.getStudent_id());
preparedStatement.setInt(2, selectedCourse.getCourse_id());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean updateSelectedCourse(SelectedCourse selectedCourse){
String sql = "update s_selected_course set student_id = ?,course_id = ? where id = ?";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, selectedCourse.getStudent_id());
preparedStatement.setInt(2, selectedCourse.getCourse_id());
preparedStatement.setInt(3, selectedCourse.getId());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List getSelectedCourseList(SelectedCourse selectedCourse){
List retList = new ArrayList();
StringBuffer sqlString = new StringBuffer("select * from s_selected_course");
if(selectedCourse.getStudent_id() != 0){
sqlString.append(" and student_id = "+selectedCourse.getStudent_id());
}
if(selectedCourse.getCourse_id() != 0){
sqlString.append(" and course_id ="+selectedCourse.getCourse_id());
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
SelectedCourse sc = new SelectedCourse();
sc.setId(executeQuery.getInt("id"));
sc.setStudent_id(executeQuery.getInt("student_id"));
sc.setCourse_id(executeQuery.getInt("course_id"));
retList.add(sc);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
public boolean isSelected(SelectedCourse selectedCourse){
String sql = "select * from s_selected_course where student_id=? and course_id = ?";
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setInt(1, selectedCourse.getStudent_id());
prst.setInt(2, selectedCourse.getCourse_id());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean delete(int id){
String sql = "delete from s_selected_course where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
学生信息的界面:
package com.artisan.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.model.StudentClass;
import com.artisan.util.StringUtil;
public class StudentDao extends BaseDao {
public boolean addStudent(Student student){
String sql = "insert into s_student values(null,?,?,?,?)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setInt(2, student.getClassId());
preparedStatement.setString(3, student.getPassword());
preparedStatement.setString(4, student.getSex());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List getStudentList(Student student){
List retList = new ArrayList();
StringBuffer sqlString = new StringBuffer("select * from s_student");
if(!StringUtil.isEmpty(student.getName())){
sqlString.append(" and name like '%"+student.getName()+"%'");
}
if(student.getClassId() != 0){
sqlString.append(" and classId ="+student.getClassId());
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
Student s = new Student();
s.setId(executeQuery.getInt("id"));
s.setName(executeQuery.getString("name"));
s.setClassId(executeQuery.getInt("classId"));
s.setSex(executeQuery.getString("sex"));
s.setPassword(executeQuery.getString("password"));
retList.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
public boolean delete(int id){
String sql = "delete from s_student where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean update(Student student){
String sql = "update s_student set name=?, classId=?,sex=?,password=? where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setInt(2, student.getClassId());
preparedStatement.setString(3, student.getSex());
preparedStatement.setString(4, student.getPassword());
preparedStatement.setInt(5, student.getId());
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public String editPassword(Student student,String newPassword){
String sql = "select * from s_student where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = con.prepareStatement(sql);
prst.setInt(1, student.getId());
prst.setString(2, student.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(!executeQuery.next()){
String retString = "旧密码错误!";
return retString;
}
id = executeQuery.getInt("id");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//把sql语句传给数据库操作对象
String retString = "修改失败";
String sqlString = "update s_student set password = ? where id = ?";
try {
prst = con.prepareStatement(sqlString);
prst.setString(1, newPassword);
prst.setInt(2, id);
int rst = prst.executeUpdate();
if(rst > 0){
retString = "密码修改成功!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//把sql语句传给数据库操作对象
return retString;
}
public Student login(Student student){
String sql = "select * from s_student where name=? and password=?";
Student studentRst = null;
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setString(1, student.getName());
prst.setString(2, student.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
studentRst = new Student();
studentRst.setId(executeQuery.getInt("id"));
studentRst.setClassId(executeQuery.getInt("classId"));
studentRst.setName(executeQuery.getString("name"));
studentRst.setPassword(executeQuery.getString("password"));
studentRst.setSex(executeQuery.getString("sex"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentRst;
}
}
老师信息的界面:
package com.artisan.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;
public class TeacherDao extends BaseDao {
public boolean addTeacher(Teacher teacher){
String sql = "insert into s_teacher values(null,?,?,?,?,?)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, teacher.getName());
preparedStatement.setString(2, teacher.getSex());
preparedStatement.setString(3, teacher.getTitle());
preparedStatement.setInt(4, teacher.getAge());
preparedStatement.setString(5, teacher.getPassword());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List getTeacherList(Teacher teacher) {
// TODO Auto-generated method stub
List retList = new ArrayList();
StringBuffer sqlString = new StringBuffer("select * from s_teacher");
if(!StringUtil.isEmpty(teacher.getName())){
sqlString.append(" where name like '%"+teacher.getName()+"%'");
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString());
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
Teacher t = new Teacher();
t.setId(executeQuery.getInt("id"));
t.setName(executeQuery.getString("name"));
t.setSex(executeQuery.getString("sex"));
t.setTitle(executeQuery.getString("title"));
t.setAge(executeQuery.getInt("age"));
t.setPassword(executeQuery.getString("password"));
retList.add(t);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
public boolean delete(int id){
String sql = "delete from s_teacher where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean update(Teacher teacher){
String sql = "update s_teacher set name=?, sex=?,title=?,age=?,password=? where id=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, teacher.getName());
preparedStatement.setString(2, teacher.getSex());
preparedStatement.setString(3, teacher.getTitle());
preparedStatement.setInt(4, teacher.getAge());
preparedStatement.setString(5, teacher.getPassword());
preparedStatement.setInt(6, teacher.getId());
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public Teacher login(Teacher teacher){
String sql = "select * from s_teacher where name=? and password=?";
Teacher teacherRst = null;
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setString(1, teacher.getName());
prst.setString(2, teacher.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
teacherRst = new Teacher();
teacherRst.setId(executeQuery.getInt("id"));
teacherRst.setName(executeQuery.getString("name"));
teacherRst.setPassword(executeQuery.getString("password"));
teacherRst.setSex(executeQuery.getString("sex"));
teacherRst.setAge(executeQuery.getInt("Age"));
teacherRst.setTitle(executeQuery.getString("title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return teacherRst;
}
public String editPassword(Teacher teacher,String newPassword){
String sql = "select * from s_teacher where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = con.prepareStatement(sql);
prst.setInt(1, teacher.getId());
prst.setString(2, teacher.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(!executeQuery.next()){
String retString = "旧密码错误!";
return retString;
}
id = executeQuery.getInt("id");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//把sql语句传给数据库操作对象
String retString = "修改失败";
String sqlString = "update s_teacher set password = ? where id = ?";
try {
prst = con.prepareStatement(sqlString);
prst.setString(1, newPassword);
prst.setInt(2, id);
int rst = prst.executeUpdate();
if(rst > 0){
retString = "密码修改成功!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//把sql语句传给数据库操作对象
return retString;
}
}