数据库课程设计-------简单的成绩管理系统web实现

 

  • 需求分析:

本次的设计我选择了高校的成绩管理系统,其中包含对于学生成绩的管理,学生信息以及老师信息的管理,以及对于各个科目信息的管理。

在本次设计中,一共设立了两种用户,既老师与学生,学生只能进行一些简单的查询,而老师则可以对所有数据进行操作。

两者的权限有一定的不同,学生偏向于查询一些材料,而老师则可以操作这些材料。

      各种数据采用建立数据表的方式从数据库调取,稍后介绍建立数据库的sql语句。

 

  • 逻辑结构

本次设计中一共用到了四个表,分别是student表,teacher表,course表,score表,下面将各表关系用表格表示,并附加sql语句。

 

字段名

类型

主键

外键

是否空

描述

sId

int

 

学号

Spassword

Varchar(255)

 

 

密码

Ssex

Varchar(255)

 

 

性别

Sname

Varchar(255)

 

 

姓名

Sprofession

Varchar(255)

 

 

专业

Sbirthday

Date

 

 

生日

 

建表sql语句:

CREATE TABLE student  (

  sid int(15) NOT NULL,

  spassword varchar(255)  NOT NULL,

  ssex varchar(255)  NOT NULL,

  sname varchar(255)  NOT NULL,

  sprofession varchar(255) NOT NULL,

  sbirthday date NOT NULL,

  PRIMARY KEY (sid),

)

 

2.teacher表:类型基本与student表相同,就是改了下名字

 

字段名

类型

主键

外键

是否空

描述

tId

int

 

工号

tpassword

Varchar(255)

 

 

密码

tsex

Varchar(255)

 

 

性别

tname

Varchar(255)

 

 

姓名

tprofession

Varchar(255)

 

 

专业

tbirthday

Date

 

 

生日

 

建表sql语句:

CREATE TABLE teacher  (

  tid int(15) NOT NULL,

  tpassword varchar(255)  NOT NULL,

  tsex varchar(255)  NOT NULL,

  tname varchar(255)  NOT NULL,

  tprofession varchar(255) NOT NULL,

  tbirthday date NOT NULL,

  PRIMARY KEY (tid),

)

 

  1. course表,存储课程信息:

字段名

类型

主键

外键

是否空

描述

Cid

int

 

课程号

Tid

int

 

老师工号

Cname

Vachar(255)

 

 

课程名称

 

CREATE TABLE course (

  cid int(15) NOT NULL,

  tid int(15) NOT NULL,

  cname varchar(255) NOT NULL,

  PRIMARY KEY (cid) ,

  INDEX course_ys1(tid) ,

  CONSTRAINT course_ys1 FOREIGN KEY (tid) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE

)

增加了级联删除更新,更好的对数据表之间的信息保持实时的更新。也减少出现错误数据。

 

4.score表,存储分数信息:

 

字段名

类型

主键

外键

是否空

描述

Cid

int

 

课程号

sid

int

 

学生学号

Score

Double(10,1)

 

 

分数

 

CREATE TABLE score  (

  sid int(15) NOT NULL,

  cid int(15) NOT NULL,

  score double(10, 1) NOT NULL,

  INDEX score_ys1 (sid) USING BTREE,

  INDEX score_ys2 (cid) USING BTREE,

  CONSTRAINT score_ys1 FOREIGN KEY (sid) REFERENCES student (sid) ON DELETE CASCADE ON UPDATE CASCADE,

  CONSTRAINT score_ys2 FOREIGN KEY (cid) REFERENCES course (cid) ON DELETE CASCADE ON UPDATE CASCADE

)

Score没有主键,根据不同课程名以及不同的学号可以添加数据,添加有两个级联删除更新的外键。

 

  • 管理系统设计及实现:

本次设计采用MVC设计模式,从web jsp页面传入数据至servlet数据处理,然后返回至jsp页面进行显示,在数据库方面我们采取使用DAO掉取数据表元素,然后根据使用情况建立服务层,在servlet中直接调取服务层类就可以进行对数据库的操作。下面是本次代码及解析:

 

 

实体类student:

package stu.entity;

 

import java.util.Date;

 

public class Student {

private int sid;

private String spassword,ssex,sname,sprofession;

private Date sbirthday;

 

public Student() {}

 

public Student(int sid, String spassword, String sname,String ssex,String sprofeesion,

Date sbirthday) {

this.sid = sid;

this.sprofession=sprofeesion;

this.spassword = spassword;

this.ssex = ssex;

this.sname = sname;

this.sbirthday = sbirthday;

}

 

public int getSid() {

return sid;

}

 

public void setSid(int sid) {

this.sid = sid;

}

 

public String getSpassword() {

return spassword;

}

 

public void setSpassword(String spassword) {

this.spassword = spassword;

}

 

public String getSsex() {

return ssex;

}

 

public void setSsex(String ssex) {

this.ssex = ssex;

}

 

public String getSname() {

return sname;

}

 

public void setSname(String sname) {

this.sname = sname;

}

 

public Date getSbirthday() {

return sbirthday;

}

 

public void setSbirthday(Date sbirthday) {

this.sbirthday = sbirthday;

}

 

public String getSprofession() {

return sprofession;

}

 

public void setSprofession(String sprofession) {

this.sprofession = sprofession;

}

 

}

 

 

该类为创建与数据表中相同数据类型的实体类,是数据库信息类型在代码中的体现。

 

Teacher实体类:

package stu.entity;

 

import java.util.Date;

 

public class Teacher {

private int tid;

private String tpassword,tname,tsex,tprofession;

private Date tbirthday;

 

public Teacher() {}

 

public Teacher( int tid , String tpassword , String tname , String tsex ,

String tprofession , Date tbirthday ) {

this.tid = tid;

this.tpassword = tpassword;

this.tname = tname;

this.tsex = tsex;

this.tprofession = tprofession;

this.tbirthday = tbirthday;

}

 

public int getTid() {

return tid;

}

 

public void setTid( int tid ) {

this.tid = tid;

}

 

public String getTpassword() {

return tpassword;

}

 

public void setTpassword( String tpassword ) {

this.tpassword = tpassword;

}

 

public String getTname() {

return tname;

}

 

public void setTname( String tname ) {

this.tname = tname;

}

 

public String getTsex() {

return tsex;

}

 

public void setTsex( String tsex ) {

this.tsex = tsex;

}

 

public String getTprofession() {

return tprofession;

}

 

public void setTprofession( String tprofession ) {

this.tprofession = tprofession;

}

 

public Date getTbirthday() {

return tbirthday;

}

 

public void setTbirthday( Date tbirthday ) {

this.tbirthday = tbirthday;

}

 

}

 

整体类型上来说与student相差不大,所含的信息基本相同,但是在数据库中是不同的数据表,所以分开来建。

Course 实体类:

package stu.entity;

 

public class Course {

private int cid , tid;

private String cname ;

public Course() { }

public Course( int cid , int tid , String cname ) {

this.tid=tid;

this.cid = cid;

this.cname = cname;

}

 

public int getTid() {

return tid ;

}

 

public void setTid( int tid ) {

this.tid = tid ;

}

 

public int getCid() {

return cid ;

}

 

public void setCid( int cid ) {

this.cid = cid;

}

 

public String getCname() {

return cname ;

}

 

public void setCname( String cname ) {

this.cname = cname;

}

 

}

 

包含课程号,课程名,设置的外键用tid在其中表示teacher的tid。

 

Score实体类:

package stu.entity;

 

public class Score {

private double score;

private int cid , sid;

public Score() { }

 

 

 

public Score( int cid, int sid , double score) {

this.score = score;

this.cid = cid;

this.sid = sid;

}

 

 

 

public int getCid() {

return cid;

}

 

public void setCid( int cid ) {

this.cid = cid;

}

 

public int getSid() {

return sid;

}

 

public void setSid( int sid ) {

this.sid = sid;

}

 

public double getScore() {

return score;

}

 

public void setScore( double score ) {

this.score = score;

}

 

}

 

前面介绍过,包含两个外键,cid和sid分别是course和student的主键,score是double类型的分数。

 

下面介绍jdbc连接数据库的工具类:

package stu.Util;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class DbUtil {

 

public static Connection getconnection() throws ClassNotFoundException, SQLException {

 String driveName = "com.mysql.cj.jdbc.Driver" ;

 String userName = "root" ;

 String userPassword = "7626712" ;

 String dbName = "sjksj" ;

 String url1 , url2 , url3 , url ;

url1 = "jdbc:mysql://localhost:3306/" + dbName ;

url2 = "?user=" + userName + "&password=" + userPassword ;

url3 = "&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;

url = url1 + url2 + url3 ;

Class.forName(driveName);

Connection conn = DriverManager.getConnection( url ) ;

return conn ;

}

 

 

public static void free ( Connection conn , Statement sta , ResultSet rs ) throws Exception{

if(conn != null) conn.close();

if(sta != null) sta.close();

if(rs != null) rs.close();

}

 

}

该工具类我用了静态方法的方式,第一个是连接数据库,第二个是释放资源,在后面DAO操作数据库时会使用到两个方法,这里注意时区的设置,因为用到了date类型,所以时区不能错。

下面介绍各个表的DAO:

每个表DAO我都先写出一个接口,然后根据接口实现功能,比较方便,下面介绍每个DAO的操作:

 

Student Dao接口:

package stu.Dao;

 

import java.util.List;

 

import stu.entity.Student;

 

public interface StudentDao {

public int Add ( Student student ) throws Exception ;

 

public int DeleteById ( int sid ) throws Exception ;

 

public Student SearchById ( int sid ) throws Exception ;

 

public int Change ( Student student ) throws Exception ;

 

public List SearchAllByName( String name ) throws Exception ;

 

public List SearchAllByProfession( String profession ) throws Exception ;

 

}

 

功能具体实现:

package stu.Dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

 

import stu.Util.DbUtil;

import stu.entity.Student;

 

