javaweb连接数据库实现图书管理系统
2022-10-19 15:09:36 # javaweb

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/19
Time: 12:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*" language="java" %>

<html>
<head>
<title>图书管理系统</title>
</head>
<body>
<p align="center">
<a href="./add.html">添加图书信息</a>
</p>
<table align="center" width="50%" border="1">
<tr>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>价格</th>
<th>管理</th>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","wangzhe2412");
String sql = "select * from bookinfo";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
%>
<tr>
<td><%= rs.getString("bookname")%></td>
<td><%= rs.getString("author")%></td>
<td><%= rs.getString("press")%></td>
<td><%= rs.getString("price")%></td>
<td>
<a href="./edit.jsp?id=<%=id%>">修改</a>
<a href="./del.jsp?id=<%=id%>" onclick="return confirm('确定要删除吗?')">删除</a>
</td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>

add.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加图书信息</title>
</head>
<body>
<h2 align="center">添加图书信息</h2>
<form name='form1' action="./add.jsp" method="post">
<table align="center" width="30%" border="1">
<tr>
<th width="30%">书名:</th>
<td><input type="text" name="bookname"></td>
</tr>
<tr>
<th width="30%">作者:</th>
<td><input type="text" name="author"></td>
</tr>
<tr>
<th width="30%">出版社:</th>
<td><input type="text" name="press"></td>
</tr>
<tr>
<th width="30%">价格:</th>
<td><input type="text" name="price"></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="添加">
<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
</body>
</html>

add.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/10/12
Time: 14:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%
request.setCharacterEncoding("UTF-8");
String bookname = request.getParameter("bookname");
String author = request.getParameter("author");
String press = request.getParameter("press");
String price = request.getParameter("price");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","wangzhe2412");
String sql = "insert into bookinfo values(null,?,?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
pstmt.setString(1,bookname);
pstmt.setString(2,author);
pstmt.setString(3,press);
pstmt.setFloat(4,Float.parseFloat(price));
int result = pstmt.executeUpdate();
String msg = "添加失败,单击确定跳转到图书列表页";
if(result == 1){
msg = "添加成功,单击确定跳转到图书列表页";
}
pstmt.close();
con.close();
%>
<script>
window.alert('<%= msg%>');
</script>
<%
response.setHeader("Refresh","1,url =./index.jsp");
%>
<html>
<head>
<title>Title</title>
</head>
<body>

</body>
</html>

edit.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/10/19
Time: 13:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改图书信息</title>
</head>
<body>
<%
String id = request.getParameter("id");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","wangzhe2412");
String sql = "select * from bookinfo where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1,Integer.parseInt(id));
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String bookname = rs.getString("bookname");
String author = rs.getString("author");
String press = rs.getString("press");
float price = rs.getFloat("price");
%>
<h2 align="center">修改图书信息</h2>
<form action="./edit_do.jsp" method="post" name="form1">
<input type="hidden" name="id" value="<%= id%>">;
<table align="center" width="30%" border="1">
<tr>
<th width="30%">书名:</th>
<td><input type="text" name="bookname" value="<%=bookname%>"></td>
</tr>
<tr>
<th width="30%">作者:</th>
<td><input type="text" name="author" value="<%=author%>"></td>
</tr>
<tr>
<th width="30%">出版社:</th>
<td><input type="text" name="press" value="<%=press%>"></td>
</tr>
<tr>
<th width="30%">价格:</th>
<td><input type="text" name="price" value="<%=price%>"></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="修改">
<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
<%
}
rs.close();
pstmt.close();
con.close();
%>
</body>
</html>

edit_do.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/10/19
Time: 13:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String bookname = request.getParameter("bookname");
String author = request.getParameter("author");
String press = request.getParameter("press");
String price = request.getParameter("price");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","wangzhe2412");
String sql = "update bookinfo set bookname=?,author=?,press=?,price=? where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,bookname);
pstmt.setString(2,author);
pstmt.setString(3,press);
pstmt.setFloat(4,Float.parseFloat(price));
pstmt.setInt(5,Integer.parseInt(id));
int result = pstmt.executeUpdate();
String msg = "修改失败,点击确定跳转到图书列表页";
if(result == 1){
msg = "修改成功,点击确定跳转到图书列表页";
}
pstmt.close();
con.close();
%>
<script>window.alert("<%=msg%>");</script>
<%
response.setHeader("Refresh","1,url=./index.jsp");
%>
<html>
<head>
<title>Title</title>
</head>
<body>

</body>
</html>

del.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/10/19
Time: 14:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String id = request.getParameter("id");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","wangzhe2412");
String sql = "delete from bookinfo where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1,Integer.parseInt(id));
int result = pstmt.executeUpdate();
String msg = "删除失败,点击确定跳转到图书列表页";
if(result == 1){
msg = "删除成功,点击确定跳转到图书列表页";
}
pstmt.close();
con.close();
%>
<script>window.alert("<%=msg%>");</script>
<%
response.setHeader("Refresh","1,url = ./index.jsp");
%>
<html>
<head>
<title>Title</title>
</head>
<body>

</body>
</html>

总结

只是用了结构,没有运用样式表。😁