问题描述:我写的这个是根据id和地址查询信息。
如果什么不输入,查询全部信息
如果输入id,将根据id查询信息
如果输入乡镇详细地址,将根据乡镇详细地址查询信息
如果2个都输入将根据2个一起查询信息。
问题是:我输入id值好用!,其他都不好用,查询全部信息,还显示0,希望大家多多帮助。

package com.hygj.buccess;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;import com.hygj.buccess.impl.Iuserinfo;
import com.hygj.db.DbConn;
import com.hygj.voo.Userinfo;public class UserOper implements Iuserinfo {

// 根据村镇详细地址查询
public Userinfo retiveByNeighborVillageName(String neighborVillageAddress) {
Connection conn = DbConn.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
Userinfo user = null;
String sql = "select * from neighbor_village where neighborVillageAddress = ?";
try {
pstmt = conn.prepareStatement(sql);
       pstmt.setString(1, neighborVillageAddress);
rs = pstmt.executeQuery();
while (rs.next()) {
user = new Userinfo();
user.setNeighborVillageid(rs.getInt(1));
user.setNeighborVillageName(rs.getString(2));
System.out.println(user.getNeighborVillageName());
user.setNeighborVillageCreateUid(rs.getString(3));
System.out.println(user.getNeighborVillageCreateUid());
user.setNeighborVillageUserAddressid(rs.getString(4));
user.setNeighborVillageAddress(rs.getString(5));
System.out.println(user.getNeighborVillageAddress());
user.setNeighborVillageCreateTime(rs.getString(6));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageAlterTime(rs.getString(7));
user.setNeighborVillageAvailable(rs.getInt(8));
user.setNeighborVillageChangeUid(rs.getString(9));
user.setNeighborVillageChangeTime(rs.getString(10));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageChanged(rs.getInt(11));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} return user;
} // 根据id查询
public Userinfo retiveByid(int neighborVillageid) {
Connection conn = DbConn.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
Userinfo user = null;
String sql = "select * from neighbor_village where neighborVillageid =?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, neighborVillageid);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new Userinfo();
user.setNeighborVillageid(rs.getInt(1));
user.setNeighborVillageName(rs.getString(2));
// System.out.print(user.getNeighborVillageName());
user.setNeighborVillageCreateUid(rs.getString(3));
// System.out.print(user.getNeighborVillageCreateUid());
user.setNeighborVillageUserAddressid(rs.getString(4));
user.setNeighborVillageAddress(rs.getString(5));
//System.out.print(user.getNeighborVillageAddress());
user.setNeighborVillageCreateTime(rs.getString(6));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageAlterTime(rs.getString(7));
user.setNeighborVillageAvailable(rs.getInt(8));
user.setNeighborVillageChangeUid(rs.getString(9));
user.setNeighborVillageChangeTime(rs.getString(10));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageChanged(rs.getInt(11));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return user; } // 根据详细地址和时间查询
public Userinfo retiveByNeighborvllage(int neighborVillageid,
String neighborVillageAddress) {
Connection conn = DbConn.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
Userinfo user = null;
String sql = "select * from neighbor_village where neighborVillageid =? and neighborVillageAddress=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, neighborVillageid);
pstmt.setString(2, neighborVillageAddress);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new Userinfo();
user.setNeighborVillageid(rs.getInt(1));
user.setNeighborVillageName(rs.getString(2));
// System.out.print(user.getNeighborVillageName());
user.setNeighborVillageCreateUid(rs.getString(3));
// System.out.print(user.getNeighborVillageCreateUid());
user.setNeighborVillageUserAddressid(rs.getString(4));
user.setNeighborVillageAddress(rs.getString(5));
// System.out.print(user.getNeighborVillageAddress());
user.setNeighborVillageCreateTime(rs.getString(6));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageAlterTime(rs.getString(7));
user.setNeighborVillageAvailable(rs.getInt(8));
user.setNeighborVillageChangeUid(rs.getString(9));
user.setNeighborVillageChangeTime(rs.getString(10));
// System.out.print(user.getNeighborVillageCreateTime());
user.setNeighborVillageChanged(rs.getInt(11));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
} }
}
return user;
}
}package com.hygj.dao;import java.util.ArrayList;
import java.util.List;import com.hygj.buccess.UserOper;public class GenericDao {
 public List  selectEmps(int neighborVillageid,String neighborVillageAddress)
 {
 List a = new ArrayList();
 UserOper  oper =new UserOper();
 if(neighborVillageAddress!=null &&neighborVillageAddress.length()>0)
 {
 a.add(oper.retiveByNeighborVillageName(neighborVillageAddress));
 }
 
 if(neighborVillageid==0 && neighborVillageAddress==null )
 {
a.add (oper.retive());
 }
 else if(neighborVillageid!=0)
 {
 a.add(oper.retiveByid(neighborVillageid));
 
 }
 else if(neighborVillageAddress!=null &&neighborVillageAddress.length()>0)
 {
 a.add(oper.retiveByNeighborVillageName(neighborVillageAddress);
 
 
 }
 else{
 a.add(oper.retiveByNeighborvllage(neighborVillageid, neighborVillageAddress));

 }
 return a;
 }
 }package com.hygj.voo;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.hygj.dao.GenericDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class Userinfo extends ActionSupport{
// 村镇编号
private int neighborVillageid;
// 村镇名称
private String neighborVillageName;
// 创建人编号
private String neighborVillageCreateUid;
// 友邻地址分类编号
private String neighborVillageUserAddressid;
// 村镇详细地址
private String neighborVillageAddress;
// 创建时间
private String neighborVillageCreateTime;
// 最后修改时间
private String neighborVillageAlterTime;
// 是否可用
private int neighborVillageAvailable;
// 被转让人编号
private String neighborVillageChangeUid;
//转让时间
private String neighborVillageChangeTime;
// 是否转让
省略get(),set()方法
@Override
public String execute(){
ActionContext ctx = ActionContext.getContext();
ctx.put("elist", new GenericDao().selectEmps(neighborVillageid, neighborVillageAddress));
System.out.println(ctx.get("elist"));
return "Message";

}

}chengzhen.jsp<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD  4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="/blog/js/Calendar1.js"></script>
<title>My JSP 'sousuo.jsp' starting page</title> </head>
<body>
<s:form  action="message.action" method="post" >
<s:textfield label="用户id" name="neighborVillageid"/>
  <s:textfield label="乡镇详细地址" name="neighborVillageAddress"/>
<s:submit value="查询"/>
</s:form>
<table border="1">
<tr>
<th>乡村编号</th>
<th>村镇名称</th>
<th>创建时间</th>
<th>村镇详细地址</th>
</tr>
<s:iterator value="elist" status="stu">
<tr>
<td><s:property value="neighborVillageid"/></td>
<td><s:property value="neighborVillageName"/></td>
<td><s:property value="neighborVillageCreateTime"/></td>
<td><s:property value="neighborVillageAddress"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>

解决方案 »

  1.   

    neighborVillageAddress 要单引号?
      

  2.   

    问题在于你写的代码:
    while (rs.next()) {    和  if (rs.next()) {      
    user = new Userinfo();     user = new Userinfo();
    。                             。                    
    。                             。                    
    }                          }     
    有区别的,while 是取出全部的数据,但是你有没有将user这个对象保存下来,只能取到最后一条数据 。
    if 就这能去第一条数据。