public class StudentDaoimpl implements StudentDao {

 

@Override

public int Add(Student student) throws Exception {

// TODO Auto-generated method stub

Connection conn = DbUtil.getconnection( ) ;

String sql="insert into student values ( ? , ? , ? , ? , ? , ? )" ;

PreparedStatement ps = conn.prepareStatement( sql ) ;

ps.setInt(1, student.getSid() ) ;

ps.setString(2, student.getSpassword() ) ;

ps.setString(3, student.getSsex() ) ;

ps.setString(4, student.getSname() ) ;

ps.setString(5, student.getSprofession() ) ;

java.sql.Date dat=new java.sql.Date(student.getSbirthday().getTime());

ps.setDate(6, dat);

int n = ps.executeUpdate();

DbUtil.free( conn, ps, null );

return n ;

}

 

@Override

public int DeleteById(int sid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

conn = DbUtil.getconnection();

String sql = "delete from student where sid=? ";

ps = conn.prepareStatement( sql ) ;

ps.setInt( 1 , sid );

int n = ps.executeUpdate() ;

DbUtil.free( conn , ps , null ) ;

return n;

}

 

@Override

public Student SearchById(int sid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

ResultSet rs = null ;

Student student = null ;

conn = DbUtil.getconnection();

String sql = "select * from student where sid = ? ";

ps = conn.prepareStatement( sql );

ps.setInt( 1 , sid );

rs = ps.executeQuery() ;

if( rs.next() ){

student = new Student();

student.setSid( rs.getInt(1) );

student.setSpassword( rs.getString(2) );

student.setSsex( rs.getString(3) );

student.setSname( rs.getString(4) );

student.setSprofession( rs.getString(5) );

student.setSbirthday( rs.getDate(6) );

}

DbUtil.free( conn , ps , rs );

return student;

}

 

@Override

public int Change(Student student) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

conn = DbUtil.getconnection() ;

String sql = "update student set spassword=?,ssex=?,sname=?,sprofession=?,sbirthday=? where sid=?";

ps = conn.prepareStatement(sql);

ps.setString(1, student.getSpassword() ) ;

ps.setString(2, student.getSsex() ) ;

ps.setString(3, student.getSname() ) ;

ps.setString(4, student.getSprofession() ) ;

ps.setDate(5, new java.sql.Date(student.getSbirthday().getTime()));

ps.setInt(6, student.getSid() ) ;

int n=ps.executeUpdate();

DbUtil.free( conn , ps , null ) ;

  return n;

  }

 

@Override

public List SearchAllByName(String name) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Student student = null;

conn = DbUtil.getconnection();

String sql = "select * from student where sname=?";

ps = conn.prepareStatement( sql );

ps.setString(1, name);

rs = ps.executeQuery();

List studentlist = new ArrayList();

while( rs.next() ){

student = new Student();

student.setSid( rs.getInt(1) );

student.setSpassword( rs.getString(2) );

student.setSsex( rs.getString(3) );

student.setSname( rs.getString(4) );

student.setSprofession( rs.getString(5) );

student.setSbirthday( rs.getDate(6) );

studentlist.add( student ) ;

}

DbUtil.free( conn , ps , rs ) ;

return studentlist ;

}

 

@Override

public List SearchAllByProfession(String profession) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Student student = null;

conn = DbUtil.getconnection();

String sql = "select * from student where sprofession=?";

ps = conn.prepareStatement( sql );

ps.setString(1, profession);

rs = ps.executeQuery();

List studentlist = new ArrayList();

while( rs.next() ){

student = new Student();

student.setSid( rs.getInt(1) );

student.setSpassword( rs.getString(2) );

student.setSsex( rs.getString(3) );

student.setSname( rs.getString(4) );

student.setSprofession( rs.getString(5) );

student.setSbirthday( rs.getDate(6) );

studentlist.add( student ) ;

}

DbUtil.free( conn , ps , rs ) ;

return studentlist ;

}

 

}

 

这个DAO里面包括了对student表的数据的增删查改,又着重的多加了几个查询的方法,用于不同条件下的查询,除了按照id来查询之外,还包括按照名字查询,按照专业来查询的方法,相比较而言,增删改更为简单实现。

 

下面介绍teacher的DAO:

TeacherDAO接口类:

package stu.Dao;

 

import java.util.List;

 

import stu.entity.Teacher;

 

public interface TeacherDao {

 

public int Add ( Teacher teacher ) throws Exception ;

 

public int DeleteById ( int tid ) throws Exception ;

 

public Teacher SearchById ( int tid ) throws Exception ;

 

public int Change ( Teacher teacher ) throws Exception ;

 

public List SearchAllByName( String name ) throws Exception ;

 

public List SearchAllByProfession( String profession ) throws Exception ;

 

}

具体实现类:

 

package stu.Dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import stu.Util.DbUtil;

import stu.entity.Teacher;

 

public class TeacherDaoimpl implements TeacherDao {

 

@Override

public int Add(Teacher teacher) throws Exception {

// TODO Auto-generated method stub

Connection conn = DbUtil.getconnection( ) ;

String sql="insert into teacher values ( ? , ? , ? , ? , ? , ? )" ;

PreparedStatement ps = conn.prepareStatement( sql ) ;

ps.setInt(1, teacher.getTid() ) ;

ps.setString(2, teacher.getTpassword() ) ;

ps.setString(3, teacher.getTsex() ) ;

ps.setString(4, teacher.getTname() ) ;

ps.setString(5, teacher.getTprofession() ) ;

ps.setDate(6, new java.sql.Date(teacher.getTbirthday().getTime()));

int n = ps.executeUpdate();

DbUtil.free( conn, ps, null );

return n ;

}

 

@Override

public int DeleteById(int tid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

conn = DbUtil.getconnection();

String sql = "delete from teacher where tid=? ";

ps = conn.prepareStatement( sql ) ;

ps.setInt( 1 , tid );

int n = ps.executeUpdate() ;

DbUtil.free( conn , ps , null ) ;

return n;

}

 

@Override

public Teacher SearchById(int tid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

ResultSet rs = null ;

Teacher teacher = null ;

conn = DbUtil.getconnection();

String sql = "select * from teacher where tid = ? ";

ps = conn.prepareStatement( sql );

ps.setInt( 1 , tid );

rs = ps.executeQuery() ;

if( rs.next() ){

teacher = new Teacher();

teacher.setTid( rs.getInt(1) );

teacher.setTpassword( rs.getString(2) );

teacher.setTsex( rs.getString(3) );

teacher.setTname( rs.getString(4) );

teacher.setTprofession( rs.getString(5) );

teacher.setTbirthday( rs.getDate(6) );

}

DbUtil.free( conn , ps , rs );

return teacher ;

}

 

@Override

public int Change(Teacher teacher) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

conn = DbUtil.getconnection() ;

String sql = "update teacher set tpassword = ? , tsex = ? , tname = ? , tprofession = ? , tbirthday = ? where tid = ?";

ps = conn.prepareStatement(sql);

ps.setString(1, teacher.getTpassword() ) ;

ps.setString(2, teacher.getTsex() ) ;

ps.setString(3, teacher.getTname() ) ;

ps.setString(4, teacher.getTprofession() ) ;

ps.setDate(5, new java.sql.Date(teacher.getTbirthday().getTime()));

ps.setInt(6, teacher.getTid() ) ;

int n=ps.executeUpdate();

DbUtil.free( conn , ps , null ) ;

    return n;

}

 

@Override

public List SearchAllByName(String name) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Teacher teacher = null;

conn = DbUtil.getconnection();

String sql = "select * from teacher where tname = ?";

ps = conn.prepareStatement( sql );

ps.setString(1, name);

rs = ps.executeQuery();

List teacherlist = new ArrayList();

while( rs.next() ){

teacher = new Teacher();

teacher.setTid( rs.getInt(1) );

teacher.setTpassword( rs.getString(2) );

teacher.setTsex( rs.getString(3) );

teacher.setTname( rs.getString(4) );

teacher.setTprofession( rs.getString(5) );

teacher.setTbirthday( rs.getDate(6) );

teacherlist.add( teacher ) ;

}

DbUtil.free( conn , ps , rs ) ;

return teacherlist ;

}

 

@Override

public List SearchAllByProfession(String profession) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Teacher teacher = null;

conn = DbUtil.getconnection();

String sql = "select * from teacher where tprofession=?";

ps = conn.prepareStatement( sql );

ps.setString( 1 , profession );

rs = ps.executeQuery();

List teacherlist = new ArrayList();

while( rs.next() ){

teacher = new Teacher();

teacher.setTid( rs.getInt(1) );

teacher.setTpassword( rs.getString(2) );

teacher.setTsex( rs.getString(3) );

teacher.setTname( rs.getString(4) );

teacher.setTprofession( rs.getString(5) );

teacher.setTbirthday( rs.getDate(6) );

teacherlist.add( teacher ) ;

}

DbUtil.free( conn , ps , rs ) ;

return teacherlist ;

}

 

}

 

与student的接口差不多,功能也基本一致,不过是针对不同的数据表,所以就是sql语句有一定的不同。

 

下面介绍courseDAO:

接口类:

package stu.Dao;

 

import java.util.List;

 

import stu.entity.Course;

 

public interface CourseDao {

 

public int Add ( Course course ) throws Exception ;

 

public int DeleteById ( int cid ) throws Exception ;

 

public Course SearchById ( int cid ) throws Exception;

 

public List SearchAllByTid ( int tid ) throws Exception ;

 

public int Change ( Course course ) throws Exception ;

 

}

 

具体实现:

package stu.Dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import stu.Util.DbUtil;

import stu.entity.Course;

 

public class CourseDaoimpl implements CourseDao {

 

@Override

public int Add(Course course) throws Exception {

// TODO Auto-generated method stub

Connection conn = DbUtil.getconnection( ) ;

String sql="insert into course values ( ? , ? , ? )" ;

PreparedStatement ps = conn.prepareStatement( sql ) ;

ps.setInt(1, course.getCid() ) ;

ps.setInt(2, course.getTid() ) ;

ps.setString(3, course.getCname() ) ;

int n = ps.executeUpdate();

DbUtil.free( conn, ps, null );

return n ;

}

 

@Override

public int DeleteById( int cid ) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

conn = DbUtil.getconnection();

String sql = "delete from course where cid=? ";

ps = conn.prepareStatement( sql ) ;

ps.setInt( 1 , cid );

int n = ps.executeUpdate() ;

DbUtil.free( conn , ps , null ) ;

return n;

}

 

@Override

public Course SearchById(int cid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

ResultSet rs = null ;

Course course = null ;

conn = DbUtil.getconnection();

String sql = "select * from course where cid = ? ";

ps = conn.prepareStatement( sql );

ps.setInt( 1 , cid );

rs = ps.executeQuery() ;

if( rs.next() ){

course = new Course();

course.setCid( rs.getInt(1) );

course.setTid( rs.getInt(2) );

course.setCname( rs.getString(3) );

}

DbUtil.free( conn , ps , rs );

return course;

}

 

@Override

public List SearchAllByTid(int tid) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Course course = null;

conn = DbUtil.getconnection();

String sql = "select * from course where tid=?";

