调用的相关方法如下:
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;
}
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;
}
解决方案 »
- Servlet.service() for servlet dwr-invoker threw exception错误
- xfire client 的aegis绑定问题
- 跪求几本适合程序员适看的英文书籍
- Hibernate在什么情况下要使用open session in view模式,如何实现呢?
- 关于JAVAMAIL技术
- 怎样将一个网页之中。 以至于满足不同的浏览器
- jdbc连db2问题
- 100分相送(EJB问题)
- 单台服务器可支持的最大用户数是如何确定的呢?
- java 用stringbuffer 拼接字符串 windows里\r\n 是换行 那linux里啥是换行呢
- JSP中如何进过SESSION终止时的操作?
- jdk1.5.0中的jdk和jre文件夹有什么区别
select * from department;数据库中有2条记录,
怎么说最后的结果都不应该是0啊~!!拜托各位前辈了~能帮忙调试出来另开帖送分都行~谢谢了~
代码很麻烦~劳烦各位了~
这条语句在JSP中调用<%=session.getAttribute(Constants.LOGIN_USERNAME_KEY)%>
可以提示出来用户名~
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。
<%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><%}%>
把depList.add(dep);改成depList.addElement(dep);
IF()
{
while(rs.next())
}
我做了判断也没用的~
和if(v != null )
的问题其实是在我代码里有的~,不过这样显示不出来异常信息~
只是页面不对~
所以改成这样能直接看出来返回值的异常来~不好意思~这两天可能上不来了~
兄弟们帮忙看看~5/4号回来散分~谢谢大家热心~~
把depList.add(dep);改成depList.addElement(dep);这个也不行哦~
现在我传回来的depList值是0~
不知道怎么回事~
数据库里有2条记录的~没问题~
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再找找其他原因
刚才的代码放在这里
为什么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条信息的
郁闷~
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;
}
}
2. 观察控制台输出,有否异常错误信息提示?
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();
}
}
}
}
在return depList;之前加一条语句:System.out.println("depList's size:" + depList.size());
再运行,在控制台输出窗口就是启动Tomcat时运行的那个CMD窗口中看加的那条语句的结果是几
ok,你看到了可以发短信给我,乐意帮忙,^-^
这个问题经过调试可以解决~,但是却很奇怪~非常奇怪~希望咱一块讨论下~
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);
}
{
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);
}
我建了个群~,希望有能力而且热心的兄弟加入~大家一起讨论会好很多的~
潜水的就别加了~谁都不希望自己提问题没人响应~
不懂可以大家讨论一起提高~群号是:15046521
特别诚邀jshi123和瓜瓜的加入
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将关闭前一次调用返回的结果集所以可能正是上述原因导致执行第二次查询后,前一次的数据就找不到了。
这个问题应该是与内存无关的。
谢谢JSHI123