2024/6/10
所学时间:4小时
代码行数:200
博客园数:1篇
所学知识:
1) add.jsp文件代码
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>
<html>
<head>
<title>添加图书信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form action="addsave.jsp" method="post">
<h2>添加客户信息</h2>
<table style="width: 50%">
<tr>
<th width="30%">ID:</th>
<td width="70%"><input name="id" type="text"></td>
</tr>
<tr>
<th>姓名:</th>
<td><input name="name" type="text"></td>
</tr>
<tr>
<th>地址:</th>
<td><input name="address" type="text"></td>
</tr>
<tr>
<th>手机号:</th>
<td><input name="phone" type="text"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="添加">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
2) addsave.jsp文件代码
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>
<html>
<head>
<title>添加图书信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String bookname = request.getParameter("id");
String author = request.getParameter("name");
String price = request.getParameter("address");
String phone = request.getParameter("phone");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
//使用Statement对象
// Statement stmt = con.createStatement();
// String sql = "insert into bookinfo(bookname,author,price) values('" + bookname + "','" + author + "'," + price + ")";
// System.out.println(sql);
// int i = stmt.executeUpdate(sql);
PreparedStatement stmt = con.prepareStatement("insert into kehu(id,name,address,phone) values(?, ?, ?,?)");
stmt.setString(1, bookname);
stmt.setString(2, author);
stmt.setString(3, price);
stmt.setString(4, phone);
int i = stmt.executeUpdate();
if (i == 1) {
out.println("<h2>添加成功!</h2><br/>");
out.println("<a href='index.jsp'>返回首页</a>");
} else {
out.println("<h2>添加失败!</h2><br/>");
out.println("<a href='add.jsp'>重新添加</a>");
}
stmt.close();
con.close();
%>
</body>
</html>
3) del.jsp文件代码
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" pageEncoding="utf-8"%>
<html>
<head>
<title>删除图书信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Connection con = null;
try {con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Statement stmt= null;
try {
stmt = con.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
String id=request.getParameter("id");
int i= 0;
try {
i = stmt.executeUpdate("delete from kehu where id="+id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(i==1)
{
out.println("<h2>删除成功!</h2><br/>");
}
else
{
out.println("<h2>删除失败!</h2><br/>");
}
out.println("<a href='index.jsp'>返回首页</a>");
stmt.close();
con.close();
%>
</body>
</html>
4) edit.jsp文件代码
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
<html>
<head>
<title>修改图书信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from kehu where id=" + id);
// PreparedStatement stmt = con.prepareStatement("select * from bookinfo where id=?");
// stmt.setString(1, id);
// ResultSet rs = stmt.executeQuery();
rs.next();
%>
<form action="editsave.jsp" method="post">
<h2>修改客户信息</h2>
<table style="width:50%">
<tr>
<th width="30%">姓名:</th>
<td width="70%"><input name="name" type="text"
value="<%=rs.getString(2)%>"></td>
</tr>
<tr>
<th>地址:</th>
<td><input name="address" type="text"
value="<%=rs.getString(3)%>"></td>
</tr>
<tr>
<th>手机号:</th>
<td><input name="phone" type="text"
value="<%=rs.getString(4)%>"></td>
</tr>
<tr>
<td colspan="2"><input type="hidden" name="id" value="<%=id%>">
<input type="submit" value="修改"> <input type="reset"
value="重置"></td>
</tr>
</table>
</form>
<%
rs.close();
stmt.close();
con.close();
%>
</body>
</html>
5) editsave.jsp文件代码
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
<html>
<head>
<title>修改完成</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String bookname = request.getParameter("name");
String author = request.getParameter("address");
String price = request.getParameter("phone");
String id = request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = con.createStatement();
String sql = "update kehu set name='" + bookname + "',address='" + author + "',phone=" + price
+ " where id=" + id;
System.out.println(sql);
int i = stmt.executeUpdate(sql);
if (i == 1) {
out.println("<h2>修改成功!</h2><br/>");
out.println("<a href='index.jsp'>返回首页</a>");
} else {
out.println("<h2>修改失败!</h2><br/>");
out.println("<a href='edit.jsp?id='" + id + ">重新添加</a>");
}
stmt.close();
con.close();
%>
</body>
</html>
6) error.jsp文件代码
<%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>error page</title>
</head>
<body>
<h1>程序出现错误</h1>
<div>错误信息为:<br/>
<%=exception.getMessage()%><br/>
<%=exception.toString()%>
</div>
</body>
</html>