1+ <%@page contentType =" text/html; charset=UTF-8" import =" java.sql.*" %>
2+ <html >
3+ <head ><title >利用提交条件删除记录页面</title ></head >
4+ <body >
5+ <center >
6+ <%
7+ String driverName = " com.mysql.jdbc.Driver" ;
8+ String userName = " root" ;
9+ String userPwd = " root" ;
10+ String dbName = " students" ;
11+ String url1 = " jdbc:mysql://localhost:3306/" + dbName;
12+ String url2 = " ?user=" + userName + " &password=" + userPwd;
13+ String url3 = " &useUnicode=true&characterEncoding=UTF-8" ;
14+ String url = url1 + url2 + url3;
15+ Class . forName(driverName);
16+ Connection conn = DriverManager . getConnection(url);
17+
18+ request. setCharacterEncoding(" UTF-8" );
19+ String name = request. getParameter(" name" );
20+ String sex = request. getParameter(" sex" );
21+ String ww1 = request. getParameter(" w1" );
22+ String ww2 = request. getParameter(" w2" );
23+ String s = " 1=1 " ;
24+ if (! name. equals(" " )) s = s + " and name='" + name + " ' " ;
25+ if (sex!= null ) s = s + " and sex='" + sex + " ' " ;
26+ double w1,w2;
27+ if (! ww1. equals(" " )) {
28+ w1 = Double . parseDouble(request. getParameter(" w1" ));
29+ s = s + " and weight>=" + w1;
30+ }
31+ if (! ww2. equals(" " )) {
32+ w2 = Double . parseDouble(request. getParameter(" w2" ));
33+ s = s + " and weight<=" + w2;
34+ }
35+ String sql = " delete from stu_info where " + s;
36+ PreparedStatement pstmt = conn. prepareStatement(sql);
37+ // (5)调用PreparedStatement对象中的executeUpdate()方法
38+ int n = pstmt. executeUpdate();
39+ // (6)根据executeUpdate()方法的返回值,判断执行结果
40+ if (n>= 1 ) { % > 数据修改操作成功!<br ><% }
41+ else { % > 数据修改操作失败!<br ><% }
42+
43+ // (7)关闭所有资源
44+ if (pstmt != null ) pstmt. close();
45+ if (conn != null ) conn. close();
46+ % >
47+ </body >
48+ </html >
0 commit comments