求救!必给分.....

westwood 2001-06-18 03:39:00
以下程序为什么在for循环中加入一个读取数据的语句,这段循环就不能运行下去呀?


<%@ page import="java.sql.*" %>
<%
PreparedStatement stmt=null;
int maxNum=0;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}

catch (Exception E) {
out.println("Unable to load driver.");
}

try {
Connection C = DriverManager.getConnection("jdbc:mysql://localhost/22dd?user=fdf&password=1111&useUnicode=true&characterEncoding=8859_1");
Statement s=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=s.executeQuery("select * from bulletin where mod = 0");

rs.last();
int no_top;
no_top = rs.getRow() + 1;
int i=1;
for (i=1;i<no_top;i++)
{
if (rs.absolute(i))
{
ResultSet thisnum=s.executeQuery("select count(*) from bulletin where mod = " + rs.getInt(1));
if (thisnum.next())
maxNum=thisnum.getInt(1);


%>
<tr align="center">
<td bgcolor="#FFFFFF" width="30">
</td>
<td bgcolor="#B5E7FF" width="30"><img src="images/icon12.gif" width="15" height="15"></td>
<td bgcolor="#FFFFFF"><a href=view.jsp?serial=<%=rs.getInt(1)%>><%=rs.getString(2)%></a></td>
<td bgcolor="#FFFFFF" width="30"><a href=view.jsp?serial=<%=rs.getString(1)%> target=_blank><img src=images/newwin.gif border=0></a></td>
<td bgcolor="#B5E7FF"><a href=mailto:<%=rs.getString(5)%>><%=rs.getString(3)%></a></td>
<td bgcolor="#FFFFFF" width="30"><%=rs.getString(8)%></td>
<td bgcolor="#B5E7FF" width="30"><%=rs.getString(8)%></td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<%
}
else
{
break;
}

}
rs.close();
s.close();
C.close();


}

catch (SQLException E) {
out.println("SQLException: " + E.getMessage());
out.println("SQLState: " + E.getSQLState());
out.println("VendorError: " + E.getErrorCode());
}

%>
...全文
171 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
westwood 2001-06-19
  • 打赏
  • 举报
回复
呵呵,我终于找到了

是Statement s=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
的错误
在for循环中再次调用,要再声明一个
比如Statement d=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
然后,thisnum=d.executeQuery("select count(*) numcount from bulletin where mod = " + rs.getInt(1));
这样就可以了,可是我还是不知道,为什么?什么原因!!!!
westwood 2001-06-19
  • 打赏
  • 举报
回复
好,我先谢了,等你回来,我给你加分,呵呵
mikej 2001-06-19
  • 打赏
  • 举报
回复
ps.一定要时时查看每一个错误,千万不要忽略掉。
mikej 2001-06-19
  • 打赏
  • 举报
回复
是这里出的错误??
if (thisnum.next())
maxNum=thisnum.getInt(1);

应该是0把
这样修改
thisnum=s.executeQuery("select count(*) numcount from bulletin where mod = " + rs.getInt(1));
maxNum=thisnum.getInt("numcount");
如果还是不行就这样调试:
1.把if (thisnum.next())
maxNum=thisnum.getInt(1);
等有关thisnum的语句隐去;看看网页是否正常,
2.申明thisnum看网页是否正常;
3.在sql plus里面执行select count(*) numcount from bulletin where mod =**
看sql语句有没有错误;
4.一般来说前三个应该都不会出错。
4.5 注释掉if (thisnum.next())
maxNum=thisnum.getInt(1);
在后面填入thisnum.close();
看看是否出错
5.检查mod后面跟的rs.getint(1)输出是否有错。
6.检查maxNum是否预先申明过。
7.如果都没有问题了,加入if....
8.还出现原来的问题,我就没招了。
对了,尽快会结果,待会儿我要自习去了。



westwood 2001-06-19
  • 打赏
  • 举报
回复
就是在读出maxNum后,就跳出循环了!(此时,网页仍然显示)
加入thisnum.close()后,错误如下:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\tomcat\work\localhost_8080%2FmyJSP\_0002fbulletin_0002flist_0002ejsplist_jsp_76.java:100: ???? bulletin.thisnum?
thisnum close();
^
C:\tomcat\work\localhost_8080%2FmyJSP\_0002fbulletin_0002flist_0002ejsplist_jsp_76.java:100: ?????
thisnum close();
^
2 ???

at org.apache.jasper.compiler.Compiler.compile(Compiler.java:247)
at org.apache.jasper.runtime.JspServlet.loadJSP(JspServlet.java:413)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:149)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:161)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:261)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:369)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
at org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
at java.lang.Thread.run(Unknown Source)


代码如下:

<html>
<head>
<title>Jsp Bulletin</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<link rel="stylesheet" href="../newyear.css" type="text/css">
</head>

<body topmargin="1">
<%@ include file="top.inc" %>
<table width="740" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#3333CC">
<tr>
<td>
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr align="center">
<td width="30" bgcolor="#55B3FF">状态</td>
<td width="30" bgcolor="#55B3FF">贴图</td>
<td bgcolor="#55B3FF">主题</td>
<td width="30" bgcolor="#55B3FF">新窗</td>
<td bgcolor="#55B3FF">作者</td>
<td width="30" bgcolor="#55B3FF">回复</td>
<td width="30" bgcolor="#55B3FF">人气</td>
<td bgcolor="#55B3FF">最后回复</td>
</tr>

<%@ page import="java.sql.*" %>
<%
PreparedStatement stmt=null;
int maxNum=0;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}

catch (Exception E) {
out.println("Unable to load driver.");
}

try {
Connection C = DriverManager.getConnection("jdbc:mysql://localhost/cardchina?user=ansonding&password=1232126&useUnicode=true&characterEncoding=8859_1");
Statement s=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=s.executeQuery("select * from bulletin where mod = 0");
ResultSet thisnum=null;

rs.last();
int no_top;
no_top = rs.getRow() + 1;
int i=1;
for (i=1;i<no_top;i++)
{
if (rs.absolute(i))
{
thisnum=s.executeQuery("select count(*) from bulletin where mod = " + rs.getInt(1));

if (thisnum.next())
maxNum=thisnum.getInt(1);

%>
<tr align="center">
<td bgcolor="#FFFFFF" width="30">
</td>
<td bgcolor="#B5E7FF" width="30"><img src="images/icon12.gif" width="15" height="15"></td>
<td bgcolor="#FFFFFF"><a href=view.jsp?serial=<%=rs.getInt(1)%>><%=rs.getString(2)%></a></td>
<td bgcolor="#FFFFFF" width="30"><a href=view.jsp?serial=<%=rs.getString(1)%> target=_blank><img src=images/newwin.gif border=0></a></td>
<td bgcolor="#B5E7FF"><a href=mailto:<%=rs.getString(5)%>><%=rs.getString(3)%></a></td>
<td bgcolor="#FFFFFF" width="30"><%=rs.getString(8)%></td>
<td bgcolor="#B5E7FF" width="30"><%=rs.getString(8)%></td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<%
}
else
{
break;
}

}
rs.close();
s.close();
C.close();


}

catch (SQLException E) {
out.println("SQLException: " + E.getMessage());
out.println("SQLState: " + E.getSQLState());
out.println("VendorError: " + E.getErrorCode());
}

%>




</table>
</td>
</tr>
</table>
</body>
</html>






mikej 2001-06-19
  • 打赏
  • 举报
回复
把所有的代码贴出来,包括完整的错误信息,我说的是修改后的那个网页,怎么出现了thisnum.close;这个语句????,看看是不是写成了thisnu.close;这种低级错误,当然那个ResultSet thisnum=null;一定要放到s申明的下面。这个地方肯定有问题,虽然不一定会报错。
westwood 2001-06-19
  • 打赏
  • 举报
回复
mikej,你说的那样我试了试,还是老样子?

不知道,还有什么方法?
mikej 2001-06-19
  • 打赏
  • 举报
回复
哦,原来是一个conn只能对应一个Statement 原来如此,长见识了。
呵呵,以前我都用class把conn 和Statement封装起来,所以要用新的recordset的时候
就new 一个新的class实例,也就相当于新建了一个connect pool,难怪我从来没有遇到过这样的难题。呵呵。
建议今后把这些都用class封装起来,做起网页来就简单多了,看看我的一个jsp吧。
<%@include file="/public/common/Sort.jsp" %>
<% //网页统一变量申明

UserInfo userinfo = new UserInfo();
Security security= new Security();
Display display = new Display();
Sort sort=new Sort();
String pageName;
...
%>
<%@include file="/model/s_css.jsp" %>
<%
pageName=new String("index.jsp");
nextPage=new String ("/search/search.jsp");
redirect=pageName;
try
{
String theme=new String("今日无话题");
strTemp=(String)request.getParameter("themeid");
if (strTemp!=null && !"".equals(strTemp))
{
theme=(String)session.getAttribute(strTemp);
}

%>

<table>
<TR>
<TD>
<FONT color=#0000a0>今日话题 </font>
<%
newsnum=6;
sort.setnewsNum(newsnum);
sort.settheme(theme);
sort.hotTopicforIndex();
theme=sort.gettheme();

%>
<font color=#aa00ff size="3pt"><b><%=theme%></b> </font>
</TD>
</TR>
</table>
<TABLE align=center border=1 borderColor=#eeeeff cellPadding=1 cellSpacing=1 width="98%">

<%


//首页新闻
if ("index.jsp".equals(pageName))
{



%>
<TR borderColor=#ffffff vAlign=bottom>
<%
if (sort.getid()==null || sort.getid().equals(""))
{
%>
<TR borderColor=#ffffff vAlign=bottom>
<TD>
●<A href='./<%=pageName%>' class=a01><%=sort.gettitle()%></A>
</td>
</TR>
<%
}
else
{
do
{
sort.updateInfotopic();
%>
<TR borderColor=#ffffff vAlign=bottom>
<TD >
●<A href='/search/show_info.jsp?info_id=<%=sort.getid()%>' class=a01><%=sort.gettitle()%>(<%=sort.getreg_date()%>)</A>
</td>
</tr>

<%
}while(sort.toNext());

%>

<TR>
<TD align=right>
<% //设置链接
session.removeAttribute("newsnums");
session.setAttribute("keyfortopic",theme);
%>
<A class=a06 href='/search/search.jsp?keyfortopic=on'>更多消息...</A>
</TD>
</TR>
<%

}
//first if ends
}
//如果是子类
else
{

}

%>


</table>


<%
sort.closeDB();
}
catch(Exception ex)
{

}
finally
{
session.removeAttribute("themeid1");
session.removeAttribute("themeid2");
session.removeAttribute("themeid3");
}
%>
所有的connection都封装到Sort里面了,而且Sort class是继承的一个专门的DBConn.class以后要改connect连接参数就在class里面修改就行了,至于今后遇到的一些数据库String的Unicode转换问题也通通可以在DBConn里面统计解决,而且我这样写了jsp以后一举了然,结构很清楚,new 起来也很方便。呵呵,推荐一下。

mikej 2001-06-18
  • 打赏
  • 举报
回复
错了,把ResultSet thisnum;
放在第二重循环最外面就是申明rs的下面,呵呵。
mikej 2001-06-18
  • 打赏
  • 举报
回复
重复定义thisnum,把定义放在for外面
for (i=1;i<no_top;i++)
{
if (rs.absolute(i))
{

<!----错误开始---->
ResultSet thisnum=s.executeQuery("select count(*) from bulletin where mod = " + rs.getInt(1));

if (thisnum.next())
maxNum=thisnum.getInt(1);
<!----结束------>

修改为:
ResultSet thisnum;
for (i=1;i<no_top;i++)
{
if (rs.absolute(i))
{

<!----错误开始---->
thisnum=s.executeQuery("select count(*) from bulletin where mod = " + rs.getInt(1));
if (thisnum.next())
maxNum=thisnum.getInt(1);
<!----结束------>



asper 2001-06-18
  • 打赏
  • 举报
回复
我以前做也碰到过类似的情况,不要共用Statement和Connection 试试
beyondii 2001-06-18
  • 打赏
  • 举报
回复
我还是建议你用两个数据库连接来去这两个记录机,你在试试
westwood 2001-06-18
  • 打赏
  • 举报
回复
嘿嘿,怎么没人管了???
westwood 2001-06-18
  • 打赏
  • 举报
回复
能不能说的清楚点亚!
dhlxmy 2001-06-18
  • 打赏
  • 举报
回复
hehe,你那些get***似乎要按顺序取
westwood 2001-06-18
  • 打赏
  • 举报
回复
mm.mysql.jdbc-2.0pre5!!!!!

kookoo 2001-06-18
  • 打赏
  • 举报
回复
你用的是jdbc2.0吗?
westwood 2001-06-18
  • 打赏
  • 举报
回复
错误代码如下:
如果加入thisnum.close(),会出现
org.apache.jasper.JasperException: Unable to compile class for JSPC:\tomcat\work\localhost_8080%2FmyJSP\_0002fbulletin_0002flist_0002ejsplist_jsp_67.java:99: ????????
thisnum.close;
^
不加入thisnum.close(),就只能读出maxNum就结束了!!!!

<%@ page import="java.sql.*" %>
<%
PreparedStatement stmt=null;
int maxNum=0;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}

catch (Exception E) {
out.println("Unable to load driver.");
}

try {
Connection C = DriverManager.getConnection("jdbc:mysql://localhost/22dd?user=fdf&password=1111&useUnicode=true&characterEncoding=8859_1");
Statement s=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=s.executeQuery("select * from bulletin where mod = 0");

rs.last();
int no_top;
no_top = rs.getRow() + 1;
int i=1;
for (i=1;i<no_top;i++)
{
if (rs.absolute(i))
{

<!----错误开始---->
ResultSet thisnum=s.executeQuery("select count(*) from bulletin where mod = " + rs.getInt(1));
if (thisnum.next())
maxNum=thisnum.getInt(1);
<!----结束------>

%>
beyondii 2001-06-18
  • 打赏
  • 举报
回复
出错信息是什么?我怀疑是不是因为你的第一个resultset没有关闭,又用这个连接去取第二个resultset了,那你试一试rs.close();
kookoo 2001-06-18
  • 打赏
  • 举报
回复
"在for循环中加入一个读取数据的语句",语句是什么?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