一个自己写的的新闻网站,这是搜索,输入关键字,汉字、数字、字母都可以
public ArrayList<Newslist> findname(String name){
ArrayList<Newslist> list = new ArrayList<Newslist>();
//这是从新闻标题或内容里根据关键字查找
String sql = "select * from newslist where n_name like ? or n_text like ?";
Object [] param = {"%"+name+"%","%"+name+"%"};
ResultSet rs = null;
try {
rs = DBUtil.select(sql, param);
while(rs.next()){
System.out.println("re.next()");
Newslist news = new Newslist();
news.setId(rs.getInt(1));
news.setName(rs.getString(2));
news.setText(rs.getString(3));
news.setType(rs.getString(4));
news.setPub(rs.getInt(5));
news.setPubtime(rs.getString(6));
news.setEdi(rs.getInt(7));
news.setEditime(rs.getString(8));
news.setState(rs.getString(9));
list.add(news);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
//但是当关键字是汉字的时候,程序执行到 rs = DBUtil.select(sql, param); 就不执行了
//这是select函数:
public static ResultSet select(String sql,Object[] param) throws SQLException{
PreparedStatement pstmt = getConn().prepareStatement(sql);
//通过for循环来设置?中的值
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i+1, param[i]);
}
return pstmt.executeQuery();
}
求助啊.
------解决方案--------------------
页面、java都用相同的字符集试试的吧。
个人觉得字符集的可能性相当大。
可以设置一下request.setCharacterEncoding("UTF-8");试一下
------解决方案--------------------
URL传递中文参数
URL传递中文参数,默认格式为ISO-8859-1,一般在接收该参数时,如果不进行编码,得到的会是乱码。
一般不建议url传参,更不建议传递中文参数,如果非传不可,在传递中文参数时,接收参数时将参数转换,例如:
String result = new String(name.getBytes("ISO-8859-1"), "utf-8");
第二种:在服务器xml代码中改配置信息:
<Connector port="8080"protocol="HTTP/1.1" maxThreads="150" connectionTimeout="20000"
redirectPort="8443"URIEncoding="客户端编码"/>
------解决方案--------------------
刚开始没看到这一段,是不是数据库的编码与后台编码不一致啊
你的jdbc怎么写的,连数据库的时候加编码了没?
如果是用的框架的话
jdbc:mysql://172.16.9.6:3306/school?useUnicode=true&characterEncoding=UTF-8
或者是这种形式的
con = (Connection) DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/company?useUnicode=true&characterEncoding=UTF-8", "root", "");// 创建数据连接
------解决方案--------------------
http://jingyan.baidu.com/article/4b07be3c7dfb3248b380f3ef.html
如何修改oracle数据库,你去看看吧,希望对你有所帮助,暂时没找到在连接oracle时转编码的!