java 链接sql 模糊查询 不好使 求助 5
实现类:publicList<IpVo>queryAll(IpVovo,PageResultpage){PreparedStatementpst=null;Strings...
实现类:
public List<IpVo> queryAll(IpVo vo,PageResult page) {
PreparedStatement pst=null;
String sql="SELECT * FROM ip ";
StringBuffer sb = new StringBuffer();
sb.append(sql);
if(vo.getDepartmentname()!=null){
sb.append(" where departmentname LIKE '%");//sql+" where departmentname LIKE '%"
sb.append(vo.getDepartmentname());
sb.append("%'");
}
sql=sb.toString();
System.out.println(sql);
ResultSet rs=null;
List<IpVo> list =new ArrayList<IpVo>();
try {
pst=ConnectionManager.getConnection().prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
IpVo i=new IpVo();
i.setIpid(rs.getInt("ipid"));
i.setDevicenum(rs.getString("devicenum"));
i.setDepartmentname(rs.getString("departmentname"));
i.setIp(rs.getString("ip"));
i.setMac(rs.getString("mac"));
i.setZeren(rs.getString("zeren"));
i.setPhysics(rs.getString("physics"));
i.setPermission(rs.getString("permission"));
i.setRemarks(rs.getString("remarks"));
i.setTbuser(rs.getString("tbuser"));
i.setDtime(rs.getString("dtime"));
i.setFlag(rs.getInt("flag"));
list.add(i);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//解决中文乱码
IpBiz biz = new IpBizImpl();
List<IpVo> list =new ArrayList<IpVo>();
IpVo ip = new IpVo();
ip.setDepartmentname(request.getParameter("departmentname"));
PageResult page = new PageResult();
list=biz.queryAll(ip,page);
request.setAttribute("list",list);
request.getRequestDispatcher("/WEB-INF/jsp/ip/listIp.jsp").forward(request,response);
}
jsp页面:
<script type="text/javascript">
function query(){
document.getElementById("ipForm").submit();
}
function add(){
document.getElementById("ipForm").action = "toAdd.action";
document.getElementById("ipForm").submit();
}
</script>
<tr>
<td>部门名称:</td>
<td>
<input type="text" name=departmentname/>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="查询" onclick="query();"/>
</td>
</tr>
代码 中拼接的sql 语句拼不上 if(vo.getDepartmentname()!=null){
sb.append(" where departmentname LIKE '%");//sql+" where departmentname LIKE '%"
sb.append(vo.getDepartmentname());
sb.append("%'");
} 展开
public List<IpVo> queryAll(IpVo vo,PageResult page) {
PreparedStatement pst=null;
String sql="SELECT * FROM ip ";
StringBuffer sb = new StringBuffer();
sb.append(sql);
if(vo.getDepartmentname()!=null){
sb.append(" where departmentname LIKE '%");//sql+" where departmentname LIKE '%"
sb.append(vo.getDepartmentname());
sb.append("%'");
}
sql=sb.toString();
System.out.println(sql);
ResultSet rs=null;
List<IpVo> list =new ArrayList<IpVo>();
try {
pst=ConnectionManager.getConnection().prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
IpVo i=new IpVo();
i.setIpid(rs.getInt("ipid"));
i.setDevicenum(rs.getString("devicenum"));
i.setDepartmentname(rs.getString("departmentname"));
i.setIp(rs.getString("ip"));
i.setMac(rs.getString("mac"));
i.setZeren(rs.getString("zeren"));
i.setPhysics(rs.getString("physics"));
i.setPermission(rs.getString("permission"));
i.setRemarks(rs.getString("remarks"));
i.setTbuser(rs.getString("tbuser"));
i.setDtime(rs.getString("dtime"));
i.setFlag(rs.getInt("flag"));
list.add(i);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//解决中文乱码
IpBiz biz = new IpBizImpl();
List<IpVo> list =new ArrayList<IpVo>();
IpVo ip = new IpVo();
ip.setDepartmentname(request.getParameter("departmentname"));
PageResult page = new PageResult();
list=biz.queryAll(ip,page);
request.setAttribute("list",list);
request.getRequestDispatcher("/WEB-INF/jsp/ip/listIp.jsp").forward(request,response);
}
jsp页面:
<script type="text/javascript">
function query(){
document.getElementById("ipForm").submit();
}
function add(){
document.getElementById("ipForm").action = "toAdd.action";
document.getElementById("ipForm").submit();
}
</script>
<tr>
<td>部门名称:</td>
<td>
<input type="text" name=departmentname/>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="查询" onclick="query();"/>
</td>
</tr>
代码 中拼接的sql 语句拼不上 if(vo.getDepartmentname()!=null){
sb.append(" where departmentname LIKE '%");//sql+" where departmentname LIKE '%"
sb.append(vo.getDepartmentname());
sb.append("%'");
} 展开
展开全部
不好使指的是?
结果不是想要的?结果集为空?sql不被执行,报错?
结果不是想要的?结果集为空?sql不被执行,报错?
追问
正常模糊查询时不报错 只执行String sql="SELECT * FROM ip "; 这一句 但是 在执行修改时 保存后自动就取模糊查询的结果了
追答
if(vo.getDepartmentname()!=null) 这个前提你看看成立不成立
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看代码貌似没有问题啊,没有报错吗?报错说什么?会用firefox吗?firefox按f12可以调出开发工具,可以查看发送的请求,或者网页是否有错误!
追问
用过 我回去试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把sql语句打印出来放到数据库里面执行看看对不对
更多追问追答
追问
sql没问题
追答
是查不到数据吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询