java中的增删改查语句时,始终跑不通测试,折腾了半天才发现原来是sql语句拼写有问题,奇怪的是eclipse并没有提示;
//一个单行查询,修改前
public Customer getOne(Integer id) {
// TODO Auto-generated method stub
String sql = “select id,name,address,phone from customers”+
“where id = ?”;
Customer customer = get(sql,id);
return customer;
}
注意这个sql我是写在了两行,用加号拼接起来的,问题就出在这里,由于customers后面没有空格,这样其实就成了customerswhere,显然这个sql语句就不正确啦;
//解决方法:如下customers后加上一个空格就好啦
public Customer getOne(Integer id) {
// TODO Auto-generated method stub
String sql = “select id,name,address,phone from customers “+
“where id = ?”;
Customer customer = get(sql,id);
return customer;
}
ps:这是一个教训,在程序中拼长sql字符串时,尽量先在一行中把sql字符串拼好,之后觉得太长再在中间敲回车换行,这样能大大避免sql拼接错误的问题;