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

20160108--搜索查询

时间:2016-01-08 22:10:43      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
package com.hanqi.dao;

import java.sql.*;
import java.util.*;

import com.hanqi.dao.DBHelper;

public class ZhiWeiDAL {

    /**
     * 插入数据的方法
     * @param m
     * @return
     * @throws Exception
     */
    public int insert(ZhiWei m) throws Exception
    {
        int rtn = -1;
        
        Connection conn = DBHelper.getConnection();
        PreparedStatement pst = null;
        
        if (conn != null)
        {
            try
            {
            /*String sql = "insert into T_ZHIWEI (id, zhcid, zhiw, leib,diq,rensh,"
                    + "yuex,jiesh,zhuany,xuel,jingy,sex,minage,maxage,lianxr,lianxtel,fbdate,shanch" +
                    "email,fbdate,shanch,shenh) " +
            "values (SQ_T_ZHIWEI_ID.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate,‘0‘,‘1‘) ";
            
            pst = conn.prepareStatement(sql);
            
            pst.setInt(1, m.getZhcid());
            pst.setString(2, m.getZhiw());
            pst.setString(3, m.getLeib());
            pst.setString(4, m.getDiq());
            pst.setString(5, m.getRensh());
            pst.setString(6, m.getYuex());
            pst.setString(7, m.getJiesh());
            pst.setString(8, m.getZhuany());
            pst.setString(9, m.getXuel());
            pst.setString(10, m.getJingy());
            pst.setString(11, m.getSex());
            pst.setInt(12, m.getMinage());
            pst.setInt(13, m.getMaxage());
            pst.setString(14, m.getLianxr());
            pst.setString(15, m.getLianxtel());
            pst.setString(16, m.getEmail());*/
                
                String sql = "insert into T_ZHIWEI (id, zhcid, zhiw, leib,fbdate,shanch,shenh) " +
                "values (SQ_T_ZHIWEI_ID.nextval,?,?,?,sysdate,‘0‘,‘1‘) ";
                
                pst = conn.prepareStatement(sql);
                
                pst.setInt(1, m.getZhcid());
                pst.setString(2, m.getZhiw());
                pst.setString(3, m.getLeib());
                
            
            rtn = pst.executeUpdate();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
            try
            {
            pst.close();
            }
            catch(Exception ex){}
            
            conn.close();
            }
        }
        
        return rtn;
        
    }
    
