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,'南陽(yáng)');
insert into employee values(null,'王冰','女',23,'北京');
insert into employee values(null,'張風(fēng)','男',23,'信陽(yáng)');
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方法顯示會(huì)在網(wǎng)頁(yè)左下角顯示ID的值
<table border=1 width="800">//表格框線 <TABLE BORDER=?></TABLE>(可以設(shè)定數(shù)值)
<tr><td>請(qǐng)選擇查詢條件</td>//儲(chǔ)存格 <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>代表一個(gè)小方格 <tr>代表一行 <table>是一個(gè)表
<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");//將結(jié)果轉(zhuǎn)為漢字
sql="select * from employee where "+tiaojian+"='"+result+"'";//從數(shù)據(jù)庫(kù)中調(diào)條件和結(jié)果
}
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值;建立一個(gè)連接view.jsp,并由view.jsp從數(shù)據(jù)庫(kù)傳值,而且,將其連接到名字上
</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>//移到刪除上會(huì)顯示ID的值aa=?
</tr>
<%
}
con.close();//關(guān)閉對(duì)數(shù)據(jù)庫(kù)的連接,否則會(huì)使數(shù)據(jù)庫(kù)崩潰
}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>");//將姓名,性別,年齡,地址取回來(lái),并在點(diǎn)姓名時(shí)顯示
}
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刪除一條數(shù)據(jù)
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("刪除成功");//只刪除一條數(shù)據(jù)
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
鵬凌 閱讀(282)
評(píng)論(0) 編輯 收藏