日期:2014-05-17  浏览次数:20771 次

求助:java中实现模糊查询应该怎样写?
//模糊查询
public ArrayList getDiariesByCondition(String condition){
  ArrayList diaries=new ArrayList();
  Connection con=null;
  PreparedStatement pstmt=null;
  ResultSet rs=null;
try {
con=DBConnection.GetConnectionByJdbc();
pstmt=con.prepareStatement("select * from blogdiary where b_author like '%?%' "
+"or b_subject like '%?%' " 
+"or b_content like '%?%' " 
+"or b_weather like '%?%' " 
+"or b_provenance like '%?%' " 
+"or s_id in(select s_id from blogsort where s_name like '%?%' ");
pstmt.setString(1,condition);
pstmt.setString(2,condition);
pstmt.setString(3,condition);
pstmt.setString(4,condition);
pstmt.setString(5,condition);
pstmt.setString(6,condition);
rs=pstmt.executeQuery();
while(rs.next()){
BlogDiaryBean diary=new BlogDiaryBean();
diary.setId(rs.getInt(1));
diary.setUserId(rs.getInt(2));
diary.setPublishTime(rs.getString(3).substring(0,19));
diary.setAuthor(rs.getString(4));
diary.setSubject(rs.getString(5));
diary.setContent(rs.getString(6));
diary.setWeather(rs.getString(7));
diary.setProvenance(rs.getString(8));
diary.setSortId(rs.getInt(9));
diary.setObserveTime(rs.getInt(10));
diary.setReadTime(rs.getInt(11));
diaries.add(diary);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return diaries;
}
数据库连接没错,在sql中也能查出来,但写在class中就报错了,bean是确定没错的,以下是错误信息:
jdbc驱动加载成功!
jdbc数据库连接成功!
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(Unknown Source)
at com.accp.blog.model.dao.BlogDiaryDAO.getDiariesByCondition(BlogDiaryDAO.java:216)
at com.accp.blog.model.dao.BlogDiaryDAO.main(BlogDiaryDAO.java:254)
是写法错误吗?应该怎样改呢?请高手指教,先谢过了!

------解决方案--------------------
pstmt=con.prepareStatement("select * from blogdiary where b_author like '%?%' " 
+"or b_subject like '%?%' "
+"or b_content like '%?%' "
+"or b_weather like '%?%' "
+"or b_provenance like '%?%' "
+"or s_id in(select s_id from blogsort where s_name like '%?%' "); 


最近的括号少匹配一个 ,导置SQL语法出错了!

pstmt=con.prepareStatement("select * from blogdiary where b_author like '%?%' " 
+"or b_subject like '%?%' "
+"or b_content like '%?%' "
+"or b_weather like '%?%' "
+"or b_provenance like '%?%' "
+"or s_id in(select s_id from blogsort where s_name like '%?%' ) "); 

这样应该对! 试试看吧....
------解决方案--------------------
帮顶!
------解决方案--------------------
对象未实例化,空指针异常
看看你的BlogDiaryDAO.java类的216行和BlogDiaryDAO.java类254行




------解决方案--------------------
你把 like