JAVA 中SQL字符动态拼接
select SYR,SFZMHM,CJRZH,XSZBH,HPHM,CLSBDH,FDJH,CLLX,ZDYZT,to_char(CCDJRQ,'YYYY-MM-DD') CCDJRQ from VEH_DISABLED where CCDJRQ between to_date('2013-10-15','yyyy-MM-dd')and to_date('2015-10-15','yyyy-MM-dd') order by CCDJRQ desc
Service里的查询:
public List getAdvancedListService(Map map){
StringBuilder sqlCondition = new StringBuilder();
Set keys = map.keySet();
if (keys.contains("SYR")) {
if (map.get("SYR") != null && !"".equals(map.get("SYR").toString().trim()) && !"null".equals(map.get("SYR"))) {
sqlCondition.append("and SYR like '%" + map.get("SYR") + "%'");
}
}
if (keys.contains("SFZMHM")) {
if (map.get("SFZMHM") != null && !"".equals(map.get("SFZMHM").toString().trim()) && !"null".equals(map.get("SFZMHM"))) {
sqlCondition.append("and SFZMHM = '" + map.get("SFZMHM") + "'");
}
}
if (keys.contains("CJRZH")) {
if (map.get("CJRZH") != null && !"".equals(map.get("CJRZH").toString().trim()) && !"null".equals(map.get("CJRZH"))) {
sqlCondition.append("and CJRZH like '%" + map.get("CJRZH") + "%' ");
}
}
if (keys.contains("XSZBH")) {
if (map.get("XSZBH") != null && !"".equals(map.get("XSZBH").toString().trim()) && !"null".equals(map.get("XSZBH").toString().trim())) {
sqlCondition.append("and XSZBH like '%" + map.get("XSZBH") + "%' ");
}
}
if (keys.contains("CLSBDH")) {
if (map.get("CLSBDH") != null && !"".equals(map.get("CLSBDH").toString().trim()) && !"null".equals(map.get("CLSBDH").toString().trim())) {
sqlCondition.append("and CLSBDH = '" + map.get("CLSBDH") + "' ");
}
}
if (keys.contains("FDJH")) {
if (map.get("FDJH") != null && !"".equals(map.get("FDJH").toString().trim()) && !"null".equals(map.get("FDJH").toString().trim())) {
sqlCondition.append("and FDJH = '" + map.get("FDJH") + "' ");
}
}
if (keys.contains("CLLX")) {
if (map.get("CLLX") != null && !"".equals(map.get("CLLX").toString().trim()) && !"null".equals(map.get("CLLX").toString().trim())) {
sqlCondition.append("and CLLX = '" + map.get("CLLX") + "' ");
}
}
if (keys.contains("HPHM")) {
if (map.get("HPHM") != null && !"".equals(map.get("HPHM").toString().trim()) && !"null".equals(map.get("HPHM"))) {
sqlCondition.append("and HPHM = '" + map.get("HPHM") + "' ");
}
}
if (keys.contains("loginBeginTime") && keys.contains("loginEndTime")){
sqlCondition.append("and CCDJRQ between to_date('").append(map.get("loginBeginTime")).append("','yyyy-MM-dd')and to_date('").append(map.get("loginEndTime")).append("','yyyy-MM-dd')");
}
if(sqlCondition.length() > 3){
return disabledvehDao.getAdvancedList(sqlCondition.substring(3));
}
return disabledvehDao.getAdvancedList(sqlCondition.toString());
}
Dao类:
/**
* 高级查询
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public List getAdvancedList(String condition){
String sql;
if("".equals(condition) || "null".equals(condition) || condition == null){
sql ="select d.SYR,d.SFZMHM,d.CJRZH,d.XSZBH,d.HPHM,d.CLSBDH,d.FDJH,d.CLLX,d.ZDYZT,to_char(d.CCDJRQ,'YYYY-MM-DD') CCDJRQ"+
" from VEH_DISABLED d order by d.CCDJRQ desc";
}else{
sql = "select d.SYR,d.SFZMHM,d.CJRZH,d.XSZBH,d.HPHM,d.CLSBDH,d.FDJH,d.CLLX,d.ZDYZT,to_char(d.CCDJRQ,'YYYY-MM-DD') CCDJRQ"+
" from VEH_DISABLED d where "+condition+" order by d.CCDJRQ desc";
}
return jdbcTemplate.queryForList(sql);
}
provider类:
package com.suntek.disabledvehmgr.web.disabledveh;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.suntek.ccf.web.service.ServiceInvocationException;
import com.suntek.eap.core.app.AppHandle;
import com.suntek.disabledvehmgr.dao.base.BaseGrid;
import com.suntek.disabledvehmgr.log.DisabledvehMgrLogger;
import com.suntek.disabledvehmgr.service.disabledveh.DisabledvehService;
import com.suntek.disabledvehmgr.util.CommonUtil;
import com.suntek.disabledvehmgr.util.Constants;
import com.suntek.disabledvehmgr.util.SpringContextUtils;
/**
* 残疾车业务provider类
* @author sawshaw
*
*/
public class AdvancedQueryProvider extends BaseGrid{
@SuppressWarnings("unused")
private AppHandle appHandle;
private HttpServletRequest request;
private DisabledvehService disabledvehService;
@Override
public void after(HttpServletRequest arg0)
throws ServiceInvocationException {
}
@Override
public void before(HttpServletRequest request)
throws ServiceInvocationException {
this.request = request;
this.appHandle = AppHandle.getHandle(Constants.APP_NAME);
this.disabledvehService = (DisabledvehService)
SpringContextUtils.getBean("disabledvehService",request);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public int directCount(Map map) {
try{
if(!CommonUtil.isRequestLegal(request)){
datas = new ArrayList<Map>();
return datas.size();
}
if("firstLoad".equals(CommonUtil.getKeyValue(map, "opera_type"))){
return 0;
}
if (map != null) {
datas = disabledvehService.getAdvancedListService(map);
}
if(datas == null){
datas = new ArrayList<Map>();
}
}catch(Exception e){
DisabledvehMgrLogger.logger.error("[AdvancedQueryProvider]directCount,查询异常,", e);
datas = new ArrayList<Map>();
}
return datas.size();
}
@SuppressWarnings("rawtypes")
@Override
public List<Map> directFetch(Map map, int pageNo, int pageSize) {
if("firstLoad".equals(CommonUtil.getKeyValue(map, "opera_type"))){
return new ArrayList<Map>();
}
return CommonUtil.getPageList(pageNo, pageSize, datas);
}
}
页面关键部分:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk" pageEncoding="gbk" %>
<%@ taglib uri="http://eaptag.suntek.com" prefix="ui" %>
<%
String userCode=request.getRemoteUser();
String path = request.getContextPath();
if(userCode == null){
out.println("<script>top.location.href='" + path+ "/index.jsp';</script>");
return ;
}
%>
<html>
<head>
<title>高级查询</title>
<script type="text/javascript" src="/eaptag2/js/ccftag.form.js"></script>
<script type="text/javascript" src="/eaptag2/js/ccftag.zdialog.js" ></script>
<script type="text/javascript" src="<%=path%>/js/common.js" ></script>
<script type="text/javascript" src="<%=path%>/js/station.util.js" ></script>
<ui:Import tagNames="Window,Label,DateTimePicker,GTGrid,Button,GTGridField,Select"></ui:Import>
<style type="text/css">
.queryLabel{
height:19px;
width: 65px;
text-align:right;
padding-top: 2px;
}
</style>
<script type="text/javascript">
var ajax = new ccftag_ajax("<%=path%>/ccftag",false);
$(function(){
$('input:checkbox').click(function(){
var _this = $(this);
if(_this.attr("checked") == true){
$("input[name='status']").each(function(i){
$(this).removeAttr("checked");
});
_this.attr("checked","checked");
}else{
_this.removeAttr("checked");
}
doQuery();
});
});
function doQuery(){
var param = Form.formToBean(queryForm,"java.util.Map",true);
if(param.loginEndTime!=''&& param.loginBeginTime=='')
{
alert("请选择开始登记日期!");
return ;
}
if(param.logiBeginTime!=''&& param.loginEndTime=='')
{
alert("请选择结束登记日期!");
return ;
}
if(param.loginEndTime < param.loginBeginTime){
alert("开始登记日期必须小于结束登记日期!");
return ;
}
param.SYR = $("#queryName").val();
param.SFZMHM = $("#queryIdNo").val();
param.CJRZH = $("#queryDisabledNo").val();
param.XSZBH = $("#queryTravelNo").val();
param.HPHM = $("#queryVehNo").val();
param.CLSBDH = $("#queryChassisNo").val();
param.FDJH = $("#queryEngineNo").val();
param.CLLX= $("#queryBrand").val();
param.loginBeginTime= $("#loginBeginTime").val();
param.loginEndTime= $("#loginEndTime").val();
param.status = getSelectStatus();
var map = disabledGrid.resetParameter(20,param);
ajax.remoteCall("bean://com.suntek.disabledvehmgr.web.disabledveh.AdvancedQueryProvider:getData",[map],function(reply){
//alert(reply.getResult());
disabledGrid.cleanContent();
disabledGrid.setContent(eval("(" + reply.getResult() + ")"));
});
}
function getSelectStatus(){
var status = "";
var $statusObj = $("input[name='status']:checked");
$statusObj.each(function(i){
status += $statusObj.eq(i).val() + ",";
});
if(status.length > 0){
status = status.substring(0, status.length-1);
}
return status;
}
</script>
</head>
<body style="margin: 0px; overflow: hidden;" scroll="no">
<ui:Window id="winVehicleList" title="" style="width:100%;height:100%;table-layout:fixed;" >
<ui:Window.head>
<form id="queryForm">
<input type="hidden" id="BOOKING_TYPE" name="BOOKING_TYPE" value="4"/>
<label class="queryLabel">登记日期:</label>
<ui:DateTimePicker id="loginBeginTime" dateFmt="yyyy-mm-dd" value="2013-10-15" style="width:100px" readOnly="true"></ui:DateTimePicker>
至<ui:DateTimePicker id="loginEndTime" dateFmt="yyyy-mm-dd" value="2014-12-15" style="width:100px" readOnly="true"></ui:DateTimePicker>
<label class="queryLabel">姓名:</label><input type="text" id="queryName" style="width:106px;"/>
<label class="queryLabel">身份证号:</label><input type="text" id="queryIdNo" style="width: 166px;"/>
<label class="queryLabel">行驶证号:</label><input type="text" id="queryTravelNo" style="width:136px;"/><br/><br/>
<label class="queryLabel">残疾证号:</label><input type="text" id="queryDisabledNo" style="width:136px;"/>
<label class="queryLabel">车牌号:</label><input type="text" id="queryVehNo" style="width:106px;"/>
<label class="queryLabel">车架号:</label><input type="text" id="queryChassisNo" style="width:106px;"/>
<label class="queryLabel">发动机号:</label><input type="text" id="queryEngineNo" style="width:106px;"/>
<label class="queryLabel">厂牌型号:</label><input type="text" id="queryBrand" style="width:106px;"/>
<ui:Button cls="query" onClick="doQuery()">查询</ui:Button>
</form><hr/>
</ui:Window.head>
<ui:Window.body id="winBlackListGrid" style="width:100%;height:100%">
<ui:GTGrid id="disabledGrid"
rowNumPerPage="20"
css="width:100%; height:100%;"
uniqueField="ID"
exportSupport="true"
autoLoad="false"
dataProvider="bean://com.suntek.disabledvehmgr.web.disabledveh.AdvancedQueryProvider:getData">
<%-- <ui:GTGridField name="ID" header="序号" isChecked="true"/> --%>
<ui:GTGridField name="SYR" header="姓名" fieldType="string" align="left" width="100"/>
<ui:GTGridField name="SFZMHM" header="身份证号" fieldType="string" align="center" width="150"/>
<ui:GTGridField name="XSZBH" header="行驶证号" fieldType="string" align="center" width="120"/>
<ui:GTGridField name="CJRZH" header="残疾证号" fieldType="string" align="center" width="120"/>
<ui:GTGridField name="HPHM" header="车牌号" fieldType="string" align="center" width="120"/>
<ui:GTGridField name="CLSBDH" header="车架号" fieldType="string" align="center" width="80"/>
<ui:GTGridField name="FDJH" header="发动机号" fieldType="string" align="center" width="80"/>
<ui:GTGridField name="CLLX" header="厂牌型号" fieldType="string" align="center" width="80"/>
<ui:GTGridField name="ZDYZT" header="状态" fieldType="string" align="center" width="80"/>
<ui:GTGridField name="CCDJRQ" header="登记日期" fieldType="string" align="center" width="120"/>
<%-- <ui:GTGridField name="Files" header="申请资料电子影像文件" fieldType="string" align="center" width="140"/> --%>
</ui:GTGrid>
</ui:Window.body>
</ui:Window>
<!-- <iframe name="downloadFrame" border='0' width='0' height='0'></iframe> -->
</body>
</html>
JAVA 中SQL字符动态拼接的更多相关文章
- [转帖]关于Java中SQL语句的拼接规则
关于Java中SQL语句的拼接规则 自学demo 的时候遇到的问题 结果应该是 '"+e.getName()+"' 注意 一共有三组标点符号 (除去 方法函数后面的括号) 实现目标 ...
- 关于Java中SQL语句的拼接规则
实现目标语句是这个注意,这里的java变量是idd int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEE ...
- java中sql语句能不能加分号的问题?
一.原因 在程序运行中,当执行sql后总是报无效字符错误:但是把程序放在pl/sql中执行又没有错误.让我很纳闷!于是我开始查找资料,然后我终于发现了问题. 二.问题剖析 原来在程序中:如果你在程序 ...
- Java中面向字符的输入流
Java中面向字符的输入流 2016-12-04 Java程序员联盟 Java程序员联盟 Java程序员联盟 微信号 javalm 功能介绍 莫道君行早,更有早行人 全心敲代码,天道自酬勤 字符流是针 ...
- java中的字符集和编码
前言 上次对计算机中的“字符集”和“编码”分别进行了总结,并指出二者之间的区别,不要搞混了,不清楚的再回到上一章看一下.今天再总结下java中是如何使用字符集(主要是Unicode字符集,其他常用字符 ...
- Java中的JDK动态代理
所谓代理,其实就是相当于一个中间人,当客户端需要服务端的服务时,不是客户直接去找服务,而是客户先去找代理,告诉代理需要什么服务,然后代理再去服务端找服务,最后将结果返回给客户. 在日常生活中,就拿买火 ...
- Java中sql语句的引号问题
1..sql语句 在数据库中,当我们查询语句时,会使用类似的语句: Select * from userinfo where userid='1' or 1; Select * from userin ...
- 【Java编程】Java中的字符串匹配
在Java中,字符串的匹配可以使用下面两种方法: 1.使用正则表达式判断字符串匹配 2.使用Pattern类和Matcher类判断字符串匹配 正则表达式的字符串匹配: ...
- JAVA中Sql时间格式与util时间格式转换
关于时间格式转化: java.util.Date 与 java.sql.Date 互换 sql是子类 字符串转化成java.util.Date SimpleDateFormat date =n ...
随机推荐
- java 21 - 12 随机访问流(不属于IO流)
随机访问流: RandomAccessFile类不属于流,是Object类的子类. 但它融合了InputStream和OutputStream的功能. 支持对文件的随机访问读取和写入. public ...
- request模块提交数据
http://ctf8.shiyanbar.com/jia/ #coding:utf-8import re,requestsurl = r"http://ctf8.shiyanbar.com ...
- ArcGis实现添加MultiLayerMarkerSymbol(多个符号叠加生成新的符号)
, , ); pMarkerSymbol.Angle = ; pMarkerSymbol.XOffset = ;;;;, , ); ...
- Entity Framework4.0 (一)概述(EF4 的Database First方法)
转自:http://www.cnblogs.com/marksun/archive/2011/12/15/2289582.html Entity Framework4.0(以后简称:EF4),是Mic ...
- Nuget如何管理本地的包
1.在nuget中创建一个本地的程序包源
- C# 无边框窗体边框阴影效果
通过下面代码在构造函数中调用方法 SetShadow(); 即可实现无边框窗体的阴影效果了 需要添加命名空间 using System.Runtime.InteropServices; private ...
- [资源]PHP使用消息队列
利用PHP操作Linux消息队列完成进程间通信 基于HTTP协议的轻量级开源简单队列服务:HTTPSQS[原创] Redis队列——PHP操作简单示例 入队操作 <?php $redis = n ...
- Discuz 取各排行榜数据
取论坛指定版块帖子或回复(first=1 就是帖子的1楼, 如果=0 就是调用回复,fid=62 是论坛版块号): SELECT * FROM discuzx.pre_forum_post where ...
- C#中小数点后保留两位小数,四舍五入的函数及使用方法
Math.Round(45.367,2) //Returns 45.37 Math.Round(45.365,2) //Returns 45.36 C#中的Round()不是我 ...
- Java系列:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Javaweb工程类中没有添加Tomcat运行时相关类导致. 下面是具体的解决方法: 1.右击web工程->属性或Build Path->Java Build Path->Libra ...