1)在MySQL数据库中创建一个StudentGradeTable数据表,添加字段FlowId,Type,IdCard,ExamCard,StudentName,Location,Grade并录入几条数据
2)简单实用MVC设计模式,model为StudentDAO.java,view为studentsList.jsp,Controller为ListAllStudent.java,分别实现数据库访问,页面呈现以及前后端控制的功能
3)将数据库内的学生信息,以表格的形式呈现在Web页面上
#创建数据库
DROP DATABASE IF EXISTS myWebProject;
CREATE DATABASE myWebProject;
#使用数据库
USE myWebProject;
#创建四六级学生成绩表
CREATE TABLE StudentGradeTable(
FlowId INT PRIMARY KEY AUTO_INCREMENT,
`Type` INT NOT NULL,
IdCard VARCHAR(20) UNIQUE ,
ExamCard VARCHAR(20) UNIQUE ,
StudentName VARCHAR(20) NOT NULL,
Location VARCHAR(20),
Grade INT NOT NULL
);
DESC StudentGradeTable;
#插入数据
INSERT INTO StudentGradeTable(`Type`,`IdCard`,`ExamCard`,`StudentName`,`Location`,`Grade`)
VALUES(4,'211212199709092345','19970909','小李','泗阳',90);
INSERT INTO StudentGradeTable(`Type`,`IdCard`,`ExamCard`,`StudentName`,`Location`,`Grade`)
VALUES(6,'211212199605092345','19960509','小王','贵阳',96);
INSERT INTO StudentGradeTable(`Type`,`IdCard`,`ExamCard`,`StudentName`,`Location`,`Grade`)
VALUES(6,'211212199504092345','19950409','小狗','潮汕',120);
INSERT INTO StudentGradeTable(`Type`,`IdCard`,`ExamCard`,`StudentName`,`Location`,`Grade`)
VALUES(4,'211212199410102345','19941010','小刘','南通',145);
INSERT INTO StudentGradeTable(`Type`,`IdCard`,`ExamCard`,`StudentName`,`Location`,`Grade`)
VALUES(4,'211212199310012345','19931001','小嵇','永城',127);
#查询
SELECT * FROM StudentGradeTable;
#删除数据表
DROP TABLE IF EXISTS StudentGradeTable;
结果如图所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
查询学生信息
package com.njupt.javaweb;
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;
/**
* Servlet implementation class ListAllStudent
*/
@WebServlet("/ListAllStudent")
public class ListAllStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
System.out.println("请求来了。。。");
List students = new StudentDAO().getAll();
//设置属性
request.setAttribute("studentsList", students);
//转发
request.getRequestDispatcher("/studentsList.jsp").forward(request, response);
}
}
package com.njupt.javaweb;
public class Student {
//属性名与数据库一一对应
private int flowId;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String location;
private int grade;
//属性的getter与setter方法
public int getFlowId() {
return flowId;
}
public void setFlowId(int flowId) {
this.flowId = flowId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getExamCard() {
return examCard;
}
public void setExamCard(String examCard) {
this.examCard = examCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
//方便初始化,写一个带参构造器
public Student(int flowId, int type, String idCard, String examCard, String studentName, String location,
int grade) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
//方便反射,留一个无参构造器
public Student() {
}
@Override
public String toString() {
return "Student [flowId=" + flowId + ", type=" + type + ", idCard=" + idCard + ", examCard=" + examCard
+ ", studentName=" + studentName + ", location=" + location + ", grade=" + grade + "]";
}
}
package com.njupt.javaweb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
public List getAll() {
// 1.准备连接数据库的基本信息
String driverClass = "com.mysql.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost:3306/myWebProject";
String mysqlUser = "root";
String mysqlPassword = "root";
// 2.加载数据库驱动
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 3.获取数据库连接以及处理数据
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sqlQueryAllStudent = "SELECT * FROM StudentGradeTable";
List studentsList = new ArrayList();
try {
conn = DriverManager.getConnection(jdbcUrl, mysqlUser, mysqlPassword);
preparedStatement = conn.prepareStatement(sqlQueryAllStudent);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Student stu = new Student(resultSet.getInt(1), resultSet.getInt(2), resultSet.getString(3),
resultSet.getString(4), resultSet.getString(5), resultSet.getString(6), resultSet.getInt(7));
studentsList.add(stu);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
return studentsList;
}
}
<%@page import="com.njupt.javaweb.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
学生信息
流水号
成绩类型
身份证号
考试编号
学生姓名
家庭住址
考试成绩
<%
List studentsList = (List)request.getAttribute("studentsList");
for(Student stu : studentsList){
%>
<%= stu.getFlowId() %>
<%= stu.getType() %>
<%= stu.getIdCard() %>
<%= stu.getExamCard() %>
<%= stu.getStudentName() %>
<%= stu.getLocation() %>
<%= stu.getGrade() %>
<%
}
%>
上面代码只是一个Demo仅做参考,和自己学习,还有很多缺陷。