<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询图书页面</title>
</head>
<body>
请选择修改条件<hr width="100%" size="3">
<form action="b_update1.jsp" method="post">
图书名:<p>
<input type="text" name="name" ></p>
作者:<p>
<input type="text" name="editor" ></p>
<input type="submit" value="提交">
<input type="reset" value="取消">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改编辑页面</title>
</head>
<body>
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPwd="123456";//密码
String dbName="practice";//数据库名
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;//形成带数据库读写编码的数据库连接字
Class.forName(driverName);//加载并注册驱动程序
Connection conn=DriverManager.getConnection(url);//获取数据库连接对象
Statement stmt=conn.createStatement();//创建对象
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String editor=request.getParameter("editor");
session.setAttribute(name,"name");
session.setAttribute(editor,"editor");
String sql="select* from book where name=? and editor=? ";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,editor);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
int number=rs.getInt("number");
String name2=rs.getString("name") ;
String editor2=rs.getString("editor");
double price=rs.getDouble("price");
String remarks=rs.getString("remarks");
if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
%>
<form action="b_update2.jsp" method="post">
<table border="0" width="238" height="252">
<tr><td>图书号</td><td><input name="number" value=<%=number %>></td></tr>
<tr><td>图书名</td><td><input name="name2" value=<%=name2 %>></td></tr>
<tr><td>作者</td><td><input name="editor2" value=<%=editor2 %>></td></tr>
<tr><td>价格</td><td><input name="price" value=<%=price %>></td></tr>
<tr><td>备注字段</td><td><input name="remarks" value=<%=remarks %>></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交">
<input type="reset" value="取消">
</table>
</form>
<%}
else{%>
没有找到合适条件的记录!!<%
if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
}%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改后的重写页面</title>
</head>
<body>
<%
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="root";//数据库用户名
String userPwd="123456";//密码
String dbName="practice";//数据库名
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;//形成带数据库读写编码的数据库连接字
Class.forName(driverName);//加载并注册驱动程序
Connection conn=DriverManager.getConnection(url);//获取数据库连接对象
Statement stmt=conn.createStatement();//创建对象
String sql="update book set number=?,name=?,editor=?,price=?,remarks=? where name=? and editor=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
request.setCharacterEncoding("UTF-8");
int number=Integer.parseInt(request.getParameter("number"));
String name2=request.getParameter("name2");
String editor2=request.getParameter("editor2");
double price=Double.parseDouble(request.getParameter("price"));
String remarks=request.getParameter("remarks");
String name=(String)session.getAttribute("name");
String editor=(String)session.getAttribute("editor");
pstmt.setInt(1, number);
pstmt.setString(2, name2);
pstmt.setString(3, editor2);
pstmt.setDouble(4, price);
pstmt.setString(5, remarks);
pstmt.setString(6, name);
pstmt.setString(7, editor);
int n=pstmt.executeUpdate();
if(n>=1){%>重写数据操作成功!<br><%}
else{%>重写数据操作失败!<%=n %><br><%}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
%>
</body>
</html>