    //
    public int delete(int id) throws Exception
    {
        int rtn = -1;
        
        Connection conn = DBHelper.getConnection();
        
        PreparedStatement pst = null;
        
        if (conn != null)
        {
            try
            {
            String sql = "update T_ZHIWEI set shanch = ‘1‘ 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(ZhiWei m) throws Exception
    {
        int rtn = -1;
        
        Connection conn = DBHelper.getConnection();
        PreparedStatement pst = null;
        
        if (conn != null)
        {
            try
            {
            String sql = "update T_ZHIWEI set zhiw = ?, leib = ? where id = ? ";
            
            pst = conn.prepareStatement(sql);
            
            pst.setString(1, m.getZhiw());
            pst.setString(2, m.getLeib());
            pst.setInt(3, m.getId());
            
            rtn = pst.executeUpdate();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
            try
            {
            pst.close();
            }
            catch(Exception ex){}
            
            conn.close();
            }
        }
        
        return rtn;
        
    }
    
    public ArrayList<ZhiWei> getList(int qyid, int pagenum, int pagerows, Map<String, String> search) throws Exception
    {
        ArrayList<ZhiWei> rtn = null;
        

        Connection conn = DBHelper.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        
        if (conn != null)
        {
            try
            {
                int maxrows = pagenum * pagerows;
                
                //遍历Map
                String sq = "";
                
                Set<String> k = search.keySet();
                
                Iterator<String> ki = k.iterator();//迭代器
                
                while(ki.hasNext())
                {
                    String key = ki.next();
                    
                    String value = search.get(key);
                    
                    //组合SQL
                    switch(key)
                    {
                    case "id":
                        
                        if (value != null && value.length() > 0)
                        {
                            sq += " and id = " + value + " "; 
                        }                        
                        
                        break;
                        
                    case "zhiw":
                        
                        if (value != null && value.length() > 0)
                        {
                            sq += " and zhiw like ‘%" + value + "%‘ "; 
                        }                        
                        
                        break;
                    }
                }
                
                //找sort
                String sort = search.get("sort");
                
                if (sort != null)
                {
                    sq += " order by " + sort + " " + search.get("order");
                }
                
                
                
            String sql = "select * from (select t.*, rownum as rnum from (select * from T_ZHIWEI where zhcid = ? and shanch != ‘1‘ " +
            sq + ") t where rownum <= ?) where rnum > ?";
            
            pst = conn.prepareStatement(sql);

            pst.setInt(1, qyid); 
            pst.setInt(2, maxrows); 
            pst.setInt(3, maxrows - pagerows);
                        
            rs = pst.executeQuery();
            
            if (rs != null)
            {
                rtn = new ArrayList<ZhiWei>();
                
                while(rs.next())
                {
                    ZhiWei u = new ZhiWei();
                    
                    u.setId(rs.getInt("id"));
                    u.setZhcid(rs.getInt("zhcid"));
                    u.setZhiw(rs.getString("zhiw"));
                    u.setLeib(rs.getString("leib"));
                    u.setDiq(rs.getString("diq"));
                    u.setRensh(rs.getString("rensh"));
                    u.setYuex(rs.getString("yuex"));
                    u.setJiesh(rs.getString("jiesh"));
                    u.setZhuany(rs.getString("zhuany"));
                    u.setXuel(rs.getString("xuel"));
                    u.setJingy(rs.getString("jingy"));
                    u.setSex(rs.getString("sex"));
                    u.setMinage(rs.getInt("minage"));
                    u.setMaxage(rs.getInt("maxage"));
                    u.setLianxr(rs.getString("lianxr"));
                    u.setLianxtel(rs.getString("lianxtel"));
                    u.setEmail(rs.getString("email"));
                    u.setFbdate(rs.getString("fbdate"));
                    u.setShanch(rs.getString("shanch"));
                    u.setShenh(rs.getString("shenh"));
                    
                    rtn.add(u);
                }
            }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                rs.close();
                }
                catch(Exception ex){}
            try
            {
            pst.close();
            }
            catch(Exception ex){}
            
            conn.close();
            }
        }
        
        return rtn;
    }
    
    public ArrayList<ZhiWei> getList(int qyid, int pagenum, int pagerows) throws Exception
    {
        ArrayList<ZhiWei> rtn = null;
        

        Connection conn = DBHelper.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        
        if (conn != null)
        {
            try
            {
                int maxrows = pagenum * pagerows;
                
                
            String sql = 
            "select * from (select t.*, rownum as rnum from "
            + "(select * from T_ZHIWEI where zhcid = ? and shanch != ‘1‘ order by id desc) t "
            + "where rownum <= ?) where rnum > ?";
            
            pst = conn.prepareStatement(sql);

            pst.setInt(1, qyid); 
            pst.setInt(2, maxrows); 
            pst.setInt(3, maxrows - pagerows);
                        
            rs = pst.executeQuery();
            
            if (rs != null)
            {
                rtn = new ArrayList<ZhiWei>();
                
                while(rs.next())
                {
                    ZhiWei u = new ZhiWei();
                    
                    u.setId(rs.getInt("id"));
                    u.setZhcid(rs.getInt("zhcid"));
                    u.setZhiw(rs.getString("zhiw"));
                    u.setLeib(rs.getString("leib"));
                    u.setDiq(rs.getString("diq"));
                    u.setRensh(rs.getString("rensh"));
                    u.setYuex(rs.getString("yuex"));
                    u.setJiesh(rs.getString("jiesh"));
                    u.setZhuany(rs.getString("zhuany"));
                    u.setXuel(rs.getString("xuel"));
                    u.setJingy(rs.getString("jingy"));
                    u.setSex(rs.getString("sex"));
                    u.setMinage(rs.getInt("minage"));
                    u.setMaxage(rs.getInt("maxage"));
                    u.setLianxr(rs.getString("lianxr"));
                    u.setLianxtel(rs.getString("lianxtel"));
                    u.setEmail(rs.getString("email"));
                    u.setFbdate(rs.getString("fbdate"));
                    u.setShanch(rs.getString("shanch"));
                    u.setShenh(rs.getString("shenh"));
                    
                    rtn.add(u);
                }
            }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                rs.close();
                }
                catch(Exception ex){}
            try
            {
            pst.close();
            }
            catch(Exception ex){}
            
            conn.close();
            }
        }
        
        return rtn;
    }
    
    //记录条数查询    
    public int getCount(int qyid) throws Exception
    {
        int rtn = 0;
        
        //获取


        Connection conn = DBHelper.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        
        if (conn != null)
        {
            try
            {
            String sql = "select count(1) as c from T_ZHIWEI where zhcid = ?";
            
            pst = conn.prepareStatement(sql);
            
            pst.setInt(1, qyid);
                        
            rs = pst.executeQuery();
            
            if (rs != null && rs.next())
            {
                rtn = rs.getInt("c");                    
            }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                rs.close();
                }
                catch(Exception ex){}
                
            try
            {
            pst.close();
            }
            catch(Exception ex){}
            
            conn.close();
            }
        }
        
        
        return rtn;
    }

}
DAL
技术分享
package com.hanqi.dao;

public class ZhiWei {
    

    private int id;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getZhcid() {
        return zhcid;
    }
    public void setZhcid(int zhcid) {
        this.zhcid = zhcid;
    }
    public String getZhiw() {
        return zhiw;
    }
    public void setZhiw(String zhiw) {
        this.zhiw = zhiw;
    }
    public String getLeib() {
        return leib;
    }
    public void setLeib(String leib) {
        this.leib = leib;
    }
    public String getDiq() {
        return diq;
    }
    public void setDiq(String diq) {
        this.diq = diq;
    }
    public String getRensh() {
        return rensh;
    }
    public void setRensh(String rensh) {
        this.rensh = rensh;
    }
    public String getYuex() {
        return yuex;
    }
    public void setYuex(String yuex) {
        this.yuex = yuex;
    }
    public String getJiesh() {
        return jiesh;
    }
    public void setJiesh(String jiesh) {
        this.jiesh = jiesh;
    }
    public String getZhuany() {
        return zhuany;
    }
    public void setZhuany(String zhuany) {
        this.zhuany = zhuany;
    }
    public String getXuel() {
        return xuel;
    }
    public void setXuel(String xuel) {
        this.xuel = xuel;
    }
    public String getJingy() {
        return jingy;
    }
    public void setJingy(String jingy) {
        this.jingy = jingy;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getMinage() {
        return minage;
    }
    public void setMinage(int minage) {
        this.minage = minage;
    }
    public int getMaxage() {
        return maxage;
    }
    public void setMaxage(int maxage) {
        this.maxage = maxage;
    }
    public String getLianxr() {
        return lianxr;
    }
    public void setLianxr(String lianxr) {
        this.lianxr = lianxr;
    }
    public String getLianxtel() {
        return lianxtel;
    }
    public void setLianxtel(String lianxtel) {
        this.lianxtel = lianxtel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getFbdate() {
        return fbdate;
    }
    public void setFbdate(String fbdate) {
        this.fbdate = fbdate;
    }
    public String getShanch() {
        return shanch;
    }
    public void setShanch(String shanch) {
        this.shanch = shanch;
    }
    public String getShenh() {
        return shenh;
    }
    public void setShenh(String shenh) {
        this.shenh = shenh;
    }
    private int zhcid;
    private String zhiw;
    private String leib;
    private String diq;
    private String rensh;
    private String yuex;
    private String jiesh;
    private String zhuany;
    private String xuel;
    private String jingy;
    private String sex;
    private int minage;
    private int maxage;
    private String lianxr;
    private String lianxtel;
    private String email;
    private String fbdate;
    private String shanch;
    private String shenh;

}
zhiwei
技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<%
//从session获取
String qyid = "1";

%>

<script type="text/javascript" src="js/jquery-easyui-1.4.4/jquery.min.js"></script>
 <link rel="stylesheet" type="text/css"  href="js/jquery-easyui-1.4.4/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"  href="js/jquery-easyui-1.4.4/themes/icon.css">
<script type="text/javascript" src="js/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>

<script type="text/javascript" src="js/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>

<script >

$(function()
        {
    

    $("#mydg").datagrid({
        fit:true,
        title:‘职位列表‘,
        collapsible:true,
        width:500,
        url:‘ZhiWeiList?qyid=<%=qyid%>‘,
        fitColumns:true,
        striped:true,
        idField:‘id‘,
        pagination:true,
        rownumbers:true,
        pageList:[5,10,50],
        pageSize:5,
        sortName:‘id‘,
        sortOrder:‘desc‘,
        multiSort:true,
        remoteSort:false,
        frozenColumns:[[
                        {field:‘‘,checkbox:true},
                  {field:‘id‘,width:80,title:‘ID‘}]],
        columns:[[
                  {field:‘zhcid‘,sortable:true, width:80,title:‘企业ID‘},
                  {field:‘zhiw‘,sortable:true, width:80,title:‘职位名称‘},
                  {field:‘leib‘,width:180,title:‘职位类别‘}
                  ]],
                  toolbar: [{
                      text:‘增加职位‘,
                          iconCls: ‘icon-add‘,
                          handler: function(){

                              $("#addform").form(‘reset‘);
                              
                              //清理id
                              $("#id").val(null);
                              
                              $("#add").dialog({
                                  title:‘增加职位‘
                              });

                              $("#add").dialog(‘open‘);
                          
                          }
                      },‘-‘,{
                          text:‘编辑职位‘,
                      iconCls: ‘icon-edit‘,
                      handler: function(){

                          var s = $("#mydg").datagrid(‘getSelected‘);
                          
                          if(s != null)
                              {
                              //编辑数据

                              $("#addform").form(‘reset‘);
                              

                              $("#add").dialog({
                                  title:‘编辑职位‘
                              });
                              
                              //绑定数据
                              $("#addform").form(‘load‘, s);

                              $("#add").dialog(‘open‘);
                              
                              }
                          else
                              {
                              alert(‘请选择一条数据‘);
                              }
                      
                      }
                  },‘-‘,{
                      text:‘删除职位‘,
                      iconCls: ‘icon-remove‘,
                      handler: function(){

                          var s = $("#mydg").datagrid(‘getSelections‘);
                          
                          if(s.length > 0)
                              {
                              
                              $.messager.confirm(‘确认对话框‘, ‘您确实要删除吗?‘, function(r){
                                  
                                  if (r){                                      
                                      
                                      var ids = ‘‘;
                                      
                                      for (var i = 0; i < s.length; i++)
                                          {
                                          
                                          ids += s[i].id;
                                          
                                          if (i != (s.length - 1))
                                              {
                                              ids += ‘,‘;
                                              }                                  
                                          }                                     

                                  //alert(‘ids = ‘ + ids);
                                  
                                   $.get("DeleteZhiWei?ids=" + ids,function(data,status){
                                       
                                                                      
                                     // alert("Data: " + data + "\nStatus: " + status);
                                     

                                      var mes = eval(‘(‘ + data + ‘)‘);
                                      

                                      $.messager.show({title:‘提交信息‘, msg:mes.message});
                                      

                                    $("#mydg").datagrid(‘reload‘);
                                    

                                    //清除选择项
                                    $("#mydg").datagrid(‘clearSelections‘);
                                      
                                      
                                    });

                                  }
                              });

                              }
                          else
                              {
                              alert(‘请选择一条数据‘);
                              }                      
                          
                          }
                  }]

    });


    $("#addform").form({
        novalidate:false,
        url:‘ZhiWeiSubmit‘,
        onSubmit:function()
        {
            
            var isValid = $(this).form(‘validate‘);
            
            if (!isValid)
                {
            $.messager.show({title:‘表单验证‘, msg:‘验证未通过‘});
                }            
            
            return isValid;
        },success:function(data){    
            
            
            var mes = eval(‘(‘ + data + ‘)‘);
            

            $.messager.show({title:‘提交信息‘, msg:mes.message});
            
            if (mes.success)
                {
                    $("#add").dialog(‘close‘);
                    
                    $("#mydg").datagrid(‘reload‘);
                }
            else
                {
                
                }
        }           
    })
    
    $("#subbutton").click(function(){
        
        $("#addform").form(‘submit‘);
        
    });
    

    
/*    
    $("#searchbutton").click(function(){
        
        var f = {};
        
        var fields = $("#searchform").serializeArray();

         jQuery.each( fields, function(i, field){
              
             f[field.name] = field.value;
            });
        
        
        $(‘#mydg‘).datagrid(‘load‘,f);
        
    });
    
    */
    

    //清空
    $("#clearbutton").click(function(){
        
        $("#searchform").form(‘clear‘);
        
        $(‘#mydg‘).datagrid(‘load‘,{});
    
    }
        );
    
    
    //提交查询

    
    $("#searchbutton").click(function(){
        
        //把form转成JSON
        var f = {};//JSON字符串
        
        var fields = $("#searchform").serializeArray();//把form序列化成数组

         jQuery.each( fields, function(i, field){
              
             f[field.name] = field.value;
             
            });
        
        
        
        
        $(‘#mydg‘).datagrid(‘load‘,{
            id: $("#id").val(),
            zhiw: $("#zhiw").val()
        });
        
    });
})
</script>
</head>
<body>

<div id="cc" class="easyui-layout" style="width:600px;height:400px;">   
    <div data-options="region:‘north‘,title:‘查询条件‘,split:true" style="height:100px;">
   
<form id="searchform" method="post" >
   
    职位ID:<input id=id name=id class="easyui-numberbox" style="width:60px">
    职位名称:<input id=zhiw name=zhiw class="easyui-textbox" style="width:100px">
    
    
<input name="qyid" type="hidden" value="<%=qyid%>" />
    
<a id="searchbutton" class="easyui-linkbutton" style="width:80px" >查询</a>
    
<a id="clearbutton" class="easyui-linkbutton" style="width:80px" >清空</a>
    </form>
    </div>
    
    <div data-options="region:‘center‘" style="background:#eee;">
       
    
<table id="mydg"></table>
</div>   

<div id="add" class="easyui-dialog" data-options="closed:true" title="增加地区" style="width:300px;">

<form id="addform" method="post" >

<table >
<tr>
<td>职位名称:</td>
<td><input id="zhiw"  name="zhiw" class="easyui-textbox" data-options="required:true,
validType:‘length[2,10]‘,missingMessage:‘这是职位名称,为必填项‘" width="30" ></td>
</tr>
<tr>
<td>职位类别:</td>
<td><input id="leib"  name="leib" class="easyui-textbox" data-options="required:true,
validType:‘length[2,25]‘" width="30" ></td>
</tr>

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


<tr>
<td colspan=2 align="center">

<a id="subbutton" class="easyui-linkbutton" style="width:80px" >提交</a>
</td>
</tr>
</table>

</form>

</div>

</body>
</html>
zhiwei.jsp

 

20160108--搜索查询

标签:

原文地址:http://www.cnblogs.com/name-hanlin/p/5114707.html

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