ps = conn.prepareStatement( sql );

ps.setInt(1, tid);

rs = ps.executeQuery();

List courselist = new ArrayList();

while( rs.next() ){

course = new Course();

course.setCid( rs.getInt(1) ) ;

course.setTid( rs.getInt(2) ) ;

course.setCname( rs.getString(3) ) ;

courselist.add( course ) ;

}

DbUtil.free( conn , ps , rs ) ;

return courselist;

}

 

@Override

public int Change(Course course) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

conn = DbUtil.getconnection() ;

String sql = "update course set tid=?,cname=? where cid=?";

ps = conn.prepareStatement(sql);

ps.setInt ( 1 , course.getTid() ) ;

ps.setString(2, course.getCname()) ;

ps.setInt ( 3 , course.getCid() ) ;

int n=ps.executeUpdate();

DbUtil.free( conn , ps , null ) ;

  return n;

}

 

}

 

CourseDAO处理的是对课程的增删查改,同样的也更专注于查,一种是通过课程号来查询,另一种则是通过教授老师来查询,通过教师查询的返回list类型。

 

下面是ScoreDAO的实现:

接口类:

package stu.Dao;

 

import java.util.List;

 

import stu.entity.Score;

 

public interface ScoreDao {

 

public int Add ( Score score ) throws Exception ;

 

public int DeleteById ( int sid , int cid ) throws Exception ;

 

public Score SearchById ( int sid , int cid ) throws Exception ;

 

public int Change ( Score score ) throws Exception ;

 

public List SearchAllBySid ( int cid ) throws Exception ;

 

}

 

具体实现类:

package stu.Dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import stu.Util.DbUtil;

import stu.entity.Score;

 

public class ScoreDaoimpl implements ScoreDao {

 

@Override

public int Add(Score score) throws Exception {

// TODO Auto-generated method stub

Connection conn = DbUtil.getconnection( ) ;

String sql="insert into score values ( ? , ? , ? )" ;

PreparedStatement ps = conn.prepareStatement( sql ) ;

ps.setInt(1, score.getSid() ) ;

ps.setInt(2, score.getCid() ) ;

ps.setDouble(3, score.getScore() ) ;

int n = ps.executeUpdate();

DbUtil.free( conn, ps, null );

return n ;

}

 

@Override

public int DeleteById( int sid , int cid ) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

conn = DbUtil.getconnection();

String sql = "delete from score where sid = ? and cid = ? ";

ps = conn.prepareStatement( sql ) ;

ps.setInt( 1 , sid );

ps.setInt( 2 , cid );

int n = ps.executeUpdate() ;

DbUtil.free( conn , ps , null ) ;

return n;

}

 

@Override

public Score SearchById( int sid , int cid ) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

ResultSet rs = null ;

Score score = null ;

conn = DbUtil.getconnection();

String sql = "select * from score where sid = ? and cid = ? ";

ps = conn.prepareStatement( sql );

ps.setInt( 1 , sid );

ps.setInt( 2 , cid );

rs = ps.executeQuery() ;

if( rs.next() ){

score = new Score();

score.setCid( rs.getInt(1) );

score.setSid( rs.getInt(2) );

score.setScore( rs.getDouble(3) );

}

DbUtil.free( conn , ps , rs );

return score;

}

 

@Override

public int Change ( Score score ) throws Exception {

// TODO Auto-generated method stub

Connection conn = null ;

PreparedStatement ps = null ;

conn = DbUtil.getconnection() ;

String sql = "update score set score=? where sid=? and cid=?";

ps = conn.prepareStatement(sql);

ps.setDouble (1, score.getScore()) ;

ps.setInt ( 2 , score.getSid() ) ;

ps.setInt ( 3 , score.getCid() ) ;

int n=ps.executeUpdate();

DbUtil.free( conn , ps , null ) ;

  return n;

}

 

@Override

public List SearchAllBySid ( int sid ) throws Exception {

// TODO Auto-generated method stub

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Score score = null;

conn = DbUtil.getconnection();

String sql = "select * from score where sid=?";

ps = conn.prepareStatement( sql );

ps.setInt(1, sid);

rs = ps.executeQuery();

List scorelist = new ArrayList();

while( rs.next() ){

score=new Score();

score.setCid( rs.getInt(1) ) ;

score.setSid( rs.getInt(2) ) ;

score.setScore( rs.getDouble(3) ) ;

scorelist.add( score ) ;

}

DbUtil.free( conn , ps , rs ) ;

return scorelist ;

}

 

}

 

其中的功能包括通过sid和cid来增删查改,不过增加了一项通过学生sid来查询并返回list的方法,用来查询某个学生的全部成绩,因为score表中没有主键,所以查询需要sid和cid共同来寻找确定一个元素才行。

 

DAO数据库的数据处理就这么多,如果我们直接在servlet中调用这些DAO工具类,涉及到一个表就必然的需要声明整个DAO的类,那么在servlet中调用整个该DAO的功能,对于数据库来说是不够安全的,于是我们添加了一个服务层,针对各个需要的功能进行封装,保证了DAO的安全性,下面来介绍服务层service:

 

登录Loginservice,登录服务:

接口类:

package stu.service;

 

public interface LoginService {

 

public int Check ( int uid , String password , String job ) throws Exception ;

 

}

 

具体实现类:

package stu.service;

 

import stu.Dao.StudentDaoimpl;

import stu.Dao.TeacherDaoimpl;

import stu.entity.Student;

import stu.entity.Teacher;

 

public class LoginServiceimpl implements LoginService {

private StudentDaoimpl stu;

private TeacherDaoimpl tdi;

 

public LoginServiceimpl( ) {

this.stu = new StudentDaoimpl();

this.tdi = new TeacherDaoimpl();

}

 

@Override

public int Check( int uid , String password , String job ) throws Exception {

// TODO Auto-generated method stub

if( job.equals( "student" ) ){

Student student = new Student();

student = stu.SearchById( uid );

if( student != null ){

if( password.equals( student.getSpassword() ) ){

return 1;

}else{

return 0;

}

}else return 0;

}else if( job.equals("teacher") ){

Teacher teacher = new Teacher();

teacher = tdi.SearchById( uid );

if( teacher != null ){

if( password.equals( teacher.getTpassword() ) ){

return 1;

}else{

return 0;

}

}else return 0;

}

else return 0;

}

 

}

 

这个服务仅仅有一个方法,是针对用户输入的信息与数据库蕴含的信息匹配,若匹配成功返回成功信息,则用户能登录成功,反之则登录失败,在这里登录服务还可以通过返回string将密码错误,用户不存在等通不过的信息返回,而我用了int类型进行返回,所以只能知道是否登录成功。

 

注册RegisterService 服务:

接口类:

package stu.service;

 

import java.util.Date;

 

public interface RegisterService {

 

String registerStudent( int id , String name , String password , String sex , String profession , Date birthday ) throws Exception ;

 

String registerTeacher( int id , String name , String password , String sex , String profession , Date birthday ) throws Exception ;

 

}

 

因为仅仅包含两种用户,所以注册服务中也仅仅有两个方法,包括注册学生用户,注册老师用户两种,下面介绍具体实现类:

package stu.service;

 

import java.util.Date;

 

import stu.Dao.StudentDaoimpl;

import stu.Dao.TeacherDaoimpl;

import stu.entity.Student;

import stu.entity.Teacher;

 

public class RegisterServiceimpl implements RegisterService {

private StudentDaoimpl stu;

private TeacherDaoimpl tdi;

 

public RegisterServiceimpl(  ) {

this.stu = new StudentDaoimpl();

this.tdi = new TeacherDaoimpl();

}

 

@Override

public String registerStudent( int id , String name , String password ,

String sex , String profession , Date birthday ) throws Exception {

// TODO Auto-generated method stub

Student student=stu.SearchById( id );

if(student != null){

return "账号已存在";

}else{

 student=new Student(id,password,name,sex,profession,birthday);

stu.Add(student);

return "注册成功";

}

}

 

@Override

public String registerTeacher(int id, String name, String password,

String sex, String profession, Date birthday) throws Exception {

// TODO Auto-generated method stub

Teacher teacher=tdi.SearchById(id);

if( teacher != null ){

return "账号已存在";

}else{

 teacher=new Teacher(id,password,name,sex,profession,birthday);

tdi.Add(teacher);

return "注册成功";

}

}

 

}

 

这里每个里面都先进行查询,若已存在该账号那么返回string类型的账号已存在,若不存在该账号类型那么直接将用户输入的信息放置在数据库中,并返回string类型注册成功,我们会根据返回的字符串类型在页面中进行提醒,稍后会有显示。

 

服务层肯定少不了对老师和对学生的服务,下面介绍studentservice学生服务:

 

接口类:

package stu.service;

 

import java.util.List;

 

import stu.entity.Course;

import stu.entity.Score;

 

public interface StudentService {

 

public List searchCourseByTid( int tid ) throws Exception ;

 

public Course searchCourseByCid( int cid ) throws Exception ;

 

public List serchScoreBySid( int sid ) throws Exception ;

 

}

 

严格来说,这里学生只有查询的功能,只能查询自己的成绩以及课程,这样在页面显示时会非常容易完成,下面看具体实现类:

package stu.service;

 

import java.util.ArrayList;

import java.util.List;

 

import stu.Dao.CourseDaoimpl;

import stu.Dao.ScoreDaoimpl;

import stu.entity.Course;

import stu.entity.Score;

 

public class StudentServiceimpl implements StudentService {

    private CourseDaoimpl cdi;

    private ScoreDaoimpl sdi;

    

public StudentServiceimpl( ) {

this.cdi = new CourseDaoimpl();

this.sdi = new ScoreDaoimpl();

}

 

@Override

public List searchCourseByTid( int tid ) throws Exception {

// TODO Auto-generated method stub

List lc = new ArrayList();

lc = cdi.SearchAllByTid( tid );

return lc;

}

 

@Override

public Course searchCourseByCid(int cid) throws Exception {

// TODO Auto-generated method stub

Course course = cdi.SearchById( cid );

return course;

}

 

@Override

public List serchScoreBySid(int sid) throws Exception {

// TODO Auto-generated method stub

List ls = sdi.SearchAllBySid( sid );

return ls;

}

 

}

 

通过调取DAO来实现功能,并返回值即可。

 

下面介绍TeacherService教师服务类:

接口:

package stu.service;

 

import java.util.List;

 

import stu.entity.Course;

import stu.entity.Score;

import stu.entity.Student;

import stu.entity.Teacher;

 

