数据库查询时如何检测结果集是否为空
- Java code
try { con=DriverManager.getConnection("jdbc:MySQL://127.0.0.1:3306/Phone_Contacts", "root","fzg125009"); if(radio_name.isSelected()) { pre=con.prepareStatement("SELECT DISTINCT * FROM Contact WHERE name Like '"+field_name.getText()+"%'"); } else if(radio_phone.isSelected()) { pre=con.prepareStatement("SELECT DISTINCT * FROM Contact WHERE mobile Like '"+field_phone.getText()+"%'"); } rs=pre.executeQuery(); row=new CachedRowSetImpl(); row.populate(rs); if(row.wasNull())//这个不能被执行,所以这样检测不可以 { text.append("Sorry,无此记录!\n"); } con.close(); while(row.next()) { text.append(row.getString(1)+"\t"); text.append(row.getString(2)+"\n"); } } catch (SQLException error) { System.out.println("error!"); }
谢谢大家
------解决方案--------------------
while(row.next())
{
text.append(row.getString(1)+"\t");
text.append(row.getString(2)+"\n");
}
row.close();
pre.close();
con.close();
if(text.toString().equals("")
{
text.append("Sorry,无此记录!\n");
}