address.jsp
上传用户:shjgzm
上传日期:2017-08-31
资源大小:2757k
文件大小:8k
源码类别:

Ajax

开发平台:

Java

  1. <%@ page contentType="text/plain; charset=UTF-8"%>
  2. <%@ page language="java"%>
  3. <%@ page import="java.sql.*,ajax.db.DBUtils,org.json.simple.JSONObject,org.json.simple.JSONArray"%>
  4. <%!
  5.     //根据索引标记获取名片列表
  6.     String getAddressList(String indexKey) {
  7.         JSONArray array = new JSONArray();          //生成一个JSONArray对象
  8.         //根据参数不同选择不同的SQL语句
  9.         String sql = null;
  10.         if ("".equals(indexKey)) {
  11.             //获取全部名片
  12.             sql = "select id, name from address order by name asc";
  13.         } else {
  14.             //获取与索引匹配的名片
  15.             sql = "select id, name from address where firstpy(name) = ? order by name asc";
  16.         }
  17.         Connection conn = null;                     //声明Connection对象
  18.         PreparedStatement pstmt = null;             //声明PreparedStatement对象
  19.         ResultSet rs = null;                        //声明ResultSet对象
  20.         try {
  21.             conn = DBUtils.getConnection();         //获取数据库连接
  22.             pstmt = conn.prepareStatement(sql);     //根据sql创建PreparedStatement
  23.             if (!"".equals(indexKey)) {
  24.                 pstmt.setString(1, indexKey);
  25.             }
  26.             rs = pstmt.executeQuery();
  27.             while (rs.next()) {                     //遍历结果集
  28.                 JSONObject obj = new JSONObject();  //创建一个JSONObject对象
  29.                 obj.put("id", rs.getString(1));
  30.                 obj.put("name", rs.getString(2));
  31.                 array.add(obj);                     //将JSONObject对象放入JSONArray对象中
  32.             }
  33.         } catch (SQLException e) {
  34.             System.out.println(e.toString());
  35.         } finally {
  36.             DBUtils.close(rs);                      //关闭结果集
  37.             DBUtils.close(pstmt);                   //关闭PreparedStatement
  38.             DBUtils.close(conn);                    //关闭连接
  39.         }
  40.         return array.toString();                    //以JSON格式返回信息
  41.     }
  42.     //获取名片详细信息
  43.     String getAddress(String id) {
  44.         JSONObject obj = new JSONObject();      //创建一个JSONObject对象
  45.         String sql = "select id, name, tel, email from address where id = ?";//定义SQL语句
  46.         Connection conn = null;                 //声明Connection对象
  47.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  48.         ResultSet rs = null;                    //声明ResultSet对象
  49.         try {
  50.             conn = DBUtils.getConnection();     //获取数据库连接
  51.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  52.             pstmt.setString(1, id);             //设置参数
  53.             rs = pstmt.executeQuery();
  54.             if (rs.next()) {
  55.                 obj.put("id", rs.getString(1));
  56.                 obj.put("name", rs.getString(2));
  57.                 obj.put("tel", rs.getString(3));
  58.                 obj.put("email", rs.getString(4));
  59.             }
  60.         } catch (SQLException e) {
  61.             System.out.println(e.toString());
  62.         } finally {
  63.             DBUtils.close(rs);                  //关闭结果集
  64.             DBUtils.close(pstmt);               //关闭PreparedStatement
  65.             DBUtils.close(conn);                //关闭连接
  66.         }
  67.         return obj.toString();                  //以JSON格式返回信息
  68.     }
  69.     //添加名片信息
  70.     void addAddress(String name, String tel, String email) {
  71.         String sql = "insert into address(name, tel, email) values(?,?,?)";//定义SQL语句
  72.         Connection conn = null;                 //声明Connection对象
  73.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  74.         try {
  75.             conn = DBUtils.getConnection();     //获取数据库连接
  76.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  77.             pstmt.setString(1, name);
  78.             pstmt.setString(2, tel);
  79.             pstmt.setString(3, email);
  80.             pstmt.executeUpdate();
  81.         } catch (SQLException e) {
  82.             System.out.println(e.toString());
  83.         } finally {
  84.             DBUtils.close(pstmt);               //关闭PreparedStatement
  85.             DBUtils.close(conn);                //关闭连接
  86.         }
  87.     }
  88.     //删除名片信息
  89.     void delAddress(String id) {
  90.         String sql = "delete from address where id = ?";//定义SQL语句
  91.         Connection conn = null;                 //声明Connection对象
  92.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  93.         try {
  94.             conn = DBUtils.getConnection();     //获取数据库连接
  95.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  96.             pstmt.setString(1, id);
  97.             pstmt.executeUpdate();
  98.         } catch (SQLException e) {
  99.             System.out.println(e.toString());
  100.         } finally {
  101.             DBUtils.close(pstmt);               //关闭PreparedStatement
  102.             DBUtils.close(conn);                //关闭连接
  103.         }
  104.     }
  105.     //更新名片信息
  106.     void updateAddress(String id, String name, String tel, String email) {
  107.         String sql = "update address set name = ?, tel = ?, email = ? where id = ?";//定义SQL语句
  108.         Connection conn = null;                 //声明Connection对象
  109.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  110.         try {
  111.             conn = DBUtils.getConnection();     //获取数据库连接
  112.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  113.             pstmt.setString(1, name);
  114.             pstmt.setString(2, tel);
  115.             pstmt.setString(3, email);
  116.             pstmt.setString(4, id);
  117.             pstmt.executeUpdate();
  118.         } catch (SQLException e) {
  119.             System.out.println(e.toString());
  120.         } finally {
  121.             DBUtils.close(pstmt);               //关闭PreparedStatement
  122.             DBUtils.close(conn);                //关闭连接
  123.         }
  124.     }
  125. %>
  126. <%
  127.     out.clear();                                            //清空当前的输出内容(空格和换行符)
  128.     request.setCharacterEncoding("UTF-8");                  //设置请求字符集为UTF-8
  129.     String action = request.getParameter("action");         //获取action参数
  130.     //根据action参数不同执行不同的数据库操作
  131.     if ("getAddressList".equals(action)) {                  //执行根据索引标记获取名片列表操作
  132.         String indexKey = request.getParameter("indexKey");
  133.         out.print(getAddressList(indexKey));
  134.     } else if ("getAddress".equals(action)) {               //执行获取名片详细信息操作
  135.         String id = request.getParameter("id");
  136.         out.print(getAddress(id));
  137.     } else if ("addAddress".equals(action)) {               //执行添加名片信息操作
  138.         String name = request.getParameter("name");
  139.         String tel = request.getParameter("tel");
  140.         String email = request.getParameter("email");
  141.         addAddress(name, tel, email);
  142.         out.print("用户信息保存成功。");
  143.     } else if ("delAddress".equals(action)) {               //执行删除名片信息操作
  144.         String id = request.getParameter("id");
  145.         delAddress(id);
  146.         out.print("用户信息删除成功。");
  147.     } else if ("updateAddress".equals(action)) {            //执行更新名片信息操作
  148.         String id = request.getParameter("id");
  149.         String name = request.getParameter("name");
  150.         String tel = request.getParameter("tel");
  151.         String email = request.getParameter("email");
  152.         updateAddress(id, name, tel, email);
  153.         out.print("用户信息保存成功。");
  154.     }
  155. %>