问答详情
源自:2-9 JDBC实战---搭建视图层(下)

关于search 查询女神信息代码 问题

else if(OPERATION_SEARCH.equals(in.toUpperCase())

||OPERATION_SEARCH.substring(0,1).equals(in.toUpperCase())

||OPERATION_SEARCH.equals(previous)){

previous =OPERATION_SEARCH;

//查找

List<Map<String,Object>> params=new ArrayList<Map<String,Object>> ();

Map<String,Object> param=new HashMap<String,Object>();

if(step==1){

System.out.println("请输入的name:");

}else if(2==step){

param.put("name", in);

System.out.println("请输入的关系:");

}else if(3==step){

param.put("rela", in);

System.out.println("请输入的结果:");

}else if(4==step){

param.put("value", in);

params.add(param);

try {

List<Goddess> gs =action.query(params);

for(Goddess g:gs){

System.out.print(g.toString());

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(OPERATION_SEARCH.equals(previous)){

step++;

}

}

最后显示结果

select * from imooc_goddess where 1=1 and null null 3

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null 3' at line 1

不知道最后sql为什么是null,不是传了值吗

提问者:慕粉1469110649 2016-08-06 13:43

个回答

  • 无我无伤
    2016-12-10 17:36:10

    -Allen说的方法是对的,亲测可用

  • 小小白白白
    2016-09-20 21:30:32

    我也出现了同样的问题,因为要添加字符串,但是就不能接受自己输入的值了


  • _Allen_
    2016-08-06 16:59:46

    造成前面两个null的原因是下面这两行要放在while循环外面,

    List<Map<String,Object>> params=new ArrayList<Map<String,Object>> ();

    Map<String,Object> param=new HashMap<String,Object>();


  • 夜还没黑
    2016-08-06 16:52:41

    param.put("name", "'"+in+"'");//加上一对单引号试试