急急急,为什么while只能循环一次?高手帮忙看看吧!

happydaisy1985 2009-03-11 07:03:21
package com.hdpu.dataintime;
import java.io.IOException;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.hdpu.datasources.db.*;
import com.hdpu.utility.*;
import com.hdpu.ui.RequestHandler;
import com.hdpu.ui.*;
public class InTime extends HttpServlet {

Connection conn;
Statement st;
ResultSet rs,rs1;
String color="#000000";
String label_color="#71626C";
String Xmlshow="";

//潮位平均数据


public void init(ServletConfig config) throws ServletException {
//在数据库中校验登陆信息
//checking.......begin............................
try{
BrowseSession browseSession=new BrowseSession();
conn=((DatabaseDataSource)browseSession.getDataSource()).getConnection() ;
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ep) {
System.out.println("Data source Error:"+ep);
}
}
/*

* 处理<GET> 请求方法

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//设置输出信息的格式及字符集

String[] label_zdidshuiwen={"日期:","站点ID:","水温:","盐度:","潮位:"};
String[] label_zdidqixiang={"日期:","站点ID:","气温:","湿度:","气压:","降水:","能见度:","风速:","风向:"};
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control","no-cache");

//创建输出流对象

PrintWriter out = response.getWriter();

//依据验证结果输出不同的数据信息

out.println("<response>");
try{
String sql1="select zdname,zdid from zhandianpeizhi";
rs1=st.executeQuery(sql1);
while(rs1.next())
{
String zdname=rs1.getString("zdname");
String zdid=rs1.getString("zdid");
String sql_zdidshuiwen="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(shuiwen,1),'fm99999.0'),to_char(round(yandu,1),'fm99999.0'),to_char(round(chaowei,1),'fm99999.0') from shuiwenmindata where rq=(select max(rq) from shuiwenmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
String sql_zdidqixiang="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(qiwen,1),'fm99999.0'),to_char(round(shidu,1),'fm99999.0'),to_char(round(qiya,1),'fm99999.0'),to_char(round(jiangshui,1),'fm99990.0'),to_char(round(nengjiandu,1),'fm99999.0'),to_char(round(fengsu,1),'fm99999.0'),fengxiang from qixiangmindata where rq=(select max(rq) from qixiangmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
out.println("<"+zdname+"水文>" + departInTimeInfo(sql_zdidshuiwen,label_zdidshuiwen) + "</"+zdname+"水文>");
out.println("<"+zdname+"气象>" + departInTimeInfo(sql_zdidqixiang,label_zdidqixiang) + "</"+zdname+"气象>");
}

}

catch(Exception ep) {
System.out.println("Data source Error:"+ep);
}
out.println("</response>");

out.close();

}


public String departInTimeInfo(String sql,String[] labelString)
{
try{
rs=st.executeQuery(sql);
if (rs.next())
{
for(int i=1;i<=labelString.length;i++)
{
Xmlshow=Xmlshow+"<font color="+label_color+">"+labelString[i-1]+"</font>"+"<font color="+color+">"+rs.getString(i)+"</font><br>";
}
}
else
{
Xmlshow="没有数据。";
}
}
catch(Exception e) {}


return Xmlshow;

}

public void destroy()
{
try{
if (rs!=null) rs.close();
if (st!=null) st.close();
if (conn!=null) conn.close();
}
catch(Exception e){};
}

}
zhandianpeizhi里有三条记录,可是只显示第一条记录的结果:
- <response>
<三沙水文>没有数据。</三沙水文>
<三沙气象>没有数据。<font color=#71626C>日期:</font><font color=#000000>2009-03-11 18:53:59</font><br><font color=#71626C>站点ID:</font><font color=#000000>400</font><br><font color=#71626C>气温:</font><font color=#000000>14.4</font><br><font color=#71626C>湿度:</font><font color=#000000>78.7</font><br><font color=#71626C>气压:</font><font color=#000000>1018.9</font><br><font color=#71626C>降水:</font><font color=#000000>0.0</font><br><font color=#71626C>能见度:</font><font color=#000000>null</font><br><font color=#71626C>风速:</font><font color=#000000>9.1</font><br><font color=#71626C>风向:</font><font color=#000000>107</font><br></三沙气象>
</response>
帮忙看看怎么回事吧,看了好半天了没找到错误。谢谢大家,帮忙找找问题,分不够可以再加!
...全文
299 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
mac_shen 2009-03-13
  • 打赏
  • 举报
回复
departInTimeInfo这个方法中有这么一段
try{
...
}catch(Exception e){}

当try部分出错的时候,你把异常吃掉了,还能搞啥呢

跟踪一下异常吧,肯定try部分有错
catch(Exception e){
log.error(e);
}
qgmzhfj 2009-03-13
  • 打赏
  • 举报
回复
departInTimeInfo方法里
if (rs.next())
{
for(int i=1;i <=labelString.length;i++)
{
Xmlshow=Xmlshow+"<font color="+label_color+">"+labelString[i-1]+"</font>"+"<font color="+color+">"+rs.getString(i)+"</font><br>";
}
}
else
{
Xmlshow="没有数据。";
}
你已经让Xmlshow = "没有数据。" 了。而且Xmlshow 是个全局变量吧,你还让返回了,看的不是很明白。
zhanshengkui 2009-03-12
  • 打赏
  • 举报
回复
同上,departInTimeInfo方法中到底有没有取得数据,你写的SQL语句太复杂,俺看不懂!
keysilence 2009-03-12
  • 打赏
  • 举报
回复
我认为:
首先,可能原因出现在查询语句上,请仔细调试;
其次,可能是调用departInTimeInfo方法时出了异常,你却没有捕捉异常的执行语句,异常结果被你忽略了。
fanyuanwaifdl 2009-03-12
  • 打赏
  • 举报
回复
关注
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ZiSheng 的回复:]
Java code
while(rs1.next())



在这个循环里边你加个输出,看看是不是循环了三次
System.out.println("**********************************8");
[/Quote]
把out.println(" <"+zdname+"水文>" + departInTimeInfo(sql_zdidshuiwen,label_zdidshuiwen) + " </"+zdname+"水文>");
out.println(" <"+zdname+"气象>" + departInTimeInfo(sql_zdidqixiang,label_zdidqixiang) + " </"+zdname+"气象>");
改为
out.println(" <"+zdname+"水文>" + sql_zdidshuiwen+ " </"+zdname+"水文>");
out.println(" <"+zdname+"气象>" + sql_zdidqixiang+ " </"+zdname+"气象>");
可正常循环三次,问题出在函数 public String departInTimeInfo(String sql,String[] labelString) 上,可是不知道到底哪里出错啦,在这个循环里边你加个输出的话也可以正常循环三次,就是在departInTimeInfo(String sql,String[] labelString)这个函数上卡住了,可是我看不出来问题在哪里!
ZiSheng 2009-03-11
  • 打赏
  • 举报
回复

while(rs1.next())

在这个循环里边你加个输出,看看是不是循环了三次
System.out.println("**********************************8");
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
是不规范,拜托耐心看一下吧!
Sou2012 2009-03-11
  • 打赏
  • 举报
回复
条件,代码一点都不规范
lzt2008 2009-03-11
  • 打赏
  • 举报
回复
拜托整理下再贴
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 flylovejings 的回复:]
String sql_zdidshuiwen="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(shuiwen,1),'fm99999.0'),to_char(round(yandu,1),'fm99999.0'),to_char(round(chaowei,1),'fm99999.0') from shuiwenmindata where rq=(select max(rq) from shuiwenmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
String sql_zdidqixiang="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(qiwen,1),'fm99…
[/Quote]
这两句sql没有问题,因为有很多限制显示精度的函数,看起来比较复杂我在sqlplus里试过啦!
简单一点:
String sql_zdidshuiwen="select rq,zdid,...(字段)....from shuiwenmindata where rq=(select max(rq) from shuiwenmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
String sql_zdidqixiang="select rq,zdid,...(字段)....from qixiangmindata where rq=(select max(rq) from qixiangmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
就是取对应最新时间的那条记录!
zdid是从数据库取的字段值。
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
补充一点:这个java文件已经编译成class了,编译过程中没出现问题
flylovejings 2009-03-11
  • 打赏
  • 举报
回复
String sql_zdidshuiwen="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(shuiwen,1),'fm99999.0'),to_char(round(yandu,1),'fm99999.0'),to_char(round(chaowei,1),'fm99999.0') from shuiwenmindata where rq=(select max(rq) from shuiwenmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";
String sql_zdidqixiang="select to_char(rq,'yyyy-mm-dd hh24:mi:ss'),zdid,to_char(round(qiwen,1),'fm99999.0'),to_char(round(shidu,1),'fm99999.0'),to_char(round(qiya,1),'fm99999.0'),to_char(round(jiangshui,1),'fm99990.0'),to_char(round(nengjiandu,1),'fm99999.0'),to_char(round(fengsu,1),'fm99999.0'),fengxiang from qixiangmindata where rq=(select max(rq) from qixiangmindata where zdid='"+zdid+"') and zdid='"+zdid+"'";

这两条语句我没看太清,有点乱!你排好!顺便把关键地方注释加上!!

你先debug下,
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 guoapeng 的回复:]
debug 会吗? debug一下不就知道了,关于debug的文章可以在网上找
[/Quote]
没用过debug,给推荐篇文章看看吧!
昔日海贼 2009-03-11
  • 打赏
  • 举报
回复
你自己调试一下吧
在这上面不怎么好找
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
执行过了,可以得到记录的!
zidasine 2009-03-11
  • 打赏
  • 举报
回复
把你拼接的sql语句sql_zdidqixiang 拿去执行下看能得到记录吗
guoapeng 2009-03-11
  • 打赏
  • 举报
回复
debug 会吗? debug一下不就知道了,关于debug的文章可以在网上找
happydaisy1985 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tiantianzzz 的回复:]
太乱了 头痛- -
[/Quote]
看似复杂,其实挺简单的,大家耐心帮忙看一下吧,我看了很长时间了找不出错误,正着急呢!
tiantianzzz 2009-03-11
  • 打赏
  • 举报
回复
太乱了 头痛- -
加载更多回复(1)

81,092

社区成员

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

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