public interface TeacherService {

 

public int AddCourse ( Course course ) throws Exception ;

 

public int DeleteCourseById ( int cid ) throws Exception ;

 

public Course SearchCourseById ( int cid ) throws Exception ;

 

public List SearchAllCourseByTid ( int tid ) throws Exception ;

 

public int ChangeCourse ( Course course ) throws Exception ;

 

    public int AddScore ( Score score ) throws Exception ;

 

public int DeleteScoreById ( int sid , int cid ) throws Exception ;

 

public Score SearchScoreById ( int sid , int cid ) throws Exception ;

 

public int ChangeScore ( Score score ) throws Exception ;

 

public List SearchAllScoreBySid ( int cid ) throws Exception ;

 

    public int AddStudent ( Student student ) throws Exception ;

 

public int DeleteStudentById ( int sid ) throws Exception ;

 

public Student SearchStudentById ( int sid )  throws Exception ;

 

public int ChangeStudent ( Student student )  throws Exception ;

 

public List SearchAllStudentByName ( String name )  throws Exception ;

 

public List SearchAllStudentByProfession ( String profession ) throws Exception ;

 

    public int AddTeacher ( Teacher teacher ) throws Exception ;

 

public int DeleteTeacherById ( int tid ) throws Exception ;

 

public Teacher SearchTeacherById ( int tid ) throws Exception ;

 

public int ChangeTeacher ( Teacher teacher ) throws Exception ;

 

public List SearchAllTeacherByName ( String name ) throws Exception ;

 

public List SearchAllTeacherByProfession ( String profession ) throws Exception ;

 

}

 

 

在教师服务类中包括了所有的DAO功能,教师可以操作任何一种包括自身教师的数据,下面介绍实体类:

package stu.service;

 

import java.util.List;

 

import stu.Dao.CourseDaoimpl;

import stu.Dao.ScoreDaoimpl;

import stu.Dao.StudentDaoimpl;

import stu.Dao.TeacherDaoimpl;

import stu.entity.Course;

import stu.entity.Score;

import stu.entity.Student;

import stu.entity.Teacher;

 

public class TeacherServiceimpl implements TeacherService {

private CourseDaoimpl cdi;

private StudentDaoimpl stu;

private TeacherDaoimpl tdi;

private ScoreDaoimpl sdi;

 

public TeacherServiceimpl( ) {

this.cdi = new CourseDaoimpl();

this.stu = new StudentDaoimpl();

this.tdi = new TeacherDaoimpl();

this.sdi = new ScoreDaoimpl();

}

 

@Override

public int AddCourse(Course course) throws Exception {

// TODO Auto-generated method stub

Course cou = cdi.SearchById( course.getCid() );

if(cou != null) {

return 0;

} else {

cdi.Add(course);

return 1;

}

}

 

@Override

public int DeleteCourseById(int cid) throws Exception {

// TODO Auto-generated method stub

return cdi.DeleteById(cid);

}

 

@Override

public Course SearchCourseById( int cid ) throws Exception {

// TODO Auto-generated method stub

Course course2 = cdi.SearchById(cid);

return course2;

}

 

@Override

public List SearchAllCourseByTid( int tid ) throws Exception {

// TODO Auto-generated method stub

List lc = cdi.SearchAllByTid( tid );

return lc;

}

 

@Override

public int ChangeCourse(Course course) throws Exception {

// TODO Auto-generated method stub

int n = cdi.Change(course);

return n;

}

 

@Override

public int AddScore(Score score) throws Exception {

// TODO Auto-generated method stub

Score sc = sdi.SearchById( score.getSid() , score.getCid() );

if(sc != null) { return 0;

} else {

sdi.Add(score);

return 1;

}

}

 

@Override

public int DeleteScoreById( int sid , int cid ) throws Exception {

// TODO Auto-generated method stub

return sdi.DeleteById(sid, cid);

}

 

@Override

public Score SearchScoreById( int sid , int cid ) throws Exception {

// TODO Auto-generated method stub

Score sc=sdi.SearchById(sid, cid);

return sc;

}

 

@Override

public int ChangeScore(Score score) throws Exception {

// TODO Auto-generated method stub

return sdi.Change(score);

}

 

@Override

public List SearchAllScoreBySid(int cid) throws Exception {

// TODO Auto-generated method stub

List ls=sdi.SearchAllBySid(cid);

return ls;

}

 

@Override

public int AddStudent(Student student) throws Exception {

// TODO Auto-generated method stub

Student s = stu.SearchById( student.getSid() );

if(s != null) {

return 0;

} else {

stu.Add(student);

        return 1;

}

}

 

@Override

public int DeleteStudentById(int sid) throws Exception {

// TODO Auto-generated method stub

return stu.DeleteById(sid);

}

 

@Override

public Student SearchStudentById(int sid) throws Exception {

// TODO Auto-generated method stub

Student st=stu.SearchById(sid);

return st;

}

 

@Override

public int ChangeStudent(Student student) throws Exception {

// TODO Auto-generated method stub

return stu.Change(student);

}

 

@Override

public List SearchAllStudentByName(String name) throws Exception {

// TODO Auto-generated method stub

List ls2 = stu.SearchAllByName(name);

return ls2;

}

 

@Override

public List SearchAllStudentByProfession(String profession) throws Exception {

// TODO Auto-generated method stub

List ls2 = stu.SearchAllByProfession(profession);

return ls2;

}

 

@Override

public int AddTeacher(Teacher teacher) throws Exception {

// TODO Auto-generated method stub

Teacher tea = tdi.SearchById( teacher.getTid() );

if(tea != null) {

return 0;

} else {

tdi.Add(teacher);

return 1;

}

}

 

@Override

public int DeleteTeacherById(int tid) throws Exception {

// TODO Auto-generated method stub

return tdi.DeleteById( tid );

}

 

@Override

public Teacher SearchTeacherById(int tid) throws Exception {

// TODO Auto-generated method stub

Teacher teacher = tdi.SearchById( tid );

return teacher;

}

 

@Override

public int ChangeTeacher(Teacher teacher) throws Exception {

// TODO Auto-generated method stub

return tdi.Change( teacher );

}

 

@Override

public List SearchAllTeacherByName(String name) throws Exception {

// TODO Auto-generated method stub

List lt = tdi.SearchAllByName( name );

return lt;

}

 

@Override

public List SearchAllTeacherByProfession(String profession) throws Exception {

// TODO Auto-generated method stub

List lt = tdi.SearchAllByProfession(profession);

return lt;

}

 

}

 

这里基本都是直接返回DAO中的方法的返回类型,但是除了增加的方法,如果按照其他删查改的方法来写返回类型的话,如果不能添加成功,那么系统会报错,我们也无法知道是否成功,所以我们通过事先查询数据表中是否含有该信息,若有返回0,表示账号已存在,若没有那么返回1,并将数据插入至数据表。

 

下面开始介绍各界面以及相关的servlet服务:

本次的界面采用javaweb的知识构造,下面介绍登录Login的jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>成绩管理系统登录title>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

    String cw2 = (String)request.getAttribute("cw2");

%>

<center><h1>欢迎登录h1>center><br>

<form action="loginservlet" method="get">

<center>账号:<input type="text" name="id" class="txtb" placeholder="账号" >center><br>

<center>密码:<input type="password" name="password" class="txtb" placeholder="密码" >center><br>

<center>

   <input type="submit" value="登录">

center>

<br>

<center>

<input type="radio" name="job" id="student" value="student" class="radiob" checked="checked"/> <label> 学生 label>

<input type="radio" name="job" id="teacher" value="teacher" class="radiob"> <label> 老师 label>

center><br>

<center><a href="Register.jsp">注册一个?a>center>

form>

   <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "登录失败"){

alert('用户名或密码错误');

}

 

script>

<%

}

%>

<%

if(cw2!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw2}";

if(m == "账号已存在"){

alert('账号已存在');

}

 

script>

<%

}

%>

body>

html>

 

在这个界面我们输入账号密码登录,并选择用户的类型,意为是学生登录还是老师登录,然后进入到loginservlet中,下面介绍loginservlet:

package stu.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 stu.service.LoginServiceimpl;

 

/**

 * Servlet implementation class Loginservlet

 */

@WebServlet("/loginservlet")

public class Loginservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Loginservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String job = request.getParameter( "job" );

String i = request.getParameter( "id" );

    int id = Integer.parseInt(i);

    String password = request.getParameter( "password" );

    LoginServiceimpl lsi=new LoginServiceimpl();

    int flag;

    try {

     flag=lsi.Check(id, password, job);

     if( flag == 1 ){

     if( job.equals("student") )

     {

     request.getSession().setAttribute("sid", id);

request.getRequestDispatcher("Studentunion.jsp").forward(request, response);

}

     else if( job.equals("teacher") )

     {

     request.getSession().setAttribute("tid", id);

request.getRequestDispatcher("Teacherunion.jsp").forward(request, response);

     }

     } else {

     request.setAttribute("cw", "登录失败");

request.getRequestDispatcher("Login.jsp").forward(request, response);

     }

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

 

在这个servlet中,我们创建loginservice类型,然后通过request的传值进行判断,判断是否登录成功,若成功那么再根据job也就是是学生还是老师,来跳转不同的页面,学生跳转至studentunion页面,老师跳转至teacherunion页面。如果判断失败,那么我们跳转回login页面,并设立提示信息cw=“用户名或密码错误”来进行网页上的提示。

下面我们再介绍register页面以及servlet类:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>注册title>

head>

<body>

<form action="registerservlet" method="get">

<h2 align="center">成绩管理系统注册h2>

    <table width="550" align="center" border="0">

        <tr>

            <td> 帐号(学号/工号):td>

            <td><input type="text" placeholder="请输入学号/工号" name="id" > td>

        tr>

         <tr>

            <td>密码:td>

            <td><input type="password" name="password" placeholder="请输入密码"> td>

        tr>

        <tr>

            <td>姓名:td>

            <td>

                <input type="text" placeholder="请输入姓名" name="name" >td>

        tr>

        <tr>

            <td>性别:td>

            <td>

                <input type="radio" name="sex" id="男" value="男" > <label for="男">label>

                <input type="radio" name="sex" id="女" value="女" > <label for="女">label>

            td>

        tr>

        <tr>

            <td>出生年月:td>

            <td>

                <select name="year">

                    <option>请选择年份option>

                    <option>1980option>

                    <option>1981option>

                    <option>1982option>

                    <option>1983option>

                    <option>1984option>

                    <option>1985option>

                    <option>1986option>

                    <option>1987option>

                    <option>1988option>

                    <option>1989option>

                    <option>1990option>

                    <option>1991option>

                    <option>1992option>

                    <option>1993option>

                    <option>1994option>

                    <option>1995option>

                    <option>1996option>

                    <option>1997option>

                    <option>1998option>

                    <option>1999option>

                    <option>2000option>

                    <option>2001option>

                    <option>2002option>

                    <option>2003option>

                select>

         

                <select name="month">

                    <option>请选择月份option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                select>

         

                <select name="day">

                    <option>请选择日期option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                    <option>13option>

                    <option>14option>

                    <option>15option>

                    <option>16option>

                    <option>17option>

                    <option>18option>

                    <option>19option>

                    <option>20option>

                    <option>21option>

                    <option>22option>

                    <option>23option>

                    <option>24option>

                    <option>25option>

                    <option>26option>

                    <option>27option>

                    <option>28option>

                    <option>29option>

                    <option>30option>

                    <option>31option>

                select>

            td>

        tr>

         <tr>

            <td> 专业名称:td>

            <td><input type="text" placeholder="请输入专业名称" name="profession" > td>

        tr>

        <tr>

            <td> 身份: td>

            <td>

                <input type="radio" name="job" id="student" checked="checked" value="student"> <label for="user">学生label>

                <input type="radio" name="job" id="teacher" value="teacher" > <label for="admini">老师label>

            td>

        tr>

 

        <tr>

            <td>td>

            <td>

                <input type="submit" value="注册">

            td>

        tr>

        <tr>

            <td>td>

        tr> 

        <tr>

            <td>td>

            <td>

            <a href="Login.jsp"> 已注册,立即登入a>

            td>

        tr> 

 

    table>

    form>

body>

html>

 

在注册页面我们需要输入账号,密码,姓名,性别,出生年月日,专业,以及所注册的用户类型,然后将这些信息共同传入到servlet中。

下面介绍registerservlet:

 

package stu.Servlet;

 

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

 

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 stu.service.RegisterServiceimpl;

 

/**

 * Servlet implementation class Registerservlet

 */

@WebServlet("/registerservlet")

public class Registerservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Registerservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String name = request.getParameter("name");

String sex = request.getParameter("sex");

String i = request.getParameter("id");

int id=Integer.parseInt(i);

String password = request.getParameter("password");

String profession = request.getParameter("profession");

String year = request.getParameter("year");

String month = request.getParameter("month");

String day = request.getParameter("day");

String bir = year+"-"+month+"-"+day;

String job = request.getParameter("job");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

 

java.util.Date birthday = null;

 

try {

birthday = sdf.parse(bir);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String cw=null;

if( job.equals("student") ){

RegisterServiceimpl rsi = new RegisterServiceimpl();

try {

cw=rsi.registerStudent(id, name, password, sex, profession, birthday);

if( cw.equals("注册成功") ) {

request.getRequestDispatcher("Login.jsp").forward(request, response);

} else if( cw.equals("账号已存在")) {

request.setAttribute("cw2", "账号已存在");

request.getRequestDispatcher("Login.jsp").forward(request, response);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else if( job.equals("teacher") ) {

 RegisterServiceimpl rsi = new RegisterServiceimpl();

 try {

cw=rsi.registerTeacher(id, name, password, sex, profession, birthday);

if( cw.equals("注册成功") ) {

request.getRequestDispatcher("Login.jsp").forward(request, response);

} else if( cw.equals("账号已存在")) {

request.setAttribute("cw2", "账号已存在");

request.getRequestDispatcher("Login.jsp").forward(request, response);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

   doGet(request, response);

}

 

}

 

在这里我们根据所注册不同的用户类型,调取registservice中不同的方法,并根据返回类型来设置提醒,其中还包括将字符串转换为util.Date类型数据,在DAO操作中还包括将util.Date转换为sql.Date类型数据,详情见代码。

我们根据返回值,若注册成功,那么直接跳转至login界面,若注册失败,也跳转至login界面,并设置提醒信息,提醒账号已存在。

下面开始介绍两种用户的界面:

Student用户的界面:

首先登陆完成后跳转至studentunion界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>成绩管理系统title>

head>

 <frameset rows="80,*" style="border: 1px" >

        <frame src="Studenttitle.jsp" scrolling="no">

        <frameset cols="140,*"  style="border: 1px">

        <frame src="Studentleft.jsp" scrolling="no" noresize="noresize" >

        <frame  name="right" >

        frameset>

        frameset>

<body>

body>

html>

 

其中包含了整个系统的框架,左边是各种功能,既studentleft界面,上面是标题,右边是功能的具体实现。

标题页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<div class="operation" style="margin-left: 35%;margin-top: 10px">

<h1>欢迎登录成绩管理系统h1>

div>

body>

html>

 

这就是个标题。

 

功能栏:studentleft页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body >

<div class="operation" style="margin-left: 35%;margin-top: 70px">

<a href = "studentsearchscoreservlet" target="right" style="margin-top:20px "> 查询成绩 a> <br> <br>

<a href = "Studentsearchcourse.jsp" target="right" > 查询课程 a> <br> <br>

<a href = "Close.jsp" target="right" > 退出系统 a>

div>

body>

html>

 

包括两个功能,查询成绩,和查询课程,还有一个退出系统,和标题页面差不多,因为和teacher共用一个close页面,所以最后放出来。

下面介绍一下相较简单的查询成绩功能,当学生点击这个,我们从loginservlet里设置的id就用到了,我们直接根据这个id在servlet查询,然后返回到这个界面进行显示就可以了:

查询成绩studentsearchscoreservlet

package stu.Servlet;

 

import java.io.IOException;

import java.util.ArrayList;

 

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 stu.entity.Score;

import stu.service.StudentServiceimpl;

 

/**

 * Servlet implementation class Studentsearchscoreservlet

 */

@WebServlet("/studentsearchscoreservlet")

public class Studentsearchscoreservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Studentsearchscoreservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

        int sid = (int) request.getSession().getAttribute("sid");

        StudentServiceimpl service = new StudentServiceimpl();

        ArrayList allscore = null;

try {

allscore = (ArrayList) service.serchScoreBySid(sid);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        request.setAttribute("allscore", allscore);

        request.getRequestDispatcher("Studentsearchscore.jsp").forward(request, response);

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

我们根据所登录的sid直接通过studentservice的查询功能得到该学生的成绩,然后request传值到Studentsearchscore页面进行显示,该页面代码:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@page import="stu.entity.*"%>

<%@page import="java.util.ArrayList"%>

<%@page import="java.util.List"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

List allscore = (ArrayList)request.getAttribute("allscore");

int size = 0;

if(allscore!=null){

size = allscore.size();

}

%>

<table style="width: 100%; height: 20%" border="1">

<tr bgcolor="#B4CDCD">

<th align="center"><b>课程号b>th>

<th align="center"><b>成绩b>th>

tr>

<%

for (int i = 0; i < size; ++i) {

%>

<tr>

<td><%=allscore.get(i).getCid()%>td>

<td><%=allscore.get(i).getScore()%>td>

tr>

<%

}

%>

table>

body>

html>

 

通过for循环表格来将成绩输出。

 

 

下面介绍搜索课程的功能:

首先是搜索界面,需要提供要搜索的信息:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@page import="stu.entity.*"%>

<%@page import="java.util.ArrayList"%>

<%@page import="java.util.List"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

        Course courses1 = (Course)request.getAttribute("courses1");

ArrayList courses2 = (ArrayList)request.getAttribute("courses2");

int size = 0;

if(courses2!=null){

size = courses2.size();

}

%>

<form action="studentsearchcourseServlet" method="get" target="right">

<input type="radio" name="searchfs"  value="cid" class="radiob" checked="checked"/> <label> 课程号 label>

        <input type="radio" name="searchfs"  value="tid" class="radiob"> <label> 老师 label>

<input type="text" placeholder="输入课程号/工号" class="txtb" name="id" style="margin-left: 15px"/>

<input type="submit" value="查询" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

form>

<table style="width: 100%; height: 20%" border="1">

<tr bgcolor="#B4CDCD">

<th><b> 课程号 b>th>

<th><b> 课程名 b>th>

tr>

<%

    if(courses2 != null){

for (int i = 0; i < size; ++i) {

%>

<tr>

<td><%=courses2.get(i).getCid()%>td>

<td><%=courses2.get(i).getCname()%>td>

tr>

<%

}}

%>

<%

    if(courses1 != null){

 

%>

<tr>

<td><%=courses1.getCid()%>td>

<td><%=courses1.getCname()%>td>

tr>

<%

}

%>

table>

body>

html>

 

这里是两种搜索方式,我们可以选择是通过老师的工号来搜索还是通过课程号来搜索,然后将信息提供到studentsearchcourseServlet中,下面是该servlet代码:

package stu.Servlet;

 

import java.io.IOException;

import java.util.ArrayList;

 

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 stu.entity.Course;

import stu.service.StudentServiceimpl;

 

/**

 * Servlet implementation class StudentsearchcourseServlet

 */

@WebServlet("/studentsearchcourseServlet")

public class StudentsearchcourseServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public StudentsearchcourseServlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

        String searchfs = request.getParameter("searchfs");

        String id2 = request.getParameter("id");

        int id=Integer.parseInt(id2);

        StudentServiceimpl service = new StudentServiceimpl();

        if(searchfs.equals("cid")){

        Course courses = null;

try {

courses = service.searchCourseByCid(id);

request.setAttribute("courses1", courses);

        request.getRequestDispatcher("Studentsearchcourse.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}}

        else {

         try {

ArrayList courses = (ArrayList) service.searchCourseByTid(id);

request.setAttribute("courses2", courses);

        request.getRequestDispatcher("Studentsearchcourse.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        

        }

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

我们根据两种不同的搜索,先根据要搜索的内容,建立course类型或者list类型然后查询,将得到的值返回,若course类型用request传递的是courses1,若为list类型,那么用request传递的是courses2。

传递回刚刚输入查询信息的页面,接受信息并通过for循环表格来显示。

 

下面介绍老师的页面:

Teacherunion:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<frameset rows="80,*" style="border: 1px" >

        <frame src="Studenttitle.jsp" scrolling="no">

        <frameset cols="140,*"  style="border: 1px">

        <frame src="Teacherleft.jsp" scrolling="no" noresize="noresize" >

        <frame  name="right" >

        frameset>

        frameset>

<body>

body>

html>

 

总体和学生union的界面差不多,这里直接共用了一个标题页面,就不再写一次了。

Teacherleft页面,功能菜单页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

    <div class="operation" style="margin-left: 35%;margin-top: 70px">

<a href = "Teacheruser.jsp" target="right" style="margin-top:20px "> 用户管理 a> <br> <br>

<a href = "Teachercourse.jsp" target="right" > 课程管理 a> <br> <br>

<a href = "Teacherscore.jsp" target="right" > 分数管理 a> <br> <br>

<a href = "Close.jsp" target="right" > 退出系统 a>

div>

body>

html>

 

包含三个功能,分别是对课程的增删查改,对分数的增删查改,对用户的增删查改,这里的用户包括老师和学生两种。

下面先介绍课程管理:

每个管理都有一个总体页面,下面介绍课程总体页面Teachercourse:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<form action="teachercourseservlet" method="get" target="right">

<center> <input type="radio" name="glfs"  value="add" class="radiob" checked="checked"/> <label> 增加课程 label><br><br>

        <input type="radio" name="glfs"  value="delete" class="radiob"> <label> 删除课程 label><br><br>

        <input type="radio" name="glfs"  value="search" class="radiob"> <label> 查询课程 label><br><br>

        <input type="radio" name="glfs"  value="change" class="radiob"> <label> 修改课程 label><br><br>

<input type="submit" value="确定" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

  center>

form>

body>

html>

 

这个页面包括了对课程的增删查改,通过单选来选择要实现的功能,跳转到teachercourseservlet总体servlet中,下面介绍该servlet:

 

package stu.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;

 

/**

 * Servlet implementation class Teachercourseservlet

 */

@WebServlet("/teachercourseservlet")

public class Teachercourseservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Teachercourseservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String glfs=request.getParameter("glfs");

if( glfs.equals("add") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Addcourse.jsp").forward(request, response);

} else if( glfs.equals("delete") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Deletecourse.jsp").forward(request, response);

} else if( glfs.equals("search") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Studentsearchcourse.jsp").forward(request, response);

} else if( glfs.equals("change") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Changecourse.jsp").forward(request, response);

}

 

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

根据所选择的功能,我们跳值addcourse页面或者deletecourse页面或者changecourse页面或者searchcourse页面,下面先介绍addcourse页面及相关的功能:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="addcourse" method="get">

 <center>课程号:<input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center>教师工号:<input type="text" name="tid" class="txtb" placeholder="教师工号" > center> <br> <br>

  <center>课程名:<input type="text" name="cname" class="txtb" placeholder="课程名" > center> <br> <br>

 <center><input type="submit" value="添加">center>

 form>

 <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "插入成功"){

alert( '插入成功' );

} else if(m == "插入失败"){

alert( '插入失败' );

}

script>

<%

}

%>

body>

html>

 

在这里我们输入要加入的课程类的信息,然后跳转到addcourse这个servlet中,下面介绍该servlet:

package stu.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 stu.entity.Course;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Addcourse

 */

@WebServlet("/addcourse")

public class Addcourse extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Addcourse() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int tid = Integer.parseInt(request.getParameter("tid"));

String cname = request.getParameter("cname");

TeacherServiceimpl tsi = new TeacherServiceimpl();

Course course = new Course(cid,tid,cname);

int n = 0;

try {

n = tsi.AddCourse(course);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(n == 0) {

request.setAttribute("cw", "插入失败");

request.getRequestDispatcher("Addcourse.jsp").forward(request, response);

} else {

request.setAttribute("cw", "插入成功");

request.getRequestDispatcher("Addcourse.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

我们通过教师服务类来进行插入,并根据方法的返回值判断是否插入成功,并建立相应的提醒信息返回至addcourse页面进行提醒,若成功则提醒插入成功,若失败则提醒插入失败。

 

下面介绍删除课程页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>deletetitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="deletecourse" method="get">

 <center>课程号:<input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center><input type="submit" value="删除">center>

 form>

<%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "删除成功"){

alert('删除成功');

} else if(m == "删除失败"){

alert('删除失败');

}

script>

<%

}

%>

body>

html>

 

输入要删除的课程号id,然后点击删除传至servlet:

package stu.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 stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Deletecourse

 */

@WebServlet("/deletecourse")

public class Deletecourse extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Deletecourse() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int n = 0;

TeacherServiceimpl tsi = new TeacherServiceimpl();

try {

n = tsi.DeleteCourseById( cid );

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if( n == 1 ) {

request.setAttribute("cw", "删除成功");

request.getRequestDispatcher("Deletecourse.jsp").forward(request, response);

} else {

request.setAttribute("cw", "删除失败");

request.getRequestDispatcher("Deletecourse.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

还是用教师服务层进行删除,并根据返回值设立提醒信息。

 

查询课程:这里查询课程我直接调用了studentsearchcourse,上面有介绍,就是根据课程号或者教师工号来查询课程。

 

修改课程:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>changetitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="changecourse" method="get">

 <center>课程号:<input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center>教师工号:<input type="text" name="tid" class="txtb" placeholder="教师工号" > center> <br> <br>

  <center>课程名:<input type="text" name="cname" class="txtb" placeholder="课程名" > center> <br> <br>

 <center><input type="submit" value="更新">center>

 form>

 <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "更新成功"){

alert('更新成功');

} else if(m == "更新失败"){

alert('更新失败');

}

script>

<%

}

%>

body>

html>

 

输入修改的信息,注意,意思是修改此账号的除账号外的所有信息,账号不能错。

Changecourseservlet:

package stu.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 stu.entity.Course;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Changecourse

 */

@WebServlet("/changecourse")

public class Changecourse extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Changecourse() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int tid = Integer.parseInt(request.getParameter("tid"));

String cname = request.getParameter("cname");

TeacherServiceimpl tsi = new TeacherServiceimpl();

Course course = new Course(cid,tid,cname);

int n = 0;

try {

n = tsi.ChangeCourse(course);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(n == 0) {

request.setAttribute("cw", "更新失败");

request.getRequestDispatcher("Changecourse.jsp").forward(request, response);

} else {

request.setAttribute("cw", "更新成功");

request.getRequestDispatcher("Changecourse.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

同样也是,用教师服务层进行修改,然后根据返回值设置提醒信息返回修改界面进行提醒。

 

 

下面是score的操作,首先是teacherscore页面:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<form action="teacherscoreservlet" method="get" target="right">

<center><input type="radio" name="glfs"  value="add" class="radiob" checked="checked"/> <label> 增加分数 label><br><br>

        <input type="radio" name="glfs"  value="delete" class="radiob"> <label> 删除分数 label><br><br>

        <input type="radio" name="glfs"  value="search" class="radiob"> <label> 查询分数 label><br><br>

        <input type="radio" name="glfs"  value="change" class="radiob"> <label> 修改分数 label><br><br>

<input type="submit" value="确定" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

       center>

form>

body>

html>

 

与刚刚的teachercourse一样,先将要做的操作传值servlet然后根据操作传到不同的页面,下面介绍teachercourseservlet:

package stu.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;

 

/**

 * Servlet implementation class Teacherscoreservlet

 */

@WebServlet("/teacherscoreservlet")

public class Teacherscoreservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Teacherscoreservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String glfs=request.getParameter("glfs");

if( glfs.equals("add") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Addscore.jsp").forward(request, response);

} else if( glfs.equals("delete") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Deletescore.jsp").forward(request, response);

} else if( glfs.equals("search") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Searchscore.jsp").forward(request, response);

} else if( glfs.equals("change") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Changescore.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

根据不同的功能进入不同的界面。 下面介绍addscore界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="addscore" method="get">

 <center> 课程号: <input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center> 学生号: <input type="text" name="sid" class="txtb" placeholder="教师工号" > center> <br> <br>

  <center> 分数: <input type="text" name="score" class="txtb" placeholder="课程名" > center> <br> <br>

 <center><input type="submit" value="添加">center>

 form>

 <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "插入成功"){

alert( '插入成功' );

} else if(m == "插入失败"){

alert( '插入失败' );

}

script>

<%

}

%>

body>

html>

输入要添加的信息,然后传入servlet:

package stu.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 stu.entity.Score;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Addscore

 */

@WebServlet("/addscore")

public class Addscore extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Addscore() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int tid = Integer.parseInt(request.getParameter("sid"));

double sc = Double.parseDouble(request.getParameter("score"));

TeacherServiceimpl tsi = new TeacherServiceimpl();

Score score = new Score(cid,tid,sc);

int n = 0;

try {

n = tsi.AddScore(score);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(n == 0) {

request.setAttribute("cw", "插入失败");

request.getRequestDispatcher("Addcourse.jsp").forward(request, response);

} else {

request.setAttribute("cw", "插入成功");

request.getRequestDispatcher("Addcourse.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

用教师服务类插入,然后通过返回值设置提醒信息并返回到刚才的界面。

 

Deletescore类:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="deletescore" method="get">

 <center>课程号:<input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

  <center>学号:<input type="text" name="sid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center><input type="submit" value="删除">center>

 form>

 <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "删除成功"){

alert('删除成功');

} else if(m == "删除失败"){

alert('删除失败');

}

script>

<%

}

%>

body>

html>

 

 

这里因为score没主键,我们要同时根据sid与cid确定一个目标进行删除,servlet:

package stu.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 stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Deletescore

 */

@WebServlet("/deletescore")

public class Deletescore extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Deletescore() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int sid = Integer.parseInt(request.getParameter("sid"));

int n = 0;

TeacherServiceimpl tsi = new TeacherServiceimpl();

try {

n = tsi.DeleteScoreById(sid, cid);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if( n == 1 ) {

request.setAttribute("cw", "删除成功");

request.getRequestDispatcher("Deletecourse.jsp").forward(request, response);

} else {

request.setAttribute("cw", "删除失败");

request.getRequestDispatcher("Deletecourse.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

根据传入的sid和cid调用教师服务类删除。并根据返回值进行返回删除页面提醒。

 

查询score功能searchscore页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@page import="stu.entity.*"%>

<%@page import="java.util.ArrayList"%>

<%@page import="java.util.List"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

        Score score1 = (Score)request.getAttribute("score1");

%>

<form action="TeachersearchscoreServlet" method="get" target="right">

<input type="text" placeholder="输入学号" class="txtb" name="sid" style="margin-left: 15px"/>

<input type="text" placeholder="输入课程号" class="txtb" name="cid" style="margin-left: 15px"/>

<input type="submit" value="查询" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

form>

<table style="width: 100%; height: 20%" border="1">

<tr bgcolor="#B4CDCD">

<th><b> 课程号 b>th>

<th><b> 学号 b>th>

<th><b> 分数 b>th>

tr>

<%

    if(score1 != null){

%>

<tr>

<td><%=score1.getCid()%>td>

<td><%=score1.getSid()%>td>

<td><%=score1.getScore()%>td>

tr>

<%

}

%>

table>

body>

html>

 

因为针对的是教师查询,所以我们这里的查询是根据cid与sid的查询,也就是对单个学生单个科目成绩的查询,下面介绍servlet:

package stu.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 stu.entity.Score;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class TeachersearchscoreServlet

 */

@WebServlet("/TeachersearchscoreServlet")

public class TeachersearchscoreServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public TeachersearchscoreServlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

        TeacherServiceimpl tsi = new TeacherServiceimpl();    

        int sid = Integer.parseInt( request.getParameter("sid") );

        int cid = Integer.parseInt( request.getParameter("cid") );

        try {

Score sc = tsi.SearchScoreById(sid, cid);

 request.setAttribute("score1", sc);

     request.getRequestDispatcher("Searchscore.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        }

 

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

还是通过教师服务类进行查询,然后将数据传入到查询界面进行显示。

 

修改分数changescore页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="changescore" method="get">

 <center>课程号:<input type="text" name="cid" class="txtb" placeholder="课程号" > center> <br> <br>

 <center>学号:<input type="text" name="sid" class="txtb" placeholder="学号" > center> <br> <br>

  <center>分数:<input type="text" name="score" class="txtb" placeholder="分数" > center> <br> <br>

 <center><input type="submit" value="更新">center>

 form>

 <%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "更新成功"){

alert('更新成功');

} else if(m == "更新失败"){

alert('更新失败');

}

script>

<%

}

%>

body>

html>

 

输入课程号以及学号,这两个号码不能错,相当于只是修改分数。Servlet类:

package stu.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 stu.entity.Score;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Changescore

 */

@WebServlet("/changescore")

public class Changescore extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Changescore() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

int cid = Integer.parseInt(request.getParameter("cid"));

int sid = Integer.parseInt(request.getParameter("sid"));

double sco = Double.parseDouble( request.getParameter("score"));

TeacherServiceimpl tsi = new TeacherServiceimpl();

Score score = new Score(cid,sid,sco);

int n = 0;

try {

n = tsi.ChangeScore(score);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(n == 0) {

request.setAttribute("cw", "更新失败");

request.getRequestDispatcher("Changescore.jsp").forward(request, response);

} else {

request.setAttribute("cw", "更新成功");

request.getRequestDispatcher("Changescore.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

调用教师服务类进行修改,并设置提醒返回修改页面提醒。

 

 

用户操作:

Teacheruser页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<form action="teacheruserservlet" method="get" target="right">

<center><input type="radio" name="glfs"  value="add" class="radiob" checked="checked"/> <label> 增加用户 label><br><br>

        <input type="radio" name="glfs"  value="delete" class="radiob"> <label> 删除用户 label><br><br>

        <input type="radio" name="glfs"  value="search" class="radiob"> <label> 查询用户 label><br><br>

        <input type="radio" name="glfs"  value="change" class="radiob"> <label> 修改用户 label><br><br>

<input type="submit" value="确定" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

       center>

form>

body>

html>

 

对用户的操作,同之前两个一样,传入servlet:

package stu.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;

 

/**

 * Servlet implementation class Teacheruserservlet

 */

@WebServlet("/teacheruserservlet")

public class Teacheruserservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Teacheruserservlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String glfs=request.getParameter("glfs");

if( glfs.equals("add") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Adduser.jsp").forward(request, response);

} else if( glfs.equals("delete") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Deleteuser.jsp").forward(request, response);

} else if( glfs.equals("search") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Searchuser.jsp").forward(request, response);

} else if( glfs.equals("change") ) {

 request.setAttribute("glfs", glfs);

        request.getRequestDispatcher("Changeuser.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

 

Adduser:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

    String cw2 = (String)request.getAttribute("cw2");

%>

<form action="adduser" method="get">

<table width="550" align="center" border="0">

        <tr>

            <td> 帐号(学号/工号):td>

            <td><input type="text" placeholder="请输入学号/工号" name="id" > td>

        tr>

         <tr>

            <td>密码:td>

            <td><input type="password" name="password" placeholder="请输入密码"> td>

        tr>

        <tr>

            <td>姓名:td>

            <td>

                <input type="text" placeholder="请输入姓名" name="name" >td>

        tr>

        <tr>

            <td>性别:td>

            <td>

                <input type="radio" name="sex" id="男" value="男" > <label for="男">label>

                <input type="radio" name="sex" id="女" value="女" > <label for="女">label>

            td>

        tr>

        <tr>

            <td>出生年月:td>

            <td>

                <select name="year">

                    <option>请选择年份option>

                    <option>1980option>

                    <option>1981option>

                    <option>1982option>

                    <option>1983option>

                    <option>1984option>

                    <option>1985option>

                    <option>1986option>

                    <option>1987option>

                    <option>1988option>

                    <option>1989option>

                    <option>1990option>

                    <option>1991option>

                    <option>1992option>

                    <option>1993option>

                    <option>1994option>

                    <option>1995option>

                    <option>1996option>

                    <option>1997option>

                    <option>1998option>

                    <option>1999option>

                    <option>2000option>

                    <option>2001option>

                    <option>2002option>

                    <option>2003option>

                select>

         

                <select name="month">

                    <option>请选择月份option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                select>

         

                <select name="day">

                    <option>请选择日期option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                    <option>13option>

                    <option>14option>

                    <option>15option>

                    <option>16option>

                    <option>17option>

                    <option>18option>

                    <option>19option>

                    <option>20option>

                    <option>21option>

                    <option>22option>

                    <option>23option>

                    <option>24option>

                    <option>25option>

                    <option>26option>

                    <option>27option>

                    <option>28option>

                    <option>29option>

                    <option>30option>

                    <option>31option>

                select>

            td>

        tr>

         <tr>

            <td> 专业名称:td>

            <td><input type="text" placeholder="请输入专业名称" name="profession" > td>

        tr>

        <tr>

            <td> 身份: td>

            <td>

                <input type="radio" name="job" id="student" checked="checked" value="student"> <label for="user">学生label>

                <input type="radio" name="job" id="teacher" value="teacher" > <label for="admini">老师label>

            td>

        tr>

 

        <tr>

            <td>td>

            <td>

                <input type="submit" value="添加">

            td>

        tr>

        <tr>

            <td>td>

       tr> 

 

    table>

    form>

<%

if(cw2!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw2}";

if(m == "账号已存在"){

alert('账号已存在');

}

 

script>

<%

}

%>

body>

html>

 

这个添加用户我基本直接从注册界面拿到的,代码略微调整了一下,然后提交到adduser servlet中:

package stu.Servlet;

 

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

 

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 stu.service.RegisterServiceimpl;

 

/**

 * Servlet implementation class Adduser

 */

@WebServlet("/adduser")

public class Adduser extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Adduser() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String name = request.getParameter("name");

String sex = request.getParameter("sex");

String i = request.getParameter("id");

int id=Integer.parseInt(i);

String password = request.getParameter("password");

String profession = request.getParameter("profession");

String year = request.getParameter("year");

String month = request.getParameter("month");

String day = request.getParameter("day");

String bir = year+"-"+month+"-"+day;

String job = request.getParameter("job");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

 

java.util.Date birthday = null;

 

try {

birthday = sdf.parse(bir);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String cw=null;

if( job.equals("student") ){

RegisterServiceimpl rsi = new RegisterServiceimpl();

try {

cw=rsi.registerStudent(id, name, password, sex, profession, birthday);

if( cw.equals("注册成功") ) {

request.getRequestDispatcher("Adduser.jsp").forward(request, response);

} else if( cw.equals("账号已存在")) {

request.setAttribute("cw2", "账号已存在");

request.getRequestDispatcher("Adduser.jsp").forward(request, response);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else if( job.equals("teacher") ) {

 RegisterServiceimpl rsi = new RegisterServiceimpl();

 try {

cw=rsi.registerTeacher(id, name, password, sex, profession, birthday);

if( cw.equals("注册成功") ) {

request.getRequestDispatcher("Adduser.jsp").forward(request, response);

} else if( cw.equals("账号已存在")) {

request.setAttribute("cw2", "账号已存在");

request.getRequestDispatcher("Adduser.jsp").forward(request, response);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

这里的这个servlet功能与registerservlet功能一样,都是判断job然后查询数据是否存在,不存在则插入,然后根据返回值判断插入是否成功,并返回刚刚的页面设置提醒,仅仅是页面跳转以及提醒的方式有一些不同。

 

Deleteuser页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

String cw = (String)request.getAttribute("cw");

%>

<form action="deleteuser" method="get" target="right"> <center>

<input type="radio" name="deletefs"  value="sid" class="radiob" checked="checked"/> <label> 学生 label> 

        <input type="radio" name="deletefs"  value="tid" class="radiob"> <label> 老师 label> <br><br>

<input type="text" placeholder="输入学号/工号" class="txtb" name="id" style="margin-left: 15px"/> <br><br>

<input type="submit" value="删除" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

center>

form>

<%

if(cw!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw}";

if(m == "删除成功"){

alert('删除成功');

} else if(m == "删除失败"){

alert('删除失败');

}

script>

<%

}

%>

body>

html>

 

这里我们先选择deletefs也就是选择删除学生的信息还是老师的信息,然后输入学号/工号,然后跳转到servlet:

package stu.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 stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Deleteuser

 */

@WebServlet("/deleteuser")

public class Deleteuser extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Deleteuser() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

String deletefs = request.getParameter("deletefs");

int id = Integer.parseInt(request.getParameter("id"));

TeacherServiceimpl tsi = new TeacherServiceimpl();

int n = 0;

if(deletefs.equals("sid")){

try {

n = tsi.DeleteStudentById(id);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else if(deletefs.equals("tid")) {

try {

n = tsi.DeleteTeacherById(id);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if( n == 0 ) {

request.setAttribute("cw", "删除失败");

        request.getRequestDispatcher("Deleteuser.jsp").forward(request, response);

} else if ( n == 1 ) {

request.setAttribute("cw", "删除成功");

        request.getRequestDispatcher("Deleteuser.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

判断选择删除的类型,然后根据不同的类型调用教师服务类进行删除,然后根据返回值设立提醒信息返回刚才的页面进行提醒。

 

Searchuser页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@page import="stu.entity.*"%>

<%@page import="java.util.ArrayList"%>

<%@page import="java.util.List"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

        Student student = (Student)request.getAttribute("student");

        Teacher teacher = (Teacher)request.getAttribute("teacher");

        List allstudent = (ArrayList)request.getAttribute("allstudent");

        List allstudent2 = (ArrayList)request.getAttribute("allstudent2");

        List allteacher = (ArrayList)request.getAttribute("allteacher");

        List allteacher2 = (ArrayList)request.getAttribute("allteacher2");

        int size1=0,size2=0,size3=0,size4=0;

        if(allstudent != null) size1=allstudent.size();

        if(allstudent2 != null) size2=allstudent2.size();

        if(allteacher != null) size3=allteacher.size();

        if(allteacher2 != null) size4=allteacher2.size();

%>

<form action="searchuser" method="get" target="right"> <center>

<input type="radio" name="deletefs"  value="sid" class="radiob" checked="checked"/> <label> 学生 label> 

        <input type="radio" name="deletefs"  value="tid" class="radiob"> <label> 老师 label>

        <input type="radio" name="deletefs"  value="name" class="radiob"> <label> 姓名 label>

        <input type="radio" name="deletefs"  value="profession" class="radiob"> <label> 专业 label>

<input type="text" placeholder="输入学号/工号" class="txtb" name="id" style="margin-left: 15px"/> 

<input type="submit" value="查询" style="background-color: #3c7cdd;border-radius: 5px;color: #f2f2f2;margin-left: 15px" />

center>

<table style="width: 100%; height: 20%" border="1">

<tr bgcolor="#B4CDCD">

<th><b> 学号/工号 b>th>

<th><b> 姓名 b>th>

<th><b> 性别 b>th>

<th><b> 专业 b>th>

tr>

<%

if(size1!=0){

for(int i=0;i

%>

<tr>

<td><%=allstudent.get(i).getSid()%>td>

<td><%=allstudent.get(i).getSname()%>td>

<td><%=allstudent.get(i).getSsex()%>td>

<td><%=allstudent.get(i).getSprofession()%>td>

tr>

<%

}}

%>

<%

if(size2!=0){

for(int i=0;i

%>

<tr>

<td><%=allstudent2.get(i).getSid()%>td>

<td><%=allstudent2.get(i).getSname()%>td>

<td><%=allstudent2.get(i).getSsex()%>td>

<td><%=allstudent2.get(i).getSprofession()%>td>

tr>

<%

}}

%>

<%

if(size3!=0){

for(int i=0;i

%>

<tr>

<td><%=allteacher.get(i).getTid()%>td>

<td><%=allteacher.get(i).getTname()%>td>

<td><%=allteacher.get(i).getTsex()%>td>

<td><%=allteacher.get(i).getTprofession()%>td>

tr>

<%

}}

%>

<%

if(size4!=0){

for(int i=0;i

%>

<tr>

<td><%=allteacher2.get(i).getTid()%>td>

<td><%=allteacher2.get(i).getTname()%>td>

<td><%=allteacher2.get(i).getTsex()%>td>

<td><%=allteacher2.get(i).getTprofession()%>td>

tr>

<%

}}

%>

<%

if(student != null){

%>

<tr>

<td><%=student.getSid()%>td>

<td><%=student.getSname()%>td>

<td><%=student.getSsex()%>td>

<td><%=student.getSprofession()%>td>

tr>

<%

}

%>

<%

if(teacher != null){

%>

<tr>

<td><%=teacher.getTid()%>td>

<td><%=teacher.getTname()%>td>

<td><%=teacher.getTsex()%>td>

<td><%=teacher.getTprofession()%>td>

tr>

<%

}

%>

table>

form>

body>

html>

在这里有四个选项,我们通过选择学号/工号/姓名/专业进行查询,输入信息跳转到servlet:

package stu.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 stu.entity.Student;

import stu.entity.Teacher;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Searchuser

 */

@WebServlet("/searchuser")

public class Searchuser extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Searchuser() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

String deletefs = request.getParameter("deletefs");

TeacherServiceimpl tsi = new TeacherServiceimpl();

if(deletefs.equals("sid")){

try {

int id = Integer.parseInt(request.getParameter("id"));

Student stu = tsi.SearchStudentById(id);

 request.setAttribute("student", stu );

        request.getRequestDispatcher("Searchuser.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else if(deletefs.equals("tid")) {

try {

int id = Integer.parseInt(request.getParameter("id"));

Teacher tea = tsi.SearchTeacherById(id);

request.setAttribute("teacher", tea );

        request.getRequestDispatcher("Searchuser.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else if(deletefs.equals("profession")) {

try {

String profession = request.getParameter("id");

List ls = tsi.SearchAllStudentByProfession(profession);

List lt = tsi.SearchAllTeacherByProfession(profession);

request.setAttribute("allstudent", ls );

request.setAttribute("allteacher", lt );

        request.getRequestDispatcher("Searchuser.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else if(deletefs.equals("name")) {

try {

String name = request.getParameter("id");

List ls = tsi.SearchAllStudentByName(name);

List lt = tsi.SearchAllTeacherByName(name);

request.setAttribute("allstudent2", ls );

request.setAttribute("allteacher2", lt );

        request.getRequestDispatcher("Searchuser.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

 

这里要注意,我们先判断查询的类型,然后进行查询,学号/工号分别代表的是查询学生表和教师表,而名字/专业查询则是两个表都查询,并设立返回值返回到查询页面进行显示。

 

修改用户信息changeuser页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<%

    String cw2 = (String)request.getAttribute("cw2");

%>

<form action="changeuser" method="get">

<table width="550" align="center" border="0">

        <tr>

            <td> 帐号(学号/工号):td>

            <td><input type="text" placeholder="请输入学号/工号" name="id" > td>

        tr>

         <tr>

            <td>密码:td>

            <td><input type="password" name="password" placeholder="请输入密码"> td>

        tr>

        <tr>

            <td>姓名:td>

            <td>

                <input type="text" placeholder="请输入姓名" name="name" >td>

        tr>

        <tr>

            <td>性别:td>

            <td>

                <input type="radio" name="sex" id="男" value="男" > <label for="男">label>

                <input type="radio" name="sex" id="女" value="女" > <label for="女">label>

            td>

        tr>

        <tr>

            <td>出生年月:td>

            <td>

                <select name="year">

                    <option>请选择年份option>

                    <option>1980option>

                    <option>1981option>

                    <option>1982option>

                    <option>1983option>

                    <option>1984option>

                    <option>1985option>

                    <option>1986option>

                    <option>1987option>

                    <option>1988option>

                    <option>1989option>

                    <option>1990option>

                    <option>1991option>

                    <option>1992option>

                    <option>1993option>

                    <option>1994option>

                    <option>1995option>

                    <option>1996option>

                    <option>1997option>

                    <option>1998option>

                    <option>1999option>

                    <option>2000option>

                    <option>2001option>

                    <option>2002option>

                    <option>2003option>

                select>

         

                <select name="month">

                    <option>请选择月份option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                select>

         

                <select name="day">

                    <option>请选择日期option>

                    <option>01option>

                    <option>02option>

                    <option>03option>

                    <option>04option>

                    <option>05option>

                    <option>06option>

                    <option>07option>

                    <option>08option>

                    <option>09option>

                    <option>10option>

                    <option>11option>

                    <option>12option>

                    <option>13option>

                    <option>14option>

                    <option>15option>

                    <option>16option>

                    <option>17option>

                    <option>18option>

                    <option>19option>

                    <option>20option>

                    <option>21option>

                    <option>22option>

                    <option>23option>

                    <option>24option>

                    <option>25option>

                    <option>26option>

                    <option>27option>

                    <option>28option>

                    <option>29option>

                    <option>30option>

                    <option>31option>

                select>

            td>

        tr>

         <tr>

            <td> 专业名称:td>

            <td><input type="text" placeholder="请输入专业名称" name="profession" > td>

        tr>

        <tr>

            <td> 身份: td>

            <td>

                <input type="radio" name="job" id="student" checked="checked" value="student"> <label for="user">学生label>

                <input type="radio" name="job" id="teacher" value="teacher" > <label for="admini">老师label>

            td>

        tr>

 

        <tr>

            <td>td>

            <td>

                <input type="submit" value="修改">

            td>

        tr>

        <tr>

            <td>td>

       tr> 

 

    table>

    form>

<%

if(cw2!=null){

%>

<script type="text/javascript">

var m="${requestScope.cw2}";

if(m == "更新成功"){

alert('更新成功');

} else if( m == "更新失败" ) {

alert('更新失败');

}

 

script>

<%

}

%>

body>

html>

 

这个界面还是同register界面差不多,不过id和job不能选错,否则可能会修改错误,造成信息错误。

下面介绍servlet:

package stu.Servlet;

 

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

 

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 stu.entity.Student;

import stu.entity.Teacher;

import stu.service.TeacherServiceimpl;

 

/**

 * Servlet implementation class Changeuser

 */

@WebServlet("/changeuser")

public class Changeuser extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Changeuser() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

String name = request.getParameter( "name" );

String sex = request.getParameter( "sex" );

String i = request.getParameter( "id" );

int id=Integer.parseInt(i);

String password = request.getParameter( "password" );

String profession = request.getParameter( "profession" );

String year = request.getParameter( "year" );

String month = request.getParameter( "month" );

String day = request.getParameter( "day" );

String bir = year+"-"+month+"-"+day;

String job = request.getParameter( "job" );

SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );

java.util.Date birthday = null;

        

try {

birthday = sdf.parse(bir);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

TeacherServiceimpl tsi = new TeacherServiceimpl();

int n = 0;

if( job.equals("student") ) {

try {

n = tsi.ChangeStudent(new Student(id , password , name , sex , profession , birthday));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else if( job.equals("teacher") ) {

try {

n = tsi.ChangeTeacher(new Teacher(id , password , name , sex , profession , birthday));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if( n == 0 ) {

request.setAttribute("cw2", "更新失败");

request.getRequestDispatcher("Changeuser.jsp").forward(request, response);

} else if( n == 1 ) {

request.setAttribute("cw2", "更新成功");

request.getRequestDispatcher("Changeuser.jsp").forward(request, response);

}

}

 

/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

 

}

 

还是根据job来分为学生和老师两种,然后根据id进行修改其他的信息,通过返回值设立提醒信息返回到修改界面提醒。

 

下面就是退出登录了,没有很好的写这个页面,就是点击后有个显示谢谢使用。

Colse页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title heretitle>

head>

<body>

<br><br><br><br><br><br>

<center><font color="red" size=7 >谢谢使用!font>center>

body>

html>

 

 

你可能感兴趣的:(javaweb)