create database oa;
use oa;
create table employee(
id int primary key auto_increment,
name varchar(20),
sex varchar(2),
age int,
address varchar(50)
);
insert into employee values(null,'张冰','女',23,'河南南洋');
insert into employee values(null,'王冰','女',23,'北京');
insert into employee values(null,'张风','男',23,'河南南');
insert into employee values(null,'李洋','女',33,'河南南');
test.jsp
<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>
<Br>
<form action="" method="post" name="form1">
<table border=1 width="800">
<tr><td>请选择查询条件</td>
<td><select name="tiaojian">
<option value="name"> 姓 名 </option>
<option value="sex"> 性 别</option>
<option value="age"> 年 龄 </option>
</select></td>
<td><input type="text" name="result"></td>
<td><input type="submit" value=" 提 交 "></td>
</tr>
</table>
</form>
<table border=1 width="800">
<tr>
<td>编号</td>
<td>姓名</td><td>年龄</td>
<td>性别</td><td>地址</td>
<td>编辑</td>
</tr>
<%
Connection con = null;
Statement stmt= null;
ResultSet rs =null;
String sql="";
sql ="select * from employee";
String tiaojian=request.getParameter("tiaojian");
String result = request.getParameter("result");
if(tiaojian!=null&&result!=null){
result = new String(result.getBytes("iso-8859-1"),"gbk");
sql="select * from employee where "+tiaojian+"='"+result+"'";
}
out.print(sql);
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("age")%></td>
<td><%=rs.getString("sex")%></td>
<td><%=rs.getString("address")%></td>
<td><a href="edit.jsp?id=<%=rs.getString("id")%>" target="_blank">编辑</td>
</tr>
<%
}
con.close();
}catch(Exception e){e.printStackTrace();}
%>
</table>
</center>
</body>
</html>
edit.jsp
<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String sql="";
String id = request.getParameter("id");
sql="select * from employee where id='"+id+"'";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){%>
<form name="form1" method="post" action="doedit.jsp">
<input type="hidden" name="id" value="<%=rs.getString("id")%>">
姓名:<input type="text" name="username" value="<%=rs.getString("name")%>" readonly><br>
年龄:<input type="text" name="age" value="<%=rs.getString("age")%>"><br>
性别:<input type="text" name="sex" value="<%=rs.getString("sex")%>"><br>
地址:<input type="text" name="address" value="<%=rs.getString("address")%>"><br><br>
<input type="submit" value=" 修 改 ">
</form>
</center>
<%}
con.close();
}catch(Exception e){e.printStackTrace();}
%>
</body>
</html>
doedit.jsp
<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>
<%
Connection con = null;
Statement stmt = null;
String sql="";
String name= request.getParameter("username");
name=new String(name.getBytes("iso-8859-1"),"gbk");
String age=request.getParameter("age");
String sex=request.getParameter("sex");
sex=new String(sex.getBytes("iso-8859-1"),"gbk");
String address=request.getParameter("address");
address=new String(address.getBytes("iso-8859-1"),"gbk");
String id = request.getParameter("id");
sql="update employee set name='"+
name+"',age='"+age+"',sex='"+sex+"',address='"+ address+"' where id='"+id+"'";
out.print(sql);
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","");
stmt = con.createStatement();
int k = stmt.executeUpdate(sql);
if(k==1)out.print("修改成功");
else out.print("修改失败");
con.close();
}catch(Exception e){e.printStackTrace();}
%>
<a href="test.jsp">返回</a>
</body>
</html>
posted on 2009-04-16 15:19
鹏凌 阅读(191)
评论(0) 编辑 收藏