数据库用的是mysql,里面有个表temperature,表里有wd_1、wd_2、wd_3、、、、、、、wd_25、共25个字段,每个字段里面都有一个数值,有的可能为空,现在如何用循环来读取这些所有字段的值,读完以后加一块赋给wd.代码像下面这样:sql="select * from temperature";  //查询temperature表的SQL语句
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
if(rs.next()){
wd=rs.getString("循环语句");不知道我这个思路对不对,就是把这25个字段的值全部读出来加一起变成一个数组,如果里面有空值的话,用null替代,如24,25,26,29,28,null,null~~

解决方案 »

  1.   

    while(rs.next()){
     wd1=rs.getString("wd_1"); //或者 wd1=rs.getString(1);
     wd2=rs.getString("wd_2"); //或者 wd1=rs.getString(2);
     wd3=rs.getString("wd_3");
     wd4=rs.getString("wd_4"); //...............
     wd25=rs.getString("wd_25");
    }
      

  2.   

    写个for循环。。循环100次。。rs.getString(i)..如果没有列了。。就报错了。。用java捕捉异常就可以了
      

  3.   

    for循环的代码怎么写呢,列数我是知道的,我可以设定i<=25,不知道是不是这样!
      

  4.   

    在你select的时候
    直接select ((case when wd_1 is null then 0 else wd_1)+(case when wd_1 is null then 0 else wd_2)+(case when wd_1 is null then 0 else wd_3)...) as sumwd from temperature
      

  5.   

    select ((case when wd_1 is null then 0 else wd_1)+(case when wd_2 is null then 0 else wd_2)+(case when wd_3 is null then 0 else wd_3)...) as sumwd from temperature
    省略号代表什么,你懂的
      

  6.   

    select ((case when wd_1 is null then 0 else wd_1 end)+(case when wd_1 is null then 0 else wd_2 end)+(case when wd_1 is null then 0 else wd_3 end)...) as sumwd from temperature
      

  7.   

    不可以用循环来写吗,这样select的话 不是崩溃哇
      

  8.   

    sql="select * from temperature";  //查询temperature表的SQL语句
    rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
    String wd = "";
     while (rs.next()){
         for(int i =0;i<25;i++){
          if(rs.getString(i)!= null && rs.getString(i).equals("")){
            wd  = wd + rs.getString(i);
           }else{
            wd  = wd + "null";
           }
         }
      }
    System.out.println(wd);
      

  9.   

    上面改成:if(rs.getString(i)!= null && !rs.getString(i).equals(""))
      

  10.   


            sql="select * from temperature"; //查询temperature表的SQL语句
            rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
            String wd="";  //整点温度
            while (rs.next()){
            for(int i =0;i<25;i++){
            if(rs.getString(i)!= null && !rs.getString(i).equals("")){ //这句报错:org.apache.jasper.JasperException: Exception in JSP: /main.jsp:68        wd = wd + rs.getString(i);
             }else{
              wd = wd + "null";
             }
             }
             }
      

  11.   

    比如说这个wd_6字段下值为空的话,我就录入null~
      

  12.   

    都是数值的话用getInt就行了,都不用判断空,如果为空直接返回的值就是0
    getInt
    int getInt(int columnIndex)
               throws SQLException以 Java 编程语言中 int 的形式检索此 ResultSet 对象的当前行中指定列的值。 参数:
    columnIndex - 第一个列是 1,第二个列是 2,…… 
    返回:
    列值;如果值为 SQL NULL,则返回值为 0 
      

  13.   

    //得到那个表中有多少个字段在弄private static void getColumns(Connection conn, String ATableName)
    throws SQLException {
    DatabaseMetaData dbmd1 = conn.getMetaData();
    ResultSet rs = null;
    rs = dbmd1.getTables(null, null, "%", null); while (rs.next()) {
    String name = rs.getString(3);
    String type = rs.getString(4);
    if (type.equals("TABLE")) {
    System.out.println("table-name:  " + name);
    System.out.println("");
    String sql = "select * from " + name;
    // System.out.println("-----------:" + sql);
    Statement stmt1 = conn.createStatement();
    ResultSet rs1 = stmt1.executeQuery(sql);
    ResultSetMetaData data = rs1.getMetaData();
    for (int i = 1; i <= data.getColumnCount(); i++) {
    // 获得所有列的数目及实际列数
    // int columnCount=data.getColumnCount();
    // 获得指定列的列名
    String columnName = data.getColumnName(i);
    System.out.println("column name :" + columnName);
    // 获得指定列的数据类型名
    String columnTypeName = data.getColumnTypeName(i);
    System.out.println("column Type  name :" + columnTypeName); }
    System.out.println(""); }
    }
      

  14.   

    select ((case when wd_1 is null then 0 else wd_1 end)+(case when wd_1 is null then 0 else wd_2 end)+(case when wd_1 is null then 0 else wd_3 end)...) as sumwd from temperature
    这样写,可以直接获取记录,不需要再做任何循环操作,我觉得这样执行起来会快一些,个人愚见
      

  15.   


    Statement sta= con.createStatement();
                ResultSet rs = sta.executeQuery("select * from temperature");           
                
                while(rs.next())
                {
                    int sum = 0;
                    for (int i = 1; i <= 25; i++)
                    {
                        sum += rs.getInt(i);
                    }
                    System.out.println(sum);
                }
      

  16.   

    放倒一个ResultSet里面,getString()
      

  17.   

    问题解决了,代码如下

            sql="select * from temperature"; //查询温度的SQL语句
            rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
            String wd="";//整点温度
            if (rs.next()) 
            {
                for (int i = 1; i <= 25; i++)
                    {
                        if(i==1) { wd = rs.getString(i);//读取第一个字段是,字符串前面不需要加","号
                        
                        }
                        else  {wd = wd+","+ rs.getString(i);//从第二个字段开始,需要在新添加的字段前加","号
                       }
                       }
                       }

    感谢楼上所有人对我的帮助!