81,119
社区成员
发帖
与我相关
我的任务
分享
比如:表中有俩字段:name,content
搜索关键字:csdn 提交
public List<User> search(String key) throws Exception {
getConnection();
List<User> users = new ArrayList<User>();
Statement stmt = DriverManager.createStatement();
String sql = "";
if (key == null) {
sql = "select * from table_name";
} else {
sql = "select * from table_name where content like "%" + key + "%";
}
ResultSet rs = stmt.exxcuteQuery(sql);
while (rs) {
String name = rs.getString("name");
String content = rs.getString("content");
if (key != null) {
name = name.replaceAll(key,"<font color='red'>" + key + "</font>");
content = content.replaceAll(key,"<font color='red'>" + key + "</font>");
}
User user = new User();
user.setName(name);
user.setContent(content);
users.add(user);
}
return users;
这样一个方法就实现了查询和显示全部功能,查询功能也可以实现返红的效果。
}