基于JavaWeb开发的学生成绩,使用MyEclipse2014开发,连接MySQL数据库,存储学生的身份信息、成绩,管理员的身份信息,课程信息。
功能说明:
学生注册。学生身份:跳转到学生登录界面,点击查询成绩按钮,以表格的形式在该页面显示该学生所有成绩。管理员身份:进入功能选择页面,以列表形式显示超链接,录入成绩、修改成绩、查询成绩。录入成绩页面中,管理员输入学号、课程号和成绩进行录入,点击添加跳出弹框提示是否录入成功;查询成绩界面,可选择按学号查询和按课程号查询,在页面下方以表格形式显示成绩;修改成绩界面,先按学号或课程号查询出成绩,显示当前成绩,可进行修改、删除操作,跳出弹框显示是否修改成功。
1.编写JavaBean,保存信息:
1.1学生信息Student.java
package beans;
public class Student {
private String studentid;
private String studentname;
private String password;
public Student(){}
public Student(String studentid,String studentname,String password){
this.studentid=studentid;
this.studentname=studentname;
this.password=password;
}
public String getStudentid(){
return studentid;
}
public void setStudentid(String studentid){
this.studentid=studentid;
}
public String getStudentname(){
return studentname;
}
public void setStudentname(String studentname){
this.studentname=studentname;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
}
1.2管理员信息Admin.java
package beans;
public class Admin {
private String adminid;
private String adminname;
private String password;
public Admin(){}
public Admin(String adminid,String adminname,String password){
this.adminid=adminid;
this.adminname=adminname;
this.password=password;
}
public String getAdminid(){
return adminid;
}
public void setAdminid(String studentid){
this.adminid=studentid;
}
public String getAdminname(){
return adminname;
}
public void setAdminname(String studentname){
this.adminname=studentname;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
}
1.3课程信息Course.java
package beans;
public class Course {
private String courseid;
private String coursename;
public String getCourseid() {
return courseid;
}
public void setCourseid(String courseid) {
this.courseid = courseid;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
}
1.4学生分数Score.java
package beans;
public class Score {
private String studentid;
private String studentname;
private String courseid;
private String coursename;
private String score;
public Score(){}
public Score(String studentid,String courseid,String coursename,String score){
this.studentid=studentid;
this.courseid=courseid;
this.coursename=coursename;
this.score=score;
}
public String getStudentid(){
return studentid;
}
public void setStudentid(String studentid){
this.studentid=studentid;
}
public String getScore(){
return score;
}
public void setScore(String score){
this.score=score;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getCourseid() {
return courseid;
}
public void setCourseid(String courseid) {
this.courseid = courseid;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
}
2.编写DAO,连接数据库,并定义相关查询分数、增加成绩、删除成绩方法
2.1CourseDAO.java。通过课程号查找课程名方法。
package DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import beans.Course;
import beans.Student;
public class CourseDAO {
private static Connection conn=null;
public static void initConnection() throws Exception{//建立连接
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
}
public static Course findclasses(String courseid) throws Exception{//查找课程
initConnection();
Statement state =null;
ResultSet rs = null;
Course course=new Course();
try{
String sql = "select * from course where courseid = '"+courseid+"'";
state = conn.createStatement();
rs=state.executeQuery(sql);
if(rs.next()){
course.setCourseid(rs.getString("courseid"));
course.setCoursename(rs.getString("coursename"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
closeConnection();
}
return course;
}
public static void closeConnection() throws Exception{//断开连接
conn.close();
}
}
2.2UserDAO.java。登录时通过学号或管理员账号查找学生或管理员进行登录验证。注册时学生信息增加。
package DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import beans.Admin;
import beans.Student;
public class UserDAO {
private static Connection conn=null;
public static void initConnection() throws Exception{//建立连接
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
}
public static Student findstudent(String studentid) throws Exception{//查找学生
initConnection();
Statement state =null;
ResultSet rs = null;
Student student=new Student();
try{
String sql = "select * from student where studentid = '"+studentid+"'";
state = conn.createStatement();
rs=state.executeQuery(sql);
if(rs.next()){
student.setStudentid(rs.getString("studentid"));
student.setPassword(rs.getString("password"));
student.setStudentname(rs.getString("studentname"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
closeConnection();
}
return student;
}
public static boolean addStudent(Student student) {//增加学生
boolean flag=false;
int i=0;
try {
initConnection();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Statement state =null;
String sql= "insert into student (studentid,studentname,password) values('"+student.getStudentid()+"','"+student.getStudentname()+"','"+student.getPassword()+"')";
try {
state = conn.createStatement();
i=state.executeUpdate(sql);
if(i>0){
flag=true;
}
}catch (SQLException e) {
e.printStackTrace();
}
try {
closeConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public static Admin findadmin(String adminid) throws Exception{//查找管理员
initConnection();
Statement state =null;
ResultSet rs = null;
Admin admin=new Admin();
try{
String sql = "select * from admin where adminid = '"+adminid+"'";
state = conn.createStatement();
rs=state.executeQuery(sql);
if(rs.next()){
admin.setAdminid(rs.getString("adminid"));
admin.setPassword(rs.getString("password"));
admin.setAdminname(rs.getString("adminname"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
closeConnection();
}
return admin;
}
public static void closeConnection() throws Exception{//断开连接
conn.close();
}
}
2.3ScoreDAO.java。通过学号或课程号查找成绩。增加、删除、修改成绩等方法。
package DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import beans.Score;
import beans.Student;
public class ScoreDAO {
private static Connection conn=null;
public static void initConnection() throws Exception{//建立连接
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
}
p