日期:2014-05-18  浏览次数:20631 次

java dao实现类中查询 mysql的一个小问题
1.我有一个表,表中存有几个关键字,
表名newskey_words,只有一个字段,key_words,有几个属性值,a,b,c,d.
2.我有一个方法,接受用户输入的一个新闻标题作为参数。参数类型为:string title.
3.这个方法,如下:
public boolean checkKeywords(String title) {//向表中查询是否有相应的关键字。
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement ppst = null;
ResultSet rs = null;
try{
conn = DAOUtil.getConnection();
ppst = conn.prepareStatement("select * from news_keywords ");
 

//ppst.setString(1, title);
rs = ppst.executeQuery();
while(rs.next()){

//News news = new News();
rs.getString(1);
 
}
}
catch(SQLException e){e.printStackTrace();}
finally{
try{
if(rs!=null){rs.close();}
if(ppst!=null){ppst.close();}
if(conn!=null){conn.close();}
}
catch(SQLException e){e.printStackTrace();}
}return false;//
}
4.请问如何在while(rs.next())判断后,直接实现当前用户输入标题与库中已有关键字进行检查的操作?

------解决方案--------------------

------解决方案--------------------
应该是在select * from news_keywords 的where的条件中判断。。
怎么会是在结果集中判断呢??
------解决方案--------------------
if(rs.next())
------解决方案--------------------
写个boolean
Java code

boolean hasinfo=fasle;
while(rs.next() && !hasinfo){ 
String str=rs.getString("***");//你要比对的数据
if(str.equalse(title))
    hasinfo=false;  
}