【JavaWeb】学生成绩管理系统第一部分:录入数据
html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生成绩管理系统</title>
</head>
<body>
<h1>欢迎使用学生成绩管理系统</h1>
<hr>
<ul>
<li><a href="/app2/record.html">录入功能</a></li>
<li><a href="URL">查询功能</a></li>
<li><a href="URL">修改功能</a></li>
<li><a href="URL">排序功能</a></li>
<li><a href="URL">统计功能</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>录入学生成绩</title>
</head>
<body>
<h2>录入学生成绩</h2>
<form action="/app2/submit" method="get">
<label for="studentId">学生 ID:</label><br>
<input type="text" id="studentId" name="studentId"><br>
<label for="studentName">学生姓名:</label><br>
<input type="text" id="studentName" name="studentName"><br>
<label for="chinese">语文成绩:</label><br>
<input type="text" id="chinese" name="chinese"><br>
<label for="math">数学成绩:</label><br>
<input type="text" id="math" name="math"><br>
<label for="english">英语成绩:</label><br>
<input type="text" id="english" name="english"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Java部分的笔记
通过三个自定义方法将表单数据插入数据库,并返回插入成功的结果到浏览器
//读取表单数据
private Student getStu(HttpServletRequest request)
//将一个学生类插入数据库
private void insert(Student student)
/*注册驱动前,记着手动导mysql的包*/
//在浏览器上显示插入成功的信息
private void successResponse(HttpServletResponse response) throws IOException
Java代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取表单参数
Student student=getStu(request);
// 将学生成绩对象写入数据库
insert(student);
// 显示成功消息
successResponse(response);
}
private Student getStu(HttpServletRequest request) {
String studentId = request.getParameter("studentId");
String studentName = request.getParameter("studentName");
int chinese = Integer.parseInt(request.getParameter("chinese"));
int math = Integer.parseInt(request.getParameter("math"));
int english = Integer.parseInt(request.getParameter("english"));
// 创建学生成绩对象
Student student = new Student(studentId, studentName);
student.marks= new Marks(chinese, math, english);
return student;
}
private void insert(Student student) {
Statement stmt = null;
Connection conn = null;
try {
//1.注册驱动
//类加载的方式注册驱动
/*记着手动导包*/
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.1:3306/student_marks";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
//3.创建statement
stmt = conn.createStatement();
//4.发送sql语句
String sql = "INSERT INTO Marks (id, name, chinese,math,english,sum,avg) VALUES ( " + student + " );";
System.out.println(sql);
stmt.executeUpdate(sql);
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
//6.释放资源
//从小到大依次关闭
//分别对其try catch
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void successResponse(HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\">");
out.println("<title>表单提交成功</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>表单提交成功</h1>");
out.println("<p>你提交的表单数据已成功插入数据库。</p>");
out.println("<a href=\"record.html\">继续录入数据</a>");
out.println("<a href=\"index.html\">返回主页</a>");
out.println("</body>");
out.println("</html>");
}