+ -
当前位置:首页 → 问答吧 → mysql中文字符模糊查询问题

mysql中文字符模糊查询问题

时间:2009-03-02

来源:互联网

查询语句如下 数据库字符集为gb2312 
jsp页post提交 String id =new String(request.getParameter("id").getBytes("iso8859-1"),"GBK");
查询为 list=spd.selectSpxx(id,id,page);
当查询英文时
nai
com.mysql.jdbc.ServerPreparedStatement[1] - select * from spxx where A1 like '%nai%' or A2 like '%nai%' limit 0,5 
当查中文时

com.mysql.jdbc.ServerPreparedStatement[1] - select * from spxx where A1 like '%?%' or A2 like '%?%' limit 0,5 
在mysql数据库中查 select * from spxx where A1 like "%奶%" or A2 like "%奶%" limit 0,5 
可得到结果 请问该怎么解决
public List selectSpxx(String pinyin,String hanzi,int t){
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs = null;
List list =new ArrayList();
con=new DB().getConnection();
try {
stmt=con.prepareStatement("select * from spxx where A1 like ? or A2 like ? limit ?,5 ");
stmt.setString(1, "%"+pinyin+"%");
stmt.setString(2, "%"+hanzi+"%");
stmt.setInt(3, t);
rs=stmt.executeQuery();
System.out.println(stmt);
while(rs.next()){
spxxBean xb=new spxxBean();
xb.setA1(rs.getString("A1"));
xb.setA2(rs.getString("A2"));
xb.setA3(rs.getString("A3"));
xb.setA8(rs.getString("A8"));
xb.setA12(rs.getDouble("A12"));
xb.setA13(rs.getInt("A13"));
xb.setId(rs.getInt("id"));
list.add(xb);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(list.size());
return list;
}
public List select(String value[]){
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
List spList=new ArrayList();
con=new DB().getConnection();
try {
stmt=con.prepareStatement("Select * from spxx where id=?");
for(int i=0;i<value.length;i++){
stmt.setString(1, value[i]);
rs=stmt.executeQuery();
}
while(rs.next()){
spxxBean sp=new spxxBean();
sp.setA1(rs.getString("A1"));
sp.setA2(rs.getString("A2"));
sp.setA3(rs.getString("A3"));
sp.setA8(rs.getString("A8"));
sp.setA12(rs.getDouble("A12"));
sp.setA13(rs.getInt("A13"));
sp.setId(rs.getInt("id"));
spList.add(sp);
}
}catch(SQLException e){
e.printStackTrace();
}
return spList;
}

作者: mayifeng165   发布时间: 2009-03-02

你的做法是可以的。

估计你的jdbc配置的 url没写好。贴上来看看!

作者: java2000_net   发布时间: 2009-03-02

我曾经遇到过这个问题,
首先你要确认的的sql存储时的编码方式为GBK 或 GB2312 或UTF-8
还有就是mysql用模糊查询时当参数是从别的地方传过来的时候,要用
stmt.setString(1, "%"+你的中文参数+"%"); 
不能够把传过来的参数写在select语句当中,select中只能用?代替

作者: yang_zheng_2008   发布时间: 2009-03-02

是不是报sql语句错误,还是根本查不出数据

作者: lazybears   发布时间: 2009-07-01

第一种解决方法:where binary A1 like '%$key%' 或 where A1 binary like '%$key%'。

第二种方法(没有用处再试):把数据库的A1改成binary

作者: zzs0618   发布时间: 2009-07-01

看看这个:
http://www.cublog.cn/u/10921/showart_196779.html

作者: chouy   发布时间: 2009-07-12

我的是jdbc连接的问题, jdbc:mysql://localhost:3306/softshop?characterEncoding=gbk

characterEncoding=gbk

作者: holdlg   发布时间: 2011-08-27