当前位置: 技术问答>java相关
一个SERVLET的问题
来源: 互联网 发布时间:2015-11-11
本文导语: LinkedList result=new LinkedList(); String goTo = "/notice.jsp"; ................ ............. ps = con.createStatement(); String sql ="SELECT taname from trainactive where tcid="+Tcid; rs=ps.executeQuery(sql); if (rs!=null) { ...
LinkedList result=new LinkedList();
String goTo = "/notice.jsp";
................
.............
ps = con.createStatement();
String sql ="SELECT taname from trainactive where tcid="+Tcid;
rs=ps.executeQuery(sql);
if (rs!=null)
{
while(rs.next()){
String[] element = new String[1]:
element[0] = rs.getString(1);
System.out.println(element[0]); result.add(element);
}//while
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
}//if
else
{
goTo="delcourse?TCID=Tcid";
}
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
RequestDispatcher rd= request.getRequestDispatcher(goTo);
rd.forward(request,response);
}
问一下大家,我的想法是判断rs是否为空,(rs是从数据库里找到的数据结果集)如果rs不为空,有数据记录,我把这个结果集传到一个JSP(notice.jsp页面中),如果rs为空,则传跳到另一个SERVLET去,但是我调试的结果是不管找不找得到有记录,
它到到第一个JSP页面去了(notivce.jsp)
摆脱各位帮帮忙!!1
String goTo = "/notice.jsp";
................
.............
ps = con.createStatement();
String sql ="SELECT taname from trainactive where tcid="+Tcid;
rs=ps.executeQuery(sql);
if (rs!=null)
{
while(rs.next()){
String[] element = new String[1]:
element[0] = rs.getString(1);
System.out.println(element[0]); result.add(element);
}//while
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
}//if
else
{
goTo="delcourse?TCID=Tcid";
}
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
RequestDispatcher rd= request.getRequestDispatcher(goTo);
rd.forward(request,response);
}
问一下大家,我的想法是判断rs是否为空,(rs是从数据库里找到的数据结果集)如果rs不为空,有数据记录,我把这个结果集传到一个JSP(notice.jsp页面中),如果rs为空,则传跳到另一个SERVLET去,但是我调试的结果是不管找不找得到有记录,
它到到第一个JSP页面去了(notivce.jsp)
摆脱各位帮帮忙!!1
|
你也可以加一个标志在while循环中,如果运行到while中,则修改标志.
然后根据这个标志跳转页面.
然后根据这个标志跳转页面.
|
rs=ps.executeQuery(sql);
rs.last();
int getRow = rs.getRow();
rs.beforeFirst();
if (getRow>0)
{
while(rs.next()){
String[] element = new String[1]:
element[0] = rs.getString(1);
System.out.println(element[0]); result.add(element);
}//while
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
}//if
else
{
goTo="delcourse?TCID=Tcid";
}
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
RequestDispatcher rd= request.getRequestDispatcher(goTo);
rd.forward(request,response);
}
rs.last();
int getRow = rs.getRow();
rs.beforeFirst();
if (getRow>0)
{
while(rs.next()){
String[] element = new String[1]:
element[0] = rs.getString(1);
System.out.println(element[0]); result.add(element);
}//while
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
}//if
else
{
goTo="delcourse?TCID=Tcid";
}
request.setAttribute("TCID",Tcid);
request.setAttribute("TRAINCOURSE",result.iterator());
RequestDispatcher rd= request.getRequestDispatcher(goTo);
rd.forward(request,response);
}
|
double22822(大宝)
的方法是对的
的方法是对的
|
你用rs.next()判断rs是否为空if(rs.next()){}