调用的相关方法如下:
public class LoginAction
{
   if(InterUser.CheckUser(db,name,psw))
{
可能出问题的地方: session.setAttribute(Constants.DEP_LIST_KEY,Dep.Search(db));
session.setAttribute(Constants.LOGIN_USERNAME_KEY,name);

PageForward="toEmployeeMain";
}
}
public class InterUser
{
public static boolean CheckUser(DB db,String username,String password)throws Exception
{
String strSql;
ResultSet rs;
strSql="select * from employee where username='"+username+"'"+" and password='"+password+"'";
rs=db.OpenSql(strSql); 
if(rs!=null)
{
if(rs.next())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
public class DB
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
         public ResultSet OpenSql(String sql)
{
try
{

                  stmt=conn.createStatement();
    
                  rs=stmt.executeQuery(sql);
                   }catch(SQLException ex)
                  {
                 ex.printStackTrace();
                  }
    
                  return rs;
          }
}
可能出问题的地方:
public class Dep
{
                  Vector depList=new Vector();
ResultSet rs=null;
String depSql="select * from department";

rs=db.OpenSql(depSql);

while(rs.next())
{
Dep dep=new Dep();

dep.setId(rs.getInt("id"));
dep.setName(rs.getString("depname"));
depList.add(dep);
}
return depList;

}

解决方案 »

  1.   

    小弟调2天了~,不知道怎么了~v.size()就是0了~
    select * from department;数据库中有2条记录,
    怎么说最后的结果都不应该是0啊~!!拜托各位前辈了~能帮忙调试出来另开帖送分都行~谢谢了~
    代码很麻烦~劳烦各位了~
      

  2.   

    另外session.setAttribute(Constants.LOGIN_USERNAME_KEY,name);
    这条语句在JSP中调用<%=session.getAttribute(Constants.LOGIN_USERNAME_KEY)%>
    可以提示出来用户名~
      

  3.   

    Vector depList=new Vector();
    ResultSet rs=null;
    String depSql="select * from department";

    rs=db.OpenSql(depSql);

    while(rs.next())
    {
    Dep dep=new Dep();

    dep.setId(rs.getInt("id"));
    dep.setName(rs.getString("depname"));
    depList.add(dep);
    }
    return depList;在这里返回前判断一下rs中是否有数据结果集或者Vector的size。
      

  4.   

    楼上说的对,看看你取数据时是否加如v了,还有<select name=dep>
    <%if(v == null )                    //这里应该是if(v != null )  吧
    {
          for(int i = 0;i < v.size();i++)
          {  
            dep = (Dep)v.get(i);%>
             <option value=<%=dep.getId()%>> <%=dep.getName()%></option>
             <%
           }
    }
    else{
    %><option value=1><%=v.size()%></option><%}%>
      

  5.   

    问题出在这里吧,我以前做类似的也出现空的情况
    把depList.add(dep);改成depList.addElement(dep);
      

  6.   

    998 兄和猪头兄说的
    IF()
    {
       while(rs.next())
    }
    我做了判断也没用的~
    和if(v != null )
    的问题其实是在我代码里有的~,不过这样显示不出来异常信息~
    只是页面不对~
    所以改成这样能直接看出来返回值的异常来~不好意思~这两天可能上不来了~
    兄弟们帮忙看看~5/4号回来散分~谢谢大家热心~~
      

  7.   

    问题出在这里吧,我以前做类似的也出现空的情况
    把depList.add(dep);改成depList.addElement(dep);这个也不行哦~
    现在我传回来的depList值是0~
    不知道怎么回事~
    数据库里有2条记录的~没问题~
      

  8.   

    这是个什么东西?一个类没有方法就出了一堆代码了
    public class Dep
    {
                      Vector depList=new Vector();
    ResultSet rs=null;
    String depSql="select * from department";rs=db.OpenSql(depSql);while(rs.next())
    {
    Dep dep=new Dep();dep.setId(rs.getInt("id"));
    dep.setName(rs.getString("depname"));
    depList.add(dep);
    }
    return depList;}先在action里面确定那个vector.size()是否为0,若不为0再找找其他原因
      

  9.   

    有方法  public static Vector Search(DB db)throws Exception
    刚才的代码放在这里
    为什么vector.size()的值会是0?
    明明在你帖的代码里写了啊
    while(rs.next())
    {
    Dep dep=new Dep();dep.setId(rs.getInt("id"));
    dep.setName(rs.getString("depname"));
    depList.add(dep);
    }
    return depList;
    应该不是0啊~,数据库里有2条信息的
      

  10.   

    改成这样~depList还是空的
    郁闷~
    public static Vector Search(DB db)throws Exception
    {
    Vector depList=new Vector();
    depList=null;
    ResultSet rs=null;
    String depSql="select * from department";

    rs=db.OpenSql(depSql);
    if(rs!=null)
    {
    while(rs.next())
    {
    Dep dep=new Dep();

    dep.setId(rs.getInt(1));
    dep.setName(rs.getString(2));
    depList.add(dep);

    }

    return depList;
    }
    else
    {
    depList.add(1);
    return depList;
    }
    }
      

  11.   

    1. 你的 Connection 初始化语句写在哪里?贴上来看看。
    2. 观察控制台输出,有否异常错误信息提示?
      

  12.   

    楼上的兄弟~控制台输出~?在哪里看~?
    package docrm;import java.sql.*;
    import java.util.*;public class DB
    {
    Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;
    public DB() throws Exception
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url="jdbc:odbc:DB";
    String user="sa";
         String password=""; 
         conn=java.sql.DriverManager.getConnection(url,user,password);
    }catch(SQLException ex)
    {
    ex.printStackTrace();
    }
    }
    public ResultSet OpenSql(String sql)
    {
    try
    {

         stmt=conn.createStatement();
        
         rs=stmt.executeQuery(sql);
        }catch(SQLException ex)
        {
         ex.printStackTrace();
        }
        
        return rs;
      }
      
      public int ExecSql(String sql)
      {
       int result=0;
       try
       {
       stmt=conn.createStatement();
       result=stmt.executeUpdate(sql);
       }catch(SQLException ex)
       {
       ex.printStackTrace();
       }
      
       return result;
      }
      
      public void close()
      {
       if(conn!=null)
       {
       try
       {
       rs.close(); 
            stmt.close(); 
       conn.close();
       }catch(SQLException ex)
       {
       ex.printStackTrace();
       }
       }
      }
    }
      

  13.   

    看上去一切正常,你的问题可真有难度了。你的开发工具是?应用服务器是?如果是Tomcat,控制台输出窗口就是启动Tomcat时运行的那个CMD窗口。
      

  14.   

    不要应该不应该,要看得到,
    在return depList;之前加一条语句:System.out.println("depList's size:" + depList.size());
    再运行,在控制台输出窗口就是启动Tomcat时运行的那个CMD窗口中看加的那条语句的结果是几
    ok,你看到了可以发短信给我,乐意帮忙,^-^
      

  15.   

    呵呵~,谢谢jshi123和瓜瓜,
    这个问题经过调试可以解决~,但是却很奇怪~非常奇怪~希望咱一块讨论下~
    public class LoginAction extends Action
    {

    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)throws Exception
    {
    UserForm userform=(UserForm)form;
    String name=(String)userform.getName();
    String psw=(String)userform.getPsw();
    //看到这个定义没? DB db=new DB();
    HttpSession session=request.getSession(true);
    String PageForward=null; Vector newsList=new Vector(10);
    for(int i=0;i<3;i++)
    {
    newsList.add("sun");
    }
    ActionMessages errors=new ActionMessages();

    if(InterUser.CheckUser(db,name,psw))
    {

    //在这里调用了db对象      session.setAttribute(Constants.DEP_LIST_KEY,Dep.Search(db));

    session.setAttribute(Constants.LOGIN_USERNAME_KEY,name);


    PageForward="toEmployeeMain";
    }
    else
    {
    errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("errors.accessDeny"));
    if(!errors.isEmpty())
    {
    saveErrors(request,errors);
    }
    PageForward="toWrong";
    }
    db.close();
    return mapping.findForward(PageForward);
    }
      

  16.   

    public class LoginAction extends Action
    {

    public ActionForward execute(ActionMapping mapping,
      ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)throws Exception
    {
    UserForm userform=(UserForm)form;
    String name=(String)userform.getName();
    String psw=(String)userform.getPsw();
    //注释掉这句      DB db=new DB();
    HttpSession session=request.getSession(true);
    String PageForward=null; Vector newsList=new Vector(10);
    for(int i=0;i<3;i++)
    {
    newsList.add("sun");
    }
    ActionMessages errors=new ActionMessages();

    if(InterUser.CheckUser(db,name,psw))
    {

                          session.setAttribute(Constants.DEP_LIST_KEY,Dep.Search(new DB()));//注意Dep.Search()的参数改成这样就行了

    session.setAttribute(Constants.LOGIN_USERNAME_KEY,name);


    PageForward="toEmployeeMain";
    }
    else
    {
    errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("errors.accessDeny"));
    if(!errors.isEmpty())
    {
    saveErrors(request,errors);
    }
    PageForward="toWrong";
    }
    db.close();
    return mapping.findForward(PageForward);
    }
      

  17.   

    看到了么~?个人觉得很古怪的问题~是不是和JAVA中的栈内存和堆内存有关系呢~?
    我建了个群~,希望有能力而且热心的兄弟加入~大家一起讨论会好很多的~
    潜水的就别加了~谁都不希望自己提问题没人响应~
    不懂可以大家讨论一起提高~群号是:15046521
    特别诚邀jshi123和瓜瓜的加入
      

  18.   

    在你加注释的两条语句:
      DB db=new DB();

      session.setAttribute(Constants.DEP_LIST_KEY,Dep.Search(new DB()));
    之间有:
      if(InterUser.CheckUser(db,name,psw))
    上面这条语句中也用到了db。分析下面几点:
    1。在CheckUser 和 Search 方法里都调用了db.OpenSQL
    2。两次调用间connection没有关闭,用的是同一个connection
    3。第二次调用executeQuery将关闭前一次调用返回的结果集所以可能正是上述原因导致执行第二次查询后,前一次的数据就找不到了。
    这个问题应该是与内存无关的。
      

  19.   

    哦~,听起来有点明白啦~呵呵~
    谢谢JSHI123