码迷,mamicode.com
首页 > 其他好文 > 详细

联系人管理

时间:2015-12-25 01:05:17      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

Contact

package com.hanqi.dao;

public class Contact {

    //ID
    private int id;
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    
    //分组ID
    private int groupId;
    
    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }
    
    
    //联系人姓名
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    
    //联系人电话
    private String tel;

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    
}

contactsDal

package com.hanqi.dao;

import java.sql.*;
import java.util.*;
import com.hanqi.dao.DBHelper;

public class ContactDal 
{
    //
    public int insert(Contact c) throws Exception
    {
        int rtn = 0;
        
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement pst = null;
        
        if(conn != null)
        {
            try
            {
                String sql = "insert into Contacts (id,name,tel,groupid) values (sq_contacts_id.nextval,?,?,?)";
                
                pst = conn.prepareStatement(sql);
                
                pst.setString(1, c.getName());
                pst.setString(2, c.getTel());
                pst.setInt(3, c.getGroupId());
                
                rtn = pst.executeUpdate();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
            finally
            {
                try
                {
                    pst.close();
                }
                catch(Exception ex)
                {
                    
                }
                
                conn.close();
            }

        }
        return rtn;
    }
    
    //
    public ArrayList<Contact> getListAll() throws Exception
    {
        ArrayList<Contact> al = null;
        
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement pst = null;
        
        ResultSet rs = null;
        
        if(conn != null)
        {
            try
            {
                String sql = "select * from Contacts";
                
                pst = conn.prepareStatement(sql);

                rs = pst.executeQuery();
                
                if (rs != null)
                {
                    al = new ArrayList<Contact>();
                    
                    while(rs.next())
                    {
                        Contact c = new Contact();
                        
                        c.setId(rs.getInt("id"));
                        c.setGroupId(rs.getInt("groupId"));
                        c.setName(rs.getString("name"));
                        c.setTel(rs.getString("tel"));
                        
                        al.add(c);
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
            finally
            {
                try
                {
                    pst.close();
                }
                catch(Exception ex)
                {
                    
                }
                try
                {
                    rs.close();
                }
                catch(Exception ex)
                {
                    
                }
                conn.close();
            }
            
        }
        return al;
    }
    
    
    //
    public int delete(int id) throws Exception
    {
        int rtn=-1;
        
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement pst = null;
        
        if(conn != null)
        {
            try
            {
                String sql = "delete Contacts where id = ?";
                
                pst = conn.prepareStatement(sql);
                
                pst.setInt(1, id);
                
                rtn=pst.executeUpdate();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    pst.close();
                }
                catch(Exception ex)
                {
                    
                }
                conn.close();
            }

        }
        
        return rtn;
        
    }
    
    //
    public int update(Contact c) throws Exception
    {
        int rtn=-1;
        
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement pst = null;
        
        if(conn != null)
        {
            try
            {
                String sql = "update Contacts set name=?,tel=?,groupid=? where id=?";
                
                pst = conn.prepareStatement(sql);
                
                pst.setString(1, c.getName());
                pst.setString(2, c.getTel());
                pst.setInt(3, c.getGroupId());
                pst.setInt(4, c.getId());
                
                rtn=pst.executeUpdate();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
            finally
            {
                try
                {
                    pst.close();
                }
                catch(Exception ex)
                {
                    
                }
                
                conn.close();
            }
            
        }
        
        return rtn;
    }
    
    //单行查询
    
    public Contact getContact(int id)throws Exception
    {
        Contact rtn = null;
        
        //获取
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement ps = null;
        
        ResultSet rs = null;
        
        try
        {
            if(conn != null)
            {    
                String sql = "select * from Contacts where id = ?";
            
                ps = conn.prepareStatement(sql);
                            
                ps.setInt(1, id);
                
                rs = ps.executeQuery();
                
                if(rs != null && rs.next())
                {                                
                        rtn = new Contact();
                    
                        rtn.setId(rs.getInt("id"));
                        rtn.setGroupId(rs.getInt("groupid"));
                        rtn.setName(rs.getString("name"));
                        rtn.setTel(rs.getString("tel"));
                                    
                }
                rs.close();
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            try
            {
                ps.close();
            }
            catch(Exception e)
            {}
            try
            {
                rs.close();
            }
            catch(Exception e)
            {}
                conn.close();
        }
        
        return rtn;
    }
    
    
    
    
    
}

DBHelper

package com.hanqi.dao;

import java.sql.*;

public class DBHelper 
{
    public static Connection getConnection() throws Exception
    {
        //注册数据库驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        
        //连接URL
        String url = "jdbc:oracle:thin:@localhost:1521:ORCL" ;
        
        // 驱动管理器  工厂模式
        Connection conn = DriverManager.getConnection(url, "sa", "12345");
        
        return conn;
    }
}

首页index

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
    <%@ page import="com.hanqi.*" %>
    <%@ page import="com.hanqi.dao.*" %>
<!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 src="js/jquery-1.5.2.min.js"></script>
<script>


function confimDelete()
{
    return confirm("确定要删除该记录么?")
    
    }
    
function show()
{
    var a =document.getElementById("add");
    
    if (a.style.display =="none")
        {
        a.style.display ="";
        }
    else
        {
        a.style.display ="none";
        }
}


</script>
</head>
<body>


<table width="500" border="1">

<tr align="center">
<th></th>
<th><b>姓名</b></th>
<th><b>电话</b></th>
<th><b>分组</b></th>

<%

    ContactDal cd = new ContactDal();

    ArrayList<Contact> al = cd.getListAll();

    if(al != null)
    {
        for(Contact c : al)
        {
            out.print("<tr><td><a href=‘xiugai.jsp?id="+c.getId()+"&name="+c.getName()+"&tel="+c.getTel()
            +" ‘>修改</a>&nbsp;&nbsp;<a href=‘DeleteContact?id="+c.getId()
            +"‘onclick=‘return confimDelete();‘>删除</a></td><td>"+c.getName()+"</td> <td>"+c.getTel()+
            "</td> <td>"+c.getGroupId()+"</td> </tr>");
        }
    }
    else
    {
        out.print("查询数据失败");
    }

%>

</tr>
</table>


<input type="button" value="添加新号码" onclick="show();">

<form id="add" style="display:none;" method="get" action ="InsertContact" onsubmit="return test()" > 


姓名:<input id="name" name="name" type="text" width=30 />
<br>电话:<input id="telp" name="tel" type="text" width=30 maxlength="8" />
<br>分组:<select name="groupid">


<option value="1" >同学</option>
<option value="2" >同事</option>
<option value="3" >家人</option>
<option value="4" >朋友</option>
<option value="5" >其他</option>

</select>
<br><input type="submit" value="添加" />  <input type="reset" value="取消" />

</form>
</body>
</html>

编辑xiugai.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
        <%@ page import="java.util.*" %>
    <%@ page import="com.hanqi.*" %>
    <%@ page import="com.hanqi.dao.*" %>
    <%@ page import="java.sql.*" %>
<!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>
</head>
<body>
<%
String id = request.getParameter("id");

String name = new String(request.getParameter("name"));

name = new String(name.getBytes("ISO-8859-1"),"UTF-8");

String tel = request.getParameter("tel");

%>

<form action="xiugai" method="get">

<input type="hidden" name="id" value="<%=id %>" /> 

姓名:<input type="text" name="name" width="10" value="<%=name %>" />

电话:<input type="text" name="tel" width="15" value="<%=tel %>" />

分组:
<select name="groupid">

<option value="1">同学</option>
<option value="2">同事</option>
<option value="3">家人</option>
<option value="4">朋友</option>
<option value="5">其他</option>
</select>
<br>
<input type="submit" value="提交修改" />

</form>


</body>
</html>

添加

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.dao.*;
/**
 * Servlet implementation class InsertContact
 */
@WebServlet("/InsertContact")
public class InsertContact extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertContact() {
        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.setCharacterEncoding("UTF-8");
        
        response.setContentType("text/html; charset=UTF-8");
        
        String name =  request.getParameter("name");
        
        String tel =  request.getParameter("tel");
        
        String groupid = request.getParameter("groupid");
        
        if(name != null && name.trim().length() > 0)
        {
            //get方式
            name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            
            if(tel !=null && tel.trim().length() >0)
            {
                if(groupid != null && groupid.trim().length()>0)
                {
                    Contact c = new Contact();
                    
                    c.setName(name);
                    c.setGroupId(Integer.parseInt(groupid));
                    c.setTel(tel);
                    
                    ContactDal cd = new ContactDal();
                    
                    try
                    {
                        cd.insert(c);
                        
                        response.sendRedirect("index.jsp");;
                    
                    }
                    catch(Exception e)
                    {
                        response.getWriter().append("发生错误:" + e.getMessage());
                        
                        e.printStackTrace();
                    }
                }
                else
                {
                    response.getWriter().append("分组不能为空");
                }
            }
            else
            {
                response.getWriter().append("电话号码不能为空");
            }
        }
        else
        {
            response.getWriter().append("姓名不能为空");
        }

        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

删除

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.dao.*;

/**
 * Servlet implementation class DeleteContact
 */
@WebServlet("/DeleteContact")
public class DeleteContact extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteContact() {
        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");
        
        if (id != null && id.trim().length()>0)
        {
            
            ContactDal cd = new ContactDal();
            
            try
            {
                int ID = Integer.parseInt(id);
                
                cd.delete(ID);
                
                response.sendRedirect("index.jsp");
            } 
            catch (Exception e) 
            {
                response.getWriter().append("删除数据失败");
                
                e.printStackTrace();
            }
        }

        
        
        
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

修改

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.dao.*;

/**
 * Servlet implementation class EditContact
 */
@WebServlet("/EditContact")
public class EditContact extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditContact() {
        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");
        
        String name =  request.getParameter("name");

        String tel =  request.getParameter("tel");

        String groupid =  request.getParameter("groupid");
        
        if(name != null &&name.trim().length() > 0)
        {
            if(tel != null && tel.trim().length() > 0)
            {            
                if(groupid != null && groupid.trim().length() > 0)
                
                {
                //get方式
                name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
                
                Contact c = new Contact();
                
                c.setId(Integer.parseInt(id));
                c.setName(name);
                c.setTel(tel);
                c.setGroupId(Integer.parseInt(groupid));

                //调用模型层            
                ContactDal cd = new ContactDal();
                try{                    
                    
                cd.update(c);
                
                if(cd.update(c) > 0 )
                {
                response.sendRedirect("index.jsp");
                }
                else
                {
                    response.getWriter().append("修改数据失败");
                }
                
                }
                catch(Exception e) 
                {
                    e.printStackTrace();
                    response.getWriter().append("发生错误"+ e.getMessage());
                }
            }
                else
                {
                    response.getWriter().append("分组不能为空");
                }
            }
            else
            {
                response.getWriter().append("电话号码不能为空");
            }
        }
        else
        {
            response.getWriter().append("姓名不能为空");
        }
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

联系人管理

标签:

原文地址:http://www.cnblogs.com/zxw0004/p/5074720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!