使用Tomcat作为Web服务器和Servlet容器,使用SQL Server/MySQL作为数据服务器,从浏览器端发起对Servlet的调用,完成如下任务:
(1)向scores表中插入新的记录,表示录入新学生信息;(注意:学号为主键,插入学号相同的学生要提示错误;如果学号由数据库自动生成除外)
(2)批量录入成绩,即:在一个界面上列出所有学生信息,在同一个界面上成绩还可以重新录入;录入部分或者全部学生成绩后,提交,将所有成绩写入scores表。(没有录入的成绩,可以暂定为0写入scores表)
使用的静态html页面和动态Servlet的个数没有限制。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td><a href="searchall">查询全部+修改成绩</a></td>
</tr>
</table>
<table>
<tr>
<td><a href="InsertAndSearch">查询全部+批量录入(包括成绩)</a></td>
</tr>
</table>
<table>
<tr>
<td><a href="InsertAndSearchTwo">查询全部+批量录入(不包括成绩)</a></td>
</tr>
</table>
<table>
<tr>
<td><a href="deleteall">全部删除</a></td>
</tr>
</table>
<table>
<tr>
<td><a href="quanb">shishi</a></td>
</tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="TheStudent.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<body>
<table align="center" width="400" border="1">
<tr>
<td align="center" colspan="5">
<h2>所有学生信息</h2>
</td>
</tr>
<tr align="center">
<td><b>学号</b></td>
<td><b>姓名</b></td>
<td><b>性别</b></td>
<td><b>成绩</b></td>
<td><b>修改成绩</b></td>
</tr>
<%
// 获取图书信息集合
List<Student> list=(List<Student>)request.getAttribute("list");
if(list==null || list.size()<1){
out.print("没有 数据!");
}
else{
for(Student student:list){
%>
<tr align="center">
<td><%=student.getId() %></td>
<td><%=student.getName() %></td>
<td><%=student.getSex() %></td>
<td><%=student.getGrade() %></td>
<td>
<form action="Alter" method="post" οnsubmit="return check(this);">
<input name="id" id="id" type="hidden" value=<%=student.getId() %>>
<input name="grade" id="grade" type="text" size="5"/>
<input type="submit" value="修 改">
</form>
</td>
</tr>
<%
}
}
%>
</table>
<a href="Start.html">返回上一目录</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="TheStudent.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table align="center" width="500" border="1">
<form action="InsertTwo" method="post" οnsubmit="return check(this);">
<tr>
<td align="center" colspan=7">
<h2><b>录入信息</b></h2>
</td>
</tr>
<tr align="center">
<td><b>学号</b></td>
<td><input name="id" id="id" type="text" size="4"/></td>
<td><b>姓名</b></td>
<td><input name="name" id="name" type="text" size="6"/> </td>
<td><b>性别</b></td>
<td><input name="sex" id="sex" type="text" size="4"/></td>
<td><input name="Submit" type="submit" value="添加" οnclick=getParameter></td>
</tr>
</table>
<table align="center" width="500" border="1">
<tr>
<td align="center" colspan="4">
<h2>所有学生信息</h2>
</td>
</tr>
<tr align="center">
<td><b>学号</b></td>
<td><b>姓名</b></td>
<td><b>性别</b></td>
<td><b>成绩</b></td>
</tr>
<%
// 获取图书信息集合
List<Student> list=(List<Student>)request.getAttribute("list");
if(list==null || list.size()<1){
out.print("没有 数据!");
}
else{
for(Student student:list){
%>
<tr align="center">
<td><%=student.getId() %></td>
<td><%=student.getName() %></td>
<td><%=student.getSex() %></td>
<td><%=student.getGrade() %></td>
</tr>
<%
}
}
%>
</table>
<a href="Start.html">返回上一目录</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="TheStudent.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table align="center" width="500" border="1">
<form action="Insert" method="post" οnsubmit="return check(this);">
<tr>
<td align="center" colspan="9">
<h2><b>录入信息</b></h2>
</td>
</tr>
<tr align="center">
<td><b>学号</b></td>
<td><input name="id" id="id" type="text" size="4"/></td>
<td><b>姓名</b></td>
<td><input name="name" id="name" type="text" size="6"/> </td>
<td><b>性别</b></td>
<td><input name="sex" id="sex" type="text" size="4"/></td>
<td><b>成绩</b></td>
<td><input name="grade" id="grade" type="text" size="4"/></td>
<td><input name="Submit" type="submit" value="添加" οnclick=getParameter></td>
</tr>
</table>
<table align="center" width="500" border="1">
<tr>
<td align="center" colspan="4">
<h2>所有学生信息</h2>
</td>
</tr>
<tr align="center">
<td><b>学号</b></td>
<td><b>姓名</b></td>
<td><b>性别</b></td>
<td><b>成绩</b></td>
</tr>
<%
// 获取图书信息集合
List<Student> list=(List<Student>)request.getAttribute("list");
if(list==null || list.size()<1){
out.print("没有 数据!");
}
else{
for(Student student:list){
%>
<tr align="center">
<td><%=student.getId() %></td>
<td><%=student.getName() %></td>
<td><%=student.getSex() %></td>
<td><%=student.getGrade() %></td>
</tr>
<%
}
}
%>
</table>
<a href="Start.html">返回上一目录</a>
</body>
</html>
package TheStudent;
public class Student {
private int id;
private String name;
private String sex;
private int grade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//---------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//---------
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//---------------
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
package TheStudent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 searchall
*/
@WebServlet("/searchall")
public class searchall extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public searchall() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String sql = "select * from score";
ResultSet rs = stmt.executeQuery(sql);
List<Student> list = new ArrayList<Student>();
while (rs.next()) {
Student stu= new Student();
stu.setId(rs.getInt("学号"));
stu.setName(rs.getString("姓名"));
stu.setSex(rs.getString("性别"));
stu.setGrade(rs.getInt("得分"));
list.add(stu);
}
request.setAttribute("list", list);
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("searchall.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);
}
}
package TheStudent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 InsertTwo
*/
@WebServlet("/InsertTwo")
public class InsertTwo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertTwo() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("utf-8"); //1
response.setContentType("text/html;charset=utf-8");//2
response.setCharacterEncoding("utf-8"); //3
PrintWriter out =response.getWriter();
//====================================================================================
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = null;
//---------------------------------------------------------
String id = request.getParameter("id");
int idit = Integer.parseInt(id);
String name = request.getParameter("name");
String sex = request.getParameter("sex");
//---------------------------------------------------------
rs=stmt.executeQuery("select * from score where 学号="+idit);
if(rs.next())
{
out.println("学号已经存在
");
System.out.println("学号已经存在");
rs.close();
stmt.close();
conn.close();
}
else
{
Object insert=stmt.execute("insert into score (学号,姓名,性别)values('"+idit+"','"+name+"','"+sex+"')");
rs.close();
stmt.close();
conn.close();
request.getRequestDispatcher("InsertAndSearchTwo").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException 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);
}
}
package TheStudent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 InsertAndSearchTwo
*/
@WebServlet("/InsertAndSearchTwo")
public class InsertAndSearchTwo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertAndSearchTwo() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String sql = "select * from score";
ResultSet rs = stmt.executeQuery(sql);
List<Student> list = new ArrayList<Student>();
while (rs.next()) {
Student stu= new Student();
stu.setId(rs.getInt("学号"));
stu.setName(rs.getString("姓名"));
stu.setSex(rs.getString("性别"));
stu.setGrade(rs.getInt("得分"));
list.add(stu);
}
request.setAttribute("list", list);
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("InsertAndSearchTwo.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);
}
}
package TheStudent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 InsertAndSearch
*/
@WebServlet("/InsertAndSearch")
public class InsertAndSearch extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertAndSearch() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String sql = "select * from score";
ResultSet rs = stmt.executeQuery(sql);
List<Student> list = new ArrayList<Student>();
while (rs.next()) {
Student stu= new Student();
stu.setId(rs.getInt("学号"));
stu.setName(rs.getString("姓名"));
stu.setSex(rs.getString("性别"));
stu.setGrade(rs.getInt("得分"));
list.add(stu);
}
request.setAttribute("list", list);
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("InsertAndSearch.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);
}
}
package TheStudent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 Insert
*/
@WebServlet("/Insert")
public class Insert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Insert() {
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"); //1
response.setContentType("text/html;charset=utf-8");//2
response.setCharacterEncoding("utf-8"); //3
PrintWriter out =response.getWriter();
//====================================================================================
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = null;
//---------------------------------------------------------
String id = request.getParameter("id");
int idit = Integer.parseInt(id);
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String grade = request.getParameter("grade");
int gradeit = Integer.parseInt(grade);
//---------------------------------------------------------
rs=stmt.executeQuery("select * from score where 学号="+idit);
if(rs.next())
{
out.println("学号已经存在
");
System.out.println("学号已经存在");
rs.close();
stmt.close();
conn.close();
}
else
{
Object insert=stmt.execute("insert into score values('"+idit+"','"+name+"','"+sex+"','"+gradeit+"')");
rs.close();
stmt.close();
conn.close();
request.getRequestDispatcher("InsertAndSearch").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException 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);
}
}
package TheStudent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 deleteall
*/
@WebServlet("/deleteall")
public class deleteall extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public deleteall() {
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
// response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("utf-8"); //1
response.setContentType("text/html;charset=utf-8");//2
response.setCharacterEncoding("utf-8"); //3
PrintWriter out =response.getWriter();
//===============================================
Connection conn=null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String ur1="jdbc:sqlserver://localhost:1433;databaseName = Student";
String usename = "sa";
String password = "123456";
conn=DriverManager.getConnection(ur1,usename,password);
stmt = conn.createStatement();
}catch (Exception e) {
// TODO: handle exception
}
String name = request.getParameter("name");
try {
Object delete=stmt.execute("delete from score");
System.out.println("已删除"+name);
out.print("已删除"+name+"
");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.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);
}
}
package TheStudent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import 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 Alter
*/
@WebServlet("/Alter")
public class Alter extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Alter() {
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
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName = Student";
String username = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
//---------------------------------------------------------
String id = request.getParameter("id");
int idit = Integer.parseInt(id);
String grade = request.getParameter("grade");
int gradeit = Integer.parseInt(grade);
//---------------------------------------------------------
String sql = "update score set 得分="+gradeit+"where 学号="+idit;
ResultSet rs = stmt.executeQuery(sql);
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("searchall").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);
}
}