package com.bean;
public class Choice {
public int id;
private String classes;
private String student;
private String teacher;
public int getId() {
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher= teacher;
}
public Choice(int id,String classes,String student,String teacher) {
this.id = id;
this.classes = classes;
this.student = student;
this.teacher = teacher;
}
public Choice(String classes,String student,String teacher) {
this.classes = classes;
this.student = student;
this.teacher = teacher;
}
}
package com.bean;
public class Course {
public int id;
private String number;
private String name;
private String people;
private String teacher;
public Course () {
}
public int getId() {
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPeople(String people) {
this.people = people;
}
public String getPeople() {
return people;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher= teacher;
}
public Course(int id,String number,String name,String people,String teacher) {
this.id = id;
this.number = number;
this.name = name;
this.people = people;
this.teacher = teacher;
}
public Course(String number,String name,String people,String teacher) {
this.number = number;
this.name = name;
this.people = people;
this.teacher = teacher;
}
}
public int id;
private String number;
private String name;
private String sex;
private String classes;
private String major;
public int getId() {
return id;
}
public void setId(int id)
{
this.id=id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Student(int id,String number,String name,String sex,String classes,String major) {
this.id = id;
this.number = number;
this.name = name;
this.sex = sex;
this.classes = classes;
this.major = major;
}
public Student(String number,String name,String sex,String classes,String major) {
this.number = number;
this.name = name;
this.sex = sex;
this.classes = classes;
this.major = major;
}
public Student(int id,String name,String sex,String classes,String major) {
this.id = id;
this.name = name;
this.sex = sex;
this.classes = classes;
this.major = major;
}
}
package com.bean;
public class Teacher {
public int id;
private String number;
private String name;
private String sex;
private String college;
private String type;
public int getId() {
return id;
}
public void id(int id)
{
this.id=id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Teacher(int id,String number,String name,String sex,String college,String type) {
this.id = id;
this.number = number;
this.name = name;
this.sex = sex;
this.college = college;
this.type = type;
}
public Teacher(String number,String name,String sex,String college,String type) {
this.number = number;
this.name = name;
this.sex = sex;
this.college = college;
this.type = type;
}
public Teacher(int id,String name,String sex,String college,String type) {
this.id = id;
this.name = name;
this.sex = sex;
this.college = college;
this.type = type;
}
}
package com.bean;
public class User {
public int id;
private String username;
private String password;
private String type;
public int getId() {
return id;
}
public void id(int id)
{
this.id=id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public User(int id,String username,String password,String type) {
this.id = id;
this.username = username;
this.password = password;
this.type = type;
}
public User(String username,String password,String type) {
this.username = username;
this.password = password;
this.type = type;
}
}
package com.dao;
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 com.bean.Choice;
import com.bean.Course;
import com.bean.Student;
import com.bean.Teacher;
import com.bean.User;
import com.util.Util;
public class Dao {
public List<Course> clist() { // ��ѯ������Ϣ
List<Course> list = new ArrayList<Course>(); // ��������
Connection conn = Util.getConn();
String sql = "select * from class"; // SQL��ѯ���
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()) {
int id= rst.getInt("id"); // �õ�ID
String number = rst.getString("number");
String name= rst.getString("name");
String people = rst.getString("people");
String teacher = rst.getString("teacher");
Course course = new Course(id,number,name,people,teacher);
list.add(course);
}
rst.close(); // �ر�
pst.close(); // �ر�
} catch (SQLException e) {
e.printStackTrace(); // �׳��쳣
}
return list; // ����һ������
}
public List<Student> slist() { // ��ѯ������Ϣ
List<Student> list = new ArrayList<Student>(); // ��������
Connection conn = Util.getConn();
String sql = "select * from student"; // SQL��ѯ���
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()) {
int id= rst.getInt("id"); // �õ�ID
String number = rst.getString("number");
String name= rst.getString("name");
String sex = rst.getString("sex");
String classes = rst.getString("classes");
String major = rst.getString("major");
Student student = new Student(id,number,name,sex,classes,major);
list.add(student);
}
rst.close(); // �ر�
pst.close(); // �ر�
} catch (SQLException e) {
e.printStackTrace(); // �׳��쳣
}
return list; // ����һ������
}
public List<Teacher> tlist() { // ��ѯ������Ϣ
List<Teacher> list = new ArrayList<Teacher>(); // ��������
Connection conn = Util.getConn();
String sql = "select * from teacher"; // SQL��ѯ���
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()) {
int id= rst.getInt("id"); // �õ�ID
String number = rst.getString("number");
String name= rst.getString("name");
String sex = rst.getString("sex");
String college = rst.getString("college");
String type = rst.getString("type");
Teacher teacher = new Teacher(id,number,name,sex,college,type);
list.add(teacher);
}
rst.close(); // �ر�
pst.close(); // �ر�
} catch (SQLException e) {
e.printStackTrace(); // �׳��쳣
}
return list; // ����һ������
}
public List<Choice> xlist() { // ��ѯ������Ϣ
List<Choice> list = new ArrayList<Choice>(); // ��������
Connection conn = Util.getConn();
String sql = "select * from choice"; // SQL��ѯ���
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()) {
int id= rst.getInt("id"); // �õ�ID
String classes = rst.getString("classes");
String student= rst.getString("student");
String teacher = rst.getString("teacher");
Choice choice = new Choice(id,classes,student,teacher);
list.add(choice);
}
rst.close(); // �ر�
pst.close(); // �ر�
} catch (SQLException e) {
e.printStackTrace(); // �׳��쳣
}
return list; // ����һ������
}
public boolean login (User user) {
PreparedStatement pst = null;
Connection conn = Util.getConn();
try{
pst = conn.prepareStatement("select * from user where username=? and password=? and type=?");
pst.setString(1, user.getUsername());
pst.setString(2, user.getPassword());
pst.setString(3, user.getType());
ResultSet rs =pst.executeQuery();
if(rs.next()){
return true;
}else{
return false;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public boolean addStudent(Student student) { // �����Ϣ
String sql = "insert into student2(number, name, sex, classes, major) values('" + student.getNumber() + "','" + student.getName() + "','" + student.getSex() + "','" + student.getClasses() + "','" + student.getMajor() + "')";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean updateStudent(Student student) { // ��
String sql = "update student2 set name='" + student.getName() + "' , sex='" + student.getSex() + "', classes='" + student.getClasses() + "', major='" + student.getMajor() + "' where id='" + student.getId() + "'";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean deleteStudent(int id) { // ɾ��
String sql = "delete from student2 where id ='"+ id +"'"; // ɾ����SQL��䣬����IDɾ��
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public Student selectStudentById(int id) { // ����ID���в�ѯ
String sql = "select * from student2 where id = " + id;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Student student = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String number = rs.getString("number");
String sex = rs.getString("sex");
String classes= rs.getString("classes");
String major= rs.getString("major");
student = new Student(id, number, name, sex, classes,major);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return student;
}
public Student selectStudentByNumber(String number) { // ����ID���в�ѯ
String sql = "select * from student2 where number = " + number;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Student student = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
int id = Integer.valueOf(rs.getString("id"));
String name = rs.getString("name");
String sex = rs.getString("sex");
String classes= rs.getString("classes");
String major= rs.getString("major");
student = new Student(id, number, name, sex, classes,major);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return student;
}
public boolean addTeacher(Teacher teacher) { // �����Ϣ
String sql = "insert into teacher(number, name, sex, college, type) values('" + teacher.getNumber() + "','" + teacher.getName() + "','" + teacher.getSex() + "','" + teacher.getCollege() + "','" + teacher.getType() + "')";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean updateTeacher(Teacher teacher) { // ��
String sql = "update teacher set name='" + teacher.getName() + "' , sex='" + teacher.getSex() + "', college='" + teacher.getCollege() + "', type='" + teacher.getType() + "' where id= teacher.getId() '";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean deleteTeacher(int id) { // ɾ��
String sql = "delete from teacher where id = '"+ id +"'"; // ɾ����SQL��䣬����IDɾ��
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public Teacher selectTeacherById(int id) { // ����ID���в�ѯ
String sql = "select * from teacher where id = " + id;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Teacher teacher = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String number = rs.getString("number");
String sex = rs.getString("sex");
String college= rs.getString("college");
String type= rs.getString("type");
teacher = new Teacher(id, number, name, sex, college ,type);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return teacher;
}
public Teacher selectTeacherByNumber(String number) { // ����ID���в�ѯ
String sql = "select * from teacher where number = " + number;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Teacher teacher = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String sex = rs.getString("sex");
String college= rs.getString("college");
String type= rs.getString("type");
int id = Integer.valueOf(rs.getString("id"));
teacher = new Teacher(id, number, name, sex, college ,type);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return teacher;
}
public boolean addCourse(Course course) { // �����Ϣ
String sql = "insert into class(number, name, people, teacher) values('" + course.getNumber() + "','" + course.getName() + "','" + course.getPeople() + "','" + course.getTeacher() + "')"; // ��ӵ�SQL���
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean updateCourse(Course course) { // ��
String sql = "update class set number='" + course.getNumber() + "' , name='" + course.getName() + "', people='" + course.getPeople() + "', teacher='" + course.getTeacher() + "' where id='" + course.getId() + "'";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean deleteCourse(int id) { // ɾ��
String sql = "delete from class where id = '"+ id +"'"; // ɾ����SQL��䣬����IDɾ��
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public Course selectCourseById(int id) { // ����ID���в�ѯ
String sql = "select * from class where id = " + id;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Course course = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String number = rs.getString("number");
String people = rs.getString("people");
String teacher= rs.getString("teacher");
course = new Course(id, number, name, people, teacher);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return course;
}
public boolean addChoice(Choice choice) { // �����Ϣ
String sql = "insert into choice(classes, student, teacher) values('" + choice.getClasses() + "','" + choice.getStudent() + "','" + choice.getTeacher() + "')";
Connection conn = Util.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//�ر�����
Util.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public Choice selectChoiceByNumber(String student) { // ����ID���в�ѯ
String sql = "select * from choice where student = " + student;
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
Choice choice = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String classes = rs.getString("classes");
String teacher= rs.getString("teacher");
int id = Integer.valueOf(rs.getString("id"));
choice = new Choice(id, classes, student, teacher);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return choice;
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Course;
import com.dao.Dao;
@WebServlet("/AddCourseServlet")
public class AddCourseServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String number = req.getParameter("number");
String name = req.getParameter("name");
String people = req.getParameter("people");
String teacher = req.getParameter("teacher");
Course course = new Course(number,name,people,teacher);
Dao dao = new Dao();
dao.addCourse(course);
req.getRequestDispatcher("ShowCourseServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Student;
import com.dao.Dao;
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String number = req.getParameter("number");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String classes = req.getParameter("classes");
String major = req.getParameter("major");
Student student = new Student(number,name,sex,classes,major);
Dao dao = new Dao();
dao.addStudent(student);
req.getRequestDispatcher("ShowStudentServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Teacher;
import com.dao.Dao;
@WebServlet("/AddTeacherServlet")
public class AddTeacherServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String number = req.getParameter("number");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String college = req.getParameter("college");
String type = req.getParameter("type");
Teacher teacher = new Teacher(number,name,sex,college,type);
Dao dao = new Dao();
dao.addTeacher(teacher);
req.getRequestDispatcher("ShowTeacherServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Choice;
import com.dao.Dao;
@WebServlet("/ChoiceServlet")
public class ChoiceServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String classes = req.getParameter("classes");
String student = req.getParameter("student");
String teacher = req.getParameter("teacher");
Choice choice = new Choice(classes,student,teacher);
Dao dao = new Dao();
dao.addChoice(choice);
req.getRequestDispatcher("ShowChoiceServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.Dao;
@WebServlet("/DeleteCourseServlet")
public class DeleteCourseServlet extends HttpServlet { // ɾ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id"); // ɾ�����ݵ�ID������IDɾ��
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
dao.deleteCourse(id);
}
req.getRequestDispatcher("ShowCourseServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.Dao;
@WebServlet("/DeleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet { // ɾ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id"); // ɾ�����ݵ�ID������IDɾ��
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
dao.deleteStudent(id);
}
req.getRequestDispatcher("ShowStudentServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Teacher;
import com.dao.Dao;
@WebServlet("/DeleteTeacherServlet")
public class DeleteTeacherServlet extends HttpServlet { // ɾ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id"); // ɾ�����ݵ�ID������IDɾ��
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
dao.deleteTeacher(id);
}
req.setCharacterEncoding("utf-8");
Dao dao = new Dao();
List<Teacher> list = dao.tlist();
HttpSession session = req.getSession();
session.setAttribute("tlist", list);
req.getRequestDispatcher("ShowTeacher.jsp").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.User;
import com.dao.Dao;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String type = req.getParameter("type");
User user = new User(username,password,type);
Dao dao = new Dao();
resp.setContentType("text/html;charset=UTF-8");
resp.setHeader("Cache-Control", "no-cache");
resp.setCharacterEncoding("UTF-8");
if(dao.login(user)&&type.equals("1")) {
HttpSession session = req.getSession();
session.setAttribute("number", username);
resp.getWriter().write("<script>alert('��ӭ��');history.back()</script>");
req.getRequestDispatcher("Student.jsp").forward(req, resp);
}
else if(dao.login(user)&&type.equals("2")) {
HttpSession session = req.getSession();
session.setAttribute("number2", username);
resp.getWriter().write("<script>alert('��ӭ��');history.back()</script>");
req.getRequestDispatcher("Teacher.jsp").forward(req, resp);
}
else if(dao.login(user)&&type.equals("3")) {
resp.getWriter().write("<script>alert('��ӭ��');history.back()</script>");
req.getRequestDispatcher("Manager.jsp").forward(req, resp);
}
else {
resp.getWriter().write("<script>alert('�û��������벻��ȷ��');history.back()</script>");
req.getRequestDispatcher("Login.jsp").forward(req, resp);
}
}
}
package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Choice;
import com.dao.Dao;
@WebServlet("/ShowChoiceServlet")
public class ShowChoiceServlet extends HttpServlet { // ��ʾȫ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Dao dao = new Dao();
List<Choice> list = dao.xlist();
HttpSession session = req.getSession();
session.setAttribute("xlist", list);
req.getRequestDispatcher("ShowChoice.jsp").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Course;
import com.dao.Dao;
@WebServlet("/ShowCourseServlet")
public class ShowCourseServlet extends HttpServlet { // ��ʾȫ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Dao dao = new Dao();
List<Course> list = dao.clist();
HttpSession session = req.getSession();
session.setAttribute("clist", list);
req.getRequestDispatcher("ShowCourse.jsp").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Student;
import com.dao.Dao;
@WebServlet("/ShowStudentServlet")
public class ShowStudentServlet extends HttpServlet { // ��ʾȫ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Dao dao = new Dao();
List<Student> list = dao.slist();
HttpSession session = req.getSession();
session.setAttribute("slist", list);
req.getRequestDispatcher("ShowStudent.jsp").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bean.Teacher;
import com.dao.Dao;
@WebServlet("/ShowTeacherServlet")
public class ShowTeacherServlet extends HttpServlet { // ��ʾȫ������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id");
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
Teacher teacher = dao.selectTeacherById(id);
req.setAttribute("teacher", teacher);
}
req.getRequestDispatcher("ShowTeacher2.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Dao dao = new Dao();
List<Teacher> list = dao.tlist();
HttpSession session = req.getSession();
session.setAttribute("tlist", list);
req.getRequestDispatcher("ShowTeacher.jsp").forward(req, resp);
}
}package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Course;
import com.dao.Dao;
@WebServlet("/UpdateCourseServlet")
public class UpdateCourseServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id");
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
Course course = dao.selectCourseById(id);
req.setAttribute("course", course);
}
req.getRequestDispatcher("UpdateCourse.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String number = req.getParameter("number");
String name = req.getParameter("name");
String people = req.getParameter("people");
String teacher = req.getParameter("teacher");
Course course = new Course(number,name,people,teacher);
Dao dao = new Dao();
dao.updateCourse(course);
req.getRequestDispatcher("ShowCourseServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Student;
import com.dao.Dao;
@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*String idStr = req.getParameter("id");
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
Student student = dao.selectStudentById(id);
req.setAttribute("student", student);
}*/
String number = req.getParameter("number");
if(number != null && !number.equals("")) {
Dao dao = new Dao();
Student student = dao.selectStudentByNumber(number);
req.setAttribute("student", student);
}
req.getRequestDispatcher("UpdateStudent.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String classes = req.getParameter("classes");
String major = req.getParameter("major");
Student student = new Student(id,name,sex,classes,major);
Dao dao = new Dao();
dao.updateStudent(student);
req.getRequestDispatcher("ShowStudentServlet").forward(req, resp);
}
}
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Teacher;
import com.dao.Dao;
@WebServlet("/UpdateTeacherServlet")
public class UpdateTeacherServlet extends HttpServlet { // �������
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/* String idStr = req.getParameter("id");
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
Dao dao = new Dao();
Teacher teacher = dao.selectTeacherById(id);
req.setAttribute("teacher", teacher);
}*/
String number = req.getParameter("number");
Dao dao = new Dao();
Teacher teacher = dao.selectTeacherByNumber(number);
req.setAttribute("teacher", teacher);
req.getRequestDispatcher("UpdateTeacher.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String number = req.getParameter("number");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String college = req.getParameter("college");
String type = req.getParameter("type");
Teacher teacher = new Teacher(number,name,sex,college,type);
Dao dao = new Dao();
dao.updateTeacher(teacher);
req.getRequestDispatcher("ShowTeacherServlet").forward(req, resp);
}
}
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* ���ݿ����ӹ���
* @author YP
*/
public class Util {
public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false";
public static String db_user = "root";
public static String db_pass = "root";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");//��������
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* �ر�����
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}