老师您好,我在使用List<Map<String,Object>>params查询方法时,想从键盘通过输入汉字名字,查询出了问题,怎样将汉字通过字符输进去
模型层:
public List<Goddness> query(List<Map<String,Object>>params) throws Exception{
//获取数据库连接
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select * from imooc_goddness where 1=1" );
if(params!=null&¶ms.size()>0)
{
for(int i=0;i<params.size();i++)
{
Map<String,Object> map=params.get(i);
sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value")+" ");
}
}
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
System.out.println(sb.toString());
ResultSet rs=ptmt.executeQuery();
List<Goddness> result=new ArrayList<Goddness>();
Goddness g=null;
while(rs.next())
{
g=new Goddness();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
result.add(g);
}
return result;
}
}
视图层:
List<Map<String,Object>>params=new ArrayList<Map<String,Object>>();
Map<String,Object>map=new HashMap<String,Object>();
map.put("name", "user_name");
map.put("rela", "=");
System.out.println("请输入要查询女神姓名:");
map.put("value", scan.next());
params.add(map);
List<Goddness> result=action.query(params);
for(Goddness g:result){
System.out.println(g);
}
控制台提示错误如下: