<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.hanqi.*" %>
<!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>
<script type="text/javascript">
function disp_confirm()
{
var r = confirm("确定删除?")
if (r == true)
{
response.sendRedirect("FindList");
}
}
</script>
</head>
<body>
<a href="FindList">显示全部内容:</a>
<table border="1">
<tr>
<th></th>
<th>姓名</th>
<th>电话</th>
<th>分组</th>
</tr>
<%
Object obj = request.getAttribute("userlist");
if(obj != null)
{
ArrayList<Bao> array = (ArrayList<Bao>)obj;
for(Bao u : array)
{
out.print( "<tr><td>"+ "\t <a onclick='return disp_confirm()' href='Delete?id="+u.getId() + "'>删除</a>"
+"</td><td>"+ u.getName()+"</td><td>" + u.getTel() + "</td><td>" + u.getGroupId()+"</td></tr>");
}
}
else
{
out.append("用户列表为空");
}
%>
</table>
<form method="post" action="Baoc" >
<input name="id" type="hidden" value="0" />
<ul>
<li>请输入联系人姓名:<input name="name" type="text" width=30 /></li>
<li>请输入联系人电话号码:<input name="tel" type="text" width=30 /></li>
<li><input type="submit" value="提交" /> <input type="reset" value="取消" /> </li>
</ul>
</form>
</body>
</html>
package com.hanqi.dao;
public class Contact {
private int Id;
private String Name;
private String Tel;
private int GroupId;
private String Group;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getTel() {
return Tel;
}
public void setTel(String tel) {
Tel = tel;
}
public int getGroupId() {
return GroupId;
}
public void setGroupId(int groupId) {
GroupId = groupId;
}
public String getGroup() {
return Group;
}
public void setGroup(String group) {
Group = group;
}
}
package com.hanqi.dao;
import java.sql.*;
import java.util.ArrayList;
import com.hanqi.common.DBHelper;
public class ContactsDal {
//增
public void insert(Contact contact) throws Exception
{
Connection conn = DBHelper.getConnection();
PreparedStatement ps = null;
try
{
if(conn != null)
{
String sql = "insert into t_contacts (id,name,tel,groupid) values (SQ_contactS_ID.nextval,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, contact.getName());
ps.setString(2, contact.getTel());
ps.setInt(3, contact.getGroupId());
ps.executeUpdate();
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
ps.close();
}
catch(Exception e)
{}
conn.close();
}
}
package com.hanqi.dao;
import java.sql.*;
import java.util.ArrayList;
import com.hanqi.common.DBHelper;
public class ContactsDal {
//删
public void deleteById(int id) throws Exception
{
Connection conn = DBHelper.getConnection();
PreparedStatement ps = null;
if(conn != null)
{
try
{
String sql ="delete from t_contacts where id =?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
}
catch(Exception e)
{
throw e;
}
finally
{
try
{
ps.close();
}
catch(Exception e)
{}
conn.close();
}
}
}
//改
public void update(Contact contact) throws Exception
{
Connection conn = DBHelper.getConnection();
PreparedStatement ps = null;
try
{
if(conn != null)
{
String sql = "update t_contacts set name = ?, tel = ?, groupid = ? where id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, contact.getName());
ps.setString(2, contact.getTel());
ps.setInt(3, contact.getGroupId());
ps.setInt(4, contact.getId());
ps.executeUpdate();
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
ps.close();
}
catch(Exception e)
{}
conn.close();
}
}
//查
public ArrayList<Contact> selectAll() throws Exception
{
Connection conn = DBHelper.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Contact> al = new ArrayList<Contact>();
try
{
if(conn != null)
{
String sql = "SELECT c.id as i,c.name as a,c.tel as b,g.name as c from t_contacts c,t_groups g where c.groupid = g.id";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs != null)
{
while(rs.next())
{
Contact c = new Contact();
c.setId(rs.getInt("i"));
c.setName(rs.getString("a"));
c.setTel(rs.getString("b"));
c.setGroup(rs.getString("c"));
al.add(c);
}
}
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
rs.close();
}
catch(Exception e)
{}
try
{
ps.close();
}
catch(Exception e)
{}
conn.close();
}
return al;
}
}
package com.hanqi;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hanqi.*;
/**
* Servlet implementation class Delete
*/
@WebServlet("/Delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Delete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
DAL ud = new DAL();
if(id != null && id.trim().length() > 0)
{
try
{
ud.delete(Integer.parseInt(id));
response.sendRedirect("FindList");
}
catch (Exception e)
{
response.getWriter().append("删除数据失败");
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}