sql语句错误DB2 SQL Error: SQLCODE=-103, SQLSTATE=42...

报的异常

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute query; bad SQL grammar [select t.rule_code as ruleCode,t.run_time as runTime,t.err_code as errCode,t.err_msg as errMsg from  code_rep_rule_log t where 1=1and t.rule_code ='abc']; nested exception is com.ibm.db2.jcc.b.eo: DB2 SQL Error: SQLCODE=-103, SQLSTATE=42604, SQLERRMC=1and, DRIVER=3.53.95
com.ibm.db2.jcc.b.eo: DB2 SQL Error: SQLCODE=-103, SQLSTATE=42604, SQLERRMC=1and, DRIVER=3.53.95
at com.ibm.db2.jcc.b.bd.a(bd.java:676)
at com.ibm.db2.jcc.b.bd.a(bd.java:60)
at com.ibm.db2.jcc.b.bd.a(bd.java:127)
at com.ibm.db2.jcc.b.gm.c(gm.java:2484)
at com.ibm.db2.jcc.b.gm.d(gm.java:2461)
at com.ibm.db2.jcc.b.gm.a(gm.java:1962)
at com.ibm.db2.jcc.t4.db.g(db.java:138)
at com.ibm.db2.jcc.t4.db.a(db.java:38)
at com.ibm.db2.jcc.t4.t.a(t.java:32)
at com.ibm.db2.jcc.t4.sb.h(sb.java:141)
at com.ibm.db2.jcc.b.gm.bb(gm.java:1933)
at com.ibm.db2.jcc.b.hm.ec(hm.java:2558)
at com.ibm.db2.jcc.b.hm.e(hm.java:3259)
at com.ibm.db2.jcc.b.hm.Rb(hm.java:583)
at com.ibm.db2.jcc.b.hm.executeQuery(hm.java:557)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:112)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1414)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:153)
at com.krm.dao.hibernate.BaseDAOHibernate$6.doInHibernate(BaseDAOHibernate.java:492)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:356)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:329)
at com.krm.dao.hibernate.BaseDAOHibernate.list(BaseDAOHibernate.java:495)
at com.krm.dao.hibernate.BaseDAOHibernate.list(BaseDAOHibernate.java:457)
at com.krm.dao.hibernate.BaseDAOHibernate.list(BaseDAOHibernate.java:452)
at com.krm.standard.applog.dao.hibernate.AppLogDAOHibernate.getAppLogByCT(AppLogDAOHibernate.java:56)
at com.krm.standard.applog.service.impl.AppLogServiceImpl.getAppLogByCT(AppLogServiceImpl.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy103.getAppLogByCT(Unknown Source)
at com.krm.standard.applog.web.action.ApplicationLogAction.list(ApplicationLogAction.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
at com.krm.slsint.common.web.action.BaseAction.checkSessionTimeOut(BaseAction.java:930)
at com.krm.slsint.common.web.action.BaseAction.execute(BaseAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.krm.slsint.common.web.filter.RequestFilter.doFilter(RequestFilter.java:84)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)


A:原生态sql语句串接时注意空格
String sql = "select t.rule_code as ruleCode,t.run_time as runTime,t.err_code as errCode,t.err_msg as errMsg from  code_rep_rule_log t where 1=1" ;

//对单个条件进行判断串接
if(!"".equals(ruleCode))
{
//and前面需要有空格
//sql +="and t.rule_code ='"+ruleCode+"'";//问题出在这一行,因为是要和前面的sql串接,所以and前面需要空格
sql +=" and t.rule_code ='"+ruleCode+"'";
}


}

你可能感兴趣的:(db2异常,sql串接)