按照老师的要求,掌握数据库的连接,增删查改
大作业展示
大作业展示链接:https://www.bilibili.com/video/BV1f5411s7iy
源码链接:https://download.csdn.net/download/Mr_yuekitty/12473112
思路:
建个表格
实现增删查改
完事
显示界面
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<html>
<head>
<title>成绩</title>
</head>
<body>
<center>
<h1>学生成绩表</h1>
<hr width="80%">
<a href="insert_page.jsp" style="font-size: 20px">添加信息</a>
<table border="1" width="80%" align="center">
<tr align="center">
<th style="width: 20%"><b>学号</b></th>
<th style="width: 20%"><b>姓名</b></th>
<th style="width: 20%"><b>科目</b></th>
<th style="width: 20%"><b>成绩</b></th>
<th style="width: 20%"><b>删除</b></th>
<th style="width: 20%"><b>更新</b></th>
</tr>
<form action="update.jsp">
<%
for (int i = 1; i <= 10; i++) {
if (!rs.next()) {
break;
}
int a = rs.getInt(1);
String b = rs.getString(2);
String c = rs.getString(3);
int d = rs.getInt(4);
%>
<tr align="center">
<td style="width: 20%"><input name="id" value="<%=a%>"></td>
<td style="width: 20%"><input type="text" name="name" value="<%=b%>"></td>
<td style="width: 20%"><input type="text" name="course" value="<%=c%>"></td>
<td style="width: 20%"><input type="text" name="score" value="<%=d%>"></td>
<td style="width: 20%"><input type="button" value="删除" οnclick="del(<%=a%>)"></td>
<td style="width: 20%"><input type="submit" value="更新"></td>
</tr>
</form>
<%
}
%>
</table>
</center>
<script>
function del(x) {
window.location.href = "delete.jsp?id=" + x;
}
</script>
</body>
</html>
数据库的连接
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
%>
增删改查
增
增加的页面
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String course = request.getParameter("course");
String score = request.getParameter("score");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO test(id,name,course,score) value(?,?,?,?)";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, Integer.parseInt(id));
psmt.setString(2, name);
psmt.setString(3, course);
psmt.setInt(4, Integer.parseInt(score));
int rs = psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
增加的实现
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<html>
<head>
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
String sql = "SELECT max(id) FROM test";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
rs.next();
int id = rs.getInt(1) + 1;
conn.close();
%>
<title>插入页面</title>
</head>
<body>
<center>
<a>insert message</a>
<hr>
<form action="insert.jsp">
<a>学号:<input name="id" type="text" value="<%=id%>"></a>
<a>姓名:<input type="text" name="name"></a>
<a>科目:<select name="course">
<option>语文</option>
<option>数学</option>
<option>英语</option>
</select>
</a>
<a>成绩:<input type="text" name="score"></a>
<input type="submit" value="submit">
</form>
</center>
<%
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
删
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql="DELETE FROM test WHERE id=?";
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setInt(1,Integer.parseInt(id));
psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
改
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String course = request.getParameter("course");
String score = request.getParameter("score");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
String sql="UPDATE test SET name=? ,course=?,score=?,id=?";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,name);
psmt.setString(2,course);
psmt.setInt(3,Integer.parseInt(score));
psmt.setInt(4,Integer.parseInt(id));
psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>