联系人项目

-- Create table
create table CONTACTS
(
  id      NUMBER not null,
  name    VARCHAR2(10) not null,
  tel     VARCHAR2(8) not null,
  groupid NUMBER not null
)
tablespace TEST
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns
comment on column CONTACTS.id
  is ‘联系人唯一标示‘;
comment on column CONTACTS.name
  is ‘联系人姓名‘;
comment on column CONTACTS.tel
  is ‘联系人电话号码‘;
comment on column CONTACTS.groupid
  is ‘分组ID‘;
-- Create/Recreate primary, unique and foreign key constraints
alter table CONTACTS
  add constraint PK_CONTACTS_ID primary key (ID)
  using index
  tablespace TEST
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
alter table CONTACTS
  add constraint FK_CONTACTS_GROUPID foreign key (GROUPID)
  references GROUPS (ID);

表Contacts

-- Create table
create table GROUPS
(
  id   NUMBER not null,
  name VARCHAR2(10) not null
)
tablespace TEST
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns
comment on column GROUPS.id
  is ‘分组ID‘;
comment on column GROUPS.name
  is ‘分组名称‘;
-- Create/Recreate primary, unique and foreign key constraints
alter table GROUPS
  add constraint PK_GROUPS_ID primary key (ID)
  using index
  tablespace TEST
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

表Groups

-- Create sequence
create sequence SQ_CONTACTS_ID
minvalue 1
maxvalue 999999999999999999999999999
start with 41
increment by 1
cache 20;

SQ_CONTACTS_ID

package com.hanqi.dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBHelper {

    public static Connection getConnection() throws Exception

    {
        Class.forName("oracle.jdbc.driver.OracleDriver");

        String url = "jdbc:oracle:thin:@localhost:1521:ORCL" ;

        Connection conn = DriverManager.getConnection(url, "test", "test");

    return conn;

    }
}

DBHelper

package com.hanqi.dao;

public class Contact {

private int ID;
public int getID()
{
    return ID;
}

public void setID(int iD)
{
    ID = iD;
}

private String Name;

public String getName()
{
    return Name;
}

public void setName(String name)
{
    Name = name;
}    

private String Tel;    

public String getTel() {
    return Tel;
}

public void setTel(String tel) {
    Tel = tel;
}

private int GroupID;
public int getGroupID() {
    return GroupID;
}

public void setGroupID(int groupID) {
    GroupID = groupID;
}

private String Groupname;

public String getGroupname() {
    return Groupname;
}

public void setGroupname(String groupname) {
    Groupname = groupname;
}

}

Contact

package com.hanqi.dao;

import java.util.*;

import com.hanqi.dao.DBHelper;

import java.sql.*;

public class ContactDal {

    //增
    public int insert(Contact ct) throws Exception{

        int rtn = -1;
        {
            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, ct.getName());
                pst.setString(2,ct.getTel() );
                pst.setInt(3, ct.getGroupID());

                rtn = pst.executeUpdate();
                }
                catch(Exception e)
                {
                    throw e;
                }
                finally{
                try{
                pst.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                conn.close();
                }

            }
            return rtn;
        }
        }

    //删
        public int delete (String id) throws Exception
        {
            int rtn = -1;

            Connection conn = DBHelper.getConnection();
            PreparedStatement pst = null;
            if(conn != null)
            {
                try{
                String sql = "delete from contacts where id = ? ";

                pst = conn.prepareStatement(sql);

                pst.setString(1, id);

                pst.executeUpdate();
                }
                catch(Exception e)
                {
                    throw e;
                }
                finally
                {
                    try{

                        pst.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    conn.close();
                }
            }
            return rtn;
        }

        //改
        public int update(Contact ct)throws Exception
        {
            int rtn = -1;
            Connection conn = DBHelper.getConnection();

            if(conn != null)
            {
                PreparedStatement ps = null;
                try{
                //操作数据库
                String sql = "update Contacts set name=?,tel=?,groupid=?"+ " where id = ?" ;

                // 执行SQL语句的类
                ps = conn.prepareStatement(sql);

                ps.setString(1, ct.getName());
                ps.setString(2, ct.getTel());
                ps.setInt(3, ct.getGroupID());
                ps.setInt(4, ct.getID());

                ps.executeUpdate();//执行SQL语句并返回数据行数
                }
                catch(Exception e)
                {
                    throw e;
                }
                finally
                {
                    try
                    {
                        ps.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    conn.close();
                }
        }
            return rtn;
        }    

        //查

        public ArrayList<Contact> getListAll() throws Exception
        {
            ArrayList<Contact> al = new ArrayList<Contact>();

            Connection conn = DBHelper.getConnection();

            PreparedStatement ps = null;

            try
            {
                if(conn != null)
                {
                    String sql = "select t.id as i,t.name as n,t.tel as t,t.groupid as g,b.name as m from Contacts t join groups b on t.groupid = b.id ";

                    ps = conn.prepareStatement(sql);

                    ResultSet rs = ps.executeQuery();

                    if(rs != null)
                    {
                        while (rs.next())
                        {
                            Contact ct = new Contact();

                            ct.setID(rs.getInt("i"));
                            ct.setName(rs.getString("n"));
                            ct.setTel(rs.getString("t"));
                            ct.setGroupname(rs.getString("m"));
                            ct.setGroupID(rs.getInt("g"));
                            al.add(ct);                                    

                        }
                    }
                    rs.close();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                try

                {
                    ps.close();
                }
                catch(Exception e)
                {}
                conn.close();
            }
            return al;
        }

}

ContactDal

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 SaveContact
 */
@WebServlet("/SaveContact")
public class SaveContact extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveContact() {
        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 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 ct = new Contact();

                ct.setName(name);
                ct.setTel(tel);
                ct.setGroupID(Integer.parseInt(groupid));

                //调用模型层
                ContactDal cd = new ContactDal();
                try{                    

                cd.insert(ct);

                if(cd.insert(ct) > 0 )
                {
                response.sendRedirect("index.jsp");
                }
                else
                {
                    response.getWriter().append("保存数据失败");
                }

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    response.getWriter().append("发生错误"+ e.getMessage());
                }
            }
                else
                {
                    response.getWriter().append("groupid不能为空");
                }
            }
            else
            {
                response.getWriter().append("tel不能为空");
            }
        }
        else
        {
            response.getWriter().append("name不能为空");
        }

    }

    /**
     * @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);
    }

}

SaveContact

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.ContactDal;;

/**
 * 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 ud = new ContactDal();

            try
            {
                ud.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);
    }

}

DeleteContact

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.Contact;
import com.hanqi.dao.ContactDal;

/**
 * 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 ct = new Contact();

                ct.setID(Integer.parseInt(id));
                ct.setName(name);
                ct.setTel(tel);
                ct.setGroupID(Integer.parseInt(groupid));

                //调用模型层
                ContactDal cd = new ContactDal();
                try{                    

                cd.update(ct);

                if(cd.update(ct) > 0 )
                {
                response.sendRedirect("index.jsp");
                }
                else
                {
                    response.getWriter().append("修改数据失败");
                }

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    response.getWriter().append("发生错误"+ e.getMessage());
                }
            }
                else
                {
                    response.getWriter().append("groupid不能为空");
                }
            }
            else
            {
                response.getWriter().append("tel不能为空");
            }
        }
        else
        {
            response.getWriter().append("name不能为空");
        }

    }

    /**
     * @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);
    }

}

EditContact

<%@ 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>Insert title here</title>

<script type="text/javascript">

function show()
{
    var a = document.getElementById("add")

    if(a.style.display=="none")
    {
        a.style.display="";
    }
    else
    {
        a.style.display="none";
    }

}
function quer()
{
    return confirm("是否删除数据?删除数据会将下级地区全部删除!")
}

</script>

</head>
<body>
<table width="450" border="1">

<tr>
<th>编辑</th>
<th>删除</th>
<th>姓名</th>
<th>电话</th>
<th>分组</th>
</tr>

<%

ContactDal cd = new ContactDal();

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

if(al != null)
{
for(Contact u: al)
{

    out.print("<tr><td> <a href=‘bianji.jsp?id="+u.getID()+"&name="+u.getName()+"&tel="+u.getTel()+" ‘>编辑 </a></td><td><a href=‘DeleteContact?id="+u.getID()+"‘ onclick=‘return quer();‘>删除</a></td><td>"
    +u.getName() + "</td><td>" + u.getTel() +"</td><td>" + u.getGroupname()+ "</td><tr>");
}

}
%>

</table>

<input  type="button" value="添加" width="30" onclick="show()"/>

<form id="add" style="display:none;" method="get" action="SaveContact" >

姓名:<input id="name" type="text" name="name" width=30 ><br>
电话:<input id="tel" type="text" name="tel" width=30 maxlength="8"><br>
分组:
<select id="groupid" name="groupid" onchange="fenzChange()">
<option value="1" selected="selected">同学</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>

联系人首页

<%@ 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>
<%
ContactDal cd = new ContactDal();

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

if(al != null)
{
for(Contact u: al)
{
    int a = u.getGroupID();
%>

<form action="EditContact" method="post">

<input type="hidden" name="id" value="<%=u.getID() %>" /> 

姓名<input type="text" name="name" width="10" value="<%=u.getName() %>" />

电话<input type="text" name="tel" width="15" value="<%= u.getTel() %>" />

<select name="groupid">
分组
<option selected="selected"><%=u.getGroupname() %></option>
<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>

编辑修改

时间: 2024-10-18 08:56:15

联系人项目的相关文章

分享编译Android源码的全过程

通过参考网上的相关资料,我编译Android源码的步骤如下: 1 我的系统是Ubuntu 8.04 2 系统上必须安装以下工具(摘自网上的资料):  sudo apt-get install build-essential sudo apt-get install make  sudo apt-get install gcc  sudo apt-get install g++  sudo apt-get install libc6-dev  sudo apt-get install flex  

Android手机通讯录项目开发--联系人数据库contacts2.db介绍

项目描述:该项目为基于Android平台的手机通讯录,主要模块分为四个部分:联系人管理模块,通话记录管理模块,短信管理模块,系统设置模块. 系统结构图如下: 本项目启动时间:2014年5月28日 说明:本次开发项目的所有源码全部会分享给大家.开发此项目的目的有这几点:一.锻炼独立开发项目的能力,二.增加对Android开发的了解,三.熟悉Android通讯录机制. 闲话不多说,正式开始! 技术要点一:熟悉Android联系人数据库contacts2.db 1.获得联系人数据库contacts2.

XML小项目------dom4j操作联系人(图形界面版)

在进行此项目之前需要将dom4j的jar包及其依赖包导入,具体操作间本博客文章:XML之------dom4j对XML文档增删改查点击打开链接 公共类:DocumentFactory.java package cn.hncu.contact.common; import java.io.FileWriter; import java.io.IOException; import org.dom4j.Document; import org.dom4j.io.SAXReader; import o

[小项目] 获取手机联系人并且向服务器发送JSON数据

[小项目] 获取手机联系人并且向服务器发送JSON数据 好久没有写文档了...最近忙着带班,也没有时间学习新东西,今天刚好有个小Demo,就写了一下,顺便丰富一下我的博客吧! 首先说一下需求: 简单的说,就是一个程序,会获取手机的联系人列表,然后转换成JSON字符串数组,向指定服务器中发送数据...总感觉有侵犯别人隐私权的意味; 注:仅供学习使用,不要做违法的事情哟 这个程序我写的有点有条理,首先有几个工具类: 1. 判断是否联网的工具类(NetUtils) 2. 从手机中获取所有联系人的工具类

Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框

作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]http://jq.qq.com/?_wv=1027&k=XVfBTo 要捐钱的就打支付宝吧:13958686678(泥瓦匠开个玩笑~) 一.前言 继续AndroidUI系列,泥瓦匠又要开始扯淡了.哈哈今天在文章头加了个支付宝账号.我也真逗,至今没收到一笔是写博客的钱.或是分享的.泥瓦匠也就挂着逗逗乐

iOS项目-联系人列表

一,项目介绍 首先,简单介绍一下这个项目的效果 进入程序,首先是登录页面 登录页面用到 NSUserDefault 记住登录密码 然后是 然后是登录跳转,用到MBProgressHUD 接着是联系人列表 是一个UITableView 点击+ 进入添加联系人页面 点击每行联系人 进入编辑界面 点击注销,弹出UIAlertController 二,进入代码 首先 在SB中构建视图的架构 然后建立几个对应的控制器,并做好子类链接 然后实现 登录界面 首先添加观察者,监视textfield的值的变化 [

微项目: 联系人管理——知识与技术总结

1, index页面(带直接显示数据) <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.hanqi.*" %> <%@ page import="com.hanqi.dao.*" %> <%@ pag

23读取联系人移植到项目中

在设置向导的第三个界面,点击"选择联系人按钮"进入选择联系人界面,并且在选择联系人界面传回电话号码到第三个设置向导界面. 监听事件: /** * 选择联系人的按钮的点击事件 * * @param view */ public void selectContact(View view) { Intent intent = new Intent(Setup3Activity.this, SelectContactActivity.class); // 希望返回给本类一个电话号码,所以使用下

XML小项目------DOM操作联系人(非图形界面版)

本例分了两层的模式,即数据层和表现层 公共类:DocumentFactory.java package cn.hncu.contact.common; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.trans