com.microsoft.sqlserver.jdbc.SQLServerException: '=' 附近有语法错误。求大神指点迷津!

qq_27784145 2015-11-23 04:10:06
public class WorkDAOImpl  implements WorkDAO {

protected Connection con; //数据库连接
protected PreparedStatement pre; //实例
protected ResultSet res; //结果集

public List<CustomerInfo> getAllList( int addTime ) {

List<CustomerInfo> list = new ArrayList<CustomerInfo>();
con = DBCon.getConnection();
try {
String sql="select a.* , b.condition_name from customer_info a ,customer_condition b " +
"where a.condition_id=b.condition_id and a.is_used='1' ";
if(addTime == 0 ){
sql += " select datediff(day,birth_day,getDate())=0";

} else if(addTime != 0 ){
sql += " select datediff(day,birth_day,getDate())>=0 and datediff(day,birth_day,getDate())<='"+addTime+"' ";

}
pre = con.prepareStatement(sql);
res = pre.executeQuery(); //报错内容中提到这行 也不知道为什么
while (res.next()) {
CustomerInfo customerInfo = new CustomerInfo();

customerInfo.setCustomerId(res.getInt("customer_id"));
customerInfo.setCustomerName(res.getString("customer_name"));
customerInfo.setCustomerBirthday(res.getString("birth_day"));
customerInfo.setCustomerMobile(res.getString("customer_mobile"));
customerInfo.setConditionId(res.getInt("condition_id"));
customerInfo.setCustomerCondition(res.getString("condition_name"));
customerInfo.setIsUsed(res.getString("is_used"));


list.add(customerInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBCon.closeAll(con,pre,res);
}
return list;
}
...全文
641 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
qq_27784145 2015-11-23
引用 6 楼 yanghaimingg 的回复:
[quote=引用 5 楼 yanghaimingg 的回复:] sql += " select datediff(day,birth_day,getDate())=0"; String类型是不能这样的,用StringBuffer拼接完再转成String
错了,好像是可以这样。。。[/quote] 但是这句放在sql server 2008数据库里执行不了,报:无法绑定由多个部分组成的标识符 "a.link_nexttime"。为何??
回复
mtian2020 2015-11-23
引用 5 楼 yanghaimingg 的回复:
sql += " select datediff(day,birth_day,getDate())=0"; String类型是不能这样的,用StringBuffer拼接完再转成String
错了,好像是可以这样。。。
回复
mtian2020 2015-11-23
sql += " select datediff(day,birth_day,getDate())=0"; String类型是不能这样的,用StringBuffer拼接完再转成String
回复
qq_27784145 2015-11-23
那改如何写?sql server的时间间隔查询语句?请教
回复
KIRA_GIRL 2015-11-23
sql写错了呗,把你最后执行的sql打印出来,放到数据库里试试
回复
sinat_31535993 2015-11-23
两个select拼在一起是干嘛的?
回复
qq_27784145 2015-11-23
com.microsoft.sqlserver.jdbc.SQLServerException: '=' 附近有语法错误。
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)
	at com.sxxy.dao.impl.WorkDAOImpl.getLinkMan(WorkDAOImpl.java:89)
	at com.sxxy.service.impl.WorkServiceImpl.getLinkMan(WorkServiceImpl.java:29)
	at com.sxxy.servlet.work.WorkQueryServlet.doGet(WorkQueryServlet.java:51)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
	at java.lang.Thread.run(Thread.java:722)
系统报如上的错误 可以运行程序 但是有错
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-23 04:10
社区公告
暂无公告