servlet+js+javabean做个模糊查询的问题
想在JSP页面上的文本框内任意输入一个字符或字符串,点击BUTTON能模糊查询到数据库匹配的数据,我用的是ORACLE数据库代码如下,希望各位大侠能帮忙解决下:实体类:public class User {
private String id; //id
private String name; //姓名
private String age; //年龄
private String 易做图; //性别
public String getId() {
return id;
}...set....get....}
dao层类:public class UserHandlerImpl implements UserHandler {
@Override
public List<User> listName(String name) throws SQLException {
List<User> users = new ArrayList<User>();
Connection conn = DBUtil.getConnection();
String sql = "select * from n_user where name='%?%'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
try{
while (rs.next()) {
User u = new User();
u.setId(rs.getString("id"));
u.setAge(rs.getString("age"));
u.setSex(rs.getString("易做图"));
u.setName(rs.getString("name"));
users.add(u);
}
}catch(SQLException e){
e.printStackTrace();}
finally{
DBUtil.close(conn);
}
return users;
}
}
servlet类:}else if(path.equals("/listName")){
String name = request.getParameter("name");
UserHandler uh = new UserHandlerImpl();
try {
List<User> users = uh.listName(name);
request.setAttribute("users", users);
request.getRequestDispatcher("result.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
XML已配置好 以上后台代码 无法在JSP页面上取值。。。。望各位大侠帮个忙 或者有实例的 可以发送我邮箱:20350586@qq.com 小弟不甚感激 --------------------编程问答--------------------
String sql = "select * from n_user where name='%?%'";
模糊查询怎能用=号,
用like吧 --------------------编程问答-------------------- 哦对 这代码我纠结过半天。。。重新编缉下 感谢楼上提醒 --------------------编程问答-------------------- String sql = "select * from n_user where name like '%?%'"; 已更改 --------------------编程问答-------------------- java.sql.SQLException: ORA-01006: bind variable does notexist 运行后是报此错 用DEBUG查询 DAO类中的while (rs.next()) {
User u = new User();
u.setId(rs.getString("id"));
u.setAge(rs.getString("age"));
u.setSex(rs.getString("易做图"));
u.setName(rs.getString("name"));
users.add(u); 无法注入值 求高人帮忙 --------------------编程问答-------------------- 什么数据库?如果是oracle应该是
String sql = "select * from n_user where name like '%'||?||'%'";
--------------------编程问答-------------------- sql语句错 --------------------编程问答-------------------- 报什么异常来着。
JSP页面没有贴嘛。 --------------------编程问答-------------------- 问题已解决 --------------------编程问答-------------------- JSP页面正确的 我知道错在哪了。。。
DAO层代码改为:String sql = "select * from n_user where name like '%"+name+"%'";
PreparedStatement ps = conn.prepareStatement(sql);
//ps.setString(1, name);
便可。。。。晕死 谢谢各位了 结贴 --------------------编程问答-------------------- String sql = "select * from n_user where name like ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
补充:Java , Web 开发