随笔 - 64  文章 - 9  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(6)

我参与的团队

随笔分类(88)

随笔档案(92)

文章分类(142)

文章档案(182)

天基成员

学习园

我的海角

搜索

  •  

积分与排名

  • 积分 - 180563
  • 排名 - 318

最新评论

1.mysql
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,'周口');

2.test.jsp

<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>
<Br>
<form action="" method="post" name="form1">//用post方法显示会在网页左下角显示ID的值
<table border=1 width="800">//表格框线 <TABLE BORDER=?></TABLE>(可以设定数值)
<tr><td>请选择查询条件</td>//储存格 <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>代表一个小方格    <tr>代表一行   <table>是一个表
<td><input type="submit" value="  提   交  "></td>
</tr>
</table>
</form>
<table border=1 width="800">
<tr>
<td>ID</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","yu");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id")%></td>
<td>

<a href="view.jsp?id=<%=rs.getString("id")%>" target=_blank>
<%=rs.getString("name")%></a>  //用?传ID值;建立一个连接view.jsp,并由view.jsp从数据库传值,而且,将其连接到名字上
</td>
<td><%=rs.getString("age")%></td>
<td><%=rs.getString("sex")%></td>
<td><%=rs.getString("address")%></td>
<td><a href="del.jsp?aa=<%=rs.getString("id")%>">删除</a></td>//移到删除上会显示ID的值aa=?
</tr>
<%
 }
con.close();//关闭对数据库的连接,否则会使数据库崩溃
}catch(Exception e){e.printStackTrace();}
%>
</table>
</center>
</body>
</html>

3.view.jsp

<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>

<font size="15">
<%
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","yu");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
 out.print(rs.getString("name")+"<br>"+rs.getString("sex")+"<br>"+rs.getString("age")+"<br>"+rs.getString("address")+"<br>");//将姓名,性别,年龄,地址取回来,并在点姓名时显示
 }
con.close();
}catch(Exception e){e.printStackTrace();}
%>

</center>
</body>
</html>

4.del.jsp
<%@ page contentType="text/html;charset=gbk" import="java.sql.*"%>
<html>
<body>
<center>
<font size="20">
<%
Connection con = null;
Statement stmt= null;
String sql="";
String id= request.getParameter("aa");
sql ="delete from employee where id='"+id+"'";//用ID作为主键,并通过sql删除一条数据
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oa","root","yu");
stmt = con.createStatement();
int k=stmt.executeUpdate(sql);
if(k==1)out.print("删除成功");//只删除一条数据
else out.print("删除失败");
con.close();
}catch(Exception e){e.printStackTrace();}
%>
<br><a href="test.jsp">返回</a>
</center>
</body>
</html>
posted on 2009-04-21 11:27 鹏凌 阅读(281) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: