联系人项目

-- 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>
编辑修改

 

你可能感兴趣的:(联系人项目)