java—我想将多个浮点值存储到数组中,从mysql数据库检索,但它不存储所有值

gdx19jrr  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(372)

我想将多个浮点值存储到数组中,从mysql数据库检索,但它不存储所有值。它只显示第一个值,即数组[0]。当我在做的时候 System.out.println("bb"+rs2.getFloat(1)); 它正确地显示所有值。所以请帮帮我。我正在下面粘贴代码。

try{ 
            conn = DB_connection.ConnectDB();

            String s = (String) combocompound.getSelectedItem().toString();
            String s1 = (String) comboelement1.getSelectedItem().toString();
            String s2 = (String) comboelement2.getSelectedItem().toString();

            String sql1 = "SELECT energy FROM compound_data WHERE compound_name=\""+s+"\"";   
            System.out.println("======="+sql1);

            // R (compound)
            String sql2 = "SELECT Log_R FROM compound_data WHERE compound_name=\""+s+"\"";     
            // R1 (element)
            String sql3 = "SELECT Log_R FROM element_type,element_data WHERE element_type.element_id = element_data.element_id AND element_type.element_name=\""+s1+"\""; 
            // R2 (element)
            String sql4 = "SELECT Log_R FROM element_type,element_data WHERE element_type.element_id = element_data.element_id AND element_type.element_name=\""+s2+"\"";                      

            pst1 =conn.prepareStatement(sql1);      
            rs1 = pst1.executeQuery();
            rs1.last();
            int count = rs1.getRow();
            rs1.beforeFirst();
            System.out.println("rowsFirst"+count);

            System.out.println("Upto date: "+rs1.next());
            while (rs1.next()) {
              //System.out.println("aa"+rs1.getFloat(1));
                 for (int i=0; i<count; i++)
                             {
                                 data1[i] = rs1.getFloat(1);

                           }                       
            } 

            pst2 =conn.prepareStatement(sql2);
            rs2 = pst2.executeQuery();
            //int numRow2 = rs2.getRow();
            while (rs2.next()) {
                System.out.println("bb"+rs2.getFloat(1));
                    for (int i=0; i < count; i++)
                             {
                                   data2[i] = rs2.getFloat(1);
                                   //System.out.println("lkjh"+data2[i]);  
                             }      

            }  
            for (int i=0; i < count; i++)
                             {
                                  // data2[i] = rs2.getFloat(1);
                                   System.out.println(i+": "+data1[i]);  
                                   System.out.println(i+": "+data2[i]);  
                             }  
            pst3 =conn.prepareStatement(sql3);
            rs3 = pst3.executeQuery();
            int numRow3 = rs3.getRow();
            while (rs3.next()) {
                System.out.println("aa"+rs3.getFloat(1));
                    for (int i=1; i <= numRow3; i++)
                             {
                                   data3[i] = rs3.getFloat(i);
                                   //System.out.println(data3[i]);
                             }                         
            }  
            pst4 =conn.prepareStatement(sql4);
            rs4 = pst4.executeQuery();
            int numRow4 = rs4.getRow();
            while (rs4.next()) {
                System.out.println("bb"+rs4.getFloat(1));
                    for (int i=1; i <= numRow4; i++) 
                             {
                                   data4[i] = rs4.getFloat(i);
                                  // System.out.println(data4[i]);
                             }                         
            }  
        } catch(SQLException e){
            System.out.println(e);   
          }

我将这样显示输出

run:
        =======SELECT energy FROM compound_data WHERE compound_name="N_Acetyl_L_tryptophan"
        rowsFirst25
        Upto date: true
        bb-0.757867
        bb-0.442622
        bb-0.162836 
        bb-0.0704817
        bb-0.0355563
        bb-0.0203436
        bb-0.00824214
        bb-0.00418533
        bb-0.00123731
        bb-3.37054E-4
        bb-3.86899E-4
        bb0.0
        bb0.0
        bb-5.1072E-5
        bb0.0
        bb0.0
        bb-6.35631E-4
        bb-0.00315717
        bb-0.0113418
        bb-0.0221486
        bb-0.0345976
        bb-0.0478736
        bb-0.0755661
        bb-0.10401
        bb-0.171984
        0: 15.0
        0: -0.171984
        1: 15.0
        1: -0.171984
        2: 15.0
        2: -0.171984
        3: 15.0
        3: -0.171984
        4: 15.0
        4: -0.171984
        5: 15.0
        5: -0.171984
        6: 15.0
        6: -0.171984
        7: 15.0
        7: -0.171984
        8: 15.0
        8: -0.171984
        9: 15.0
        9: -0.171984
        10: 15.0
        10: -0.171984
        11: 15.0
        11: -0.171984
        12: 15.0
        12: -0.171984
        13: 15.0
        13: -0.171984
        14: 15.0
        14: -0.171984
        15: 15.0
        15: -0.171984
        16: 15.0
        16: -0.171984
        17: 15.0
        17: -0.171984
        18: 15.0
        18: -0.171984
        19: 15.0
        19: -0.171984
        20: 15.0
        20: -0.171984
        21: 15.0
        21: -0.171984
        22: 15.0
        22: -0.171984
        23: 15.0
        23: -0.171984
        24: 15.0
        24: -0.171984
whlutmcx

whlutmcx1#

尝试以下操作:

int count = 0

然后在while循环中执行以下操作:

while(rs1.next())
{
    data1[count] = rs1.getFloat(1);
    count++;
}
count = 0;

试着一直这样做

j0pj023g

j0pj023g2#

原因是你要一次又一次地给同一个索引赋值。
所以打印最后一个赋值结果;

while (rs1.next()) {
    //System.out.println("aa"+rs1.getFloat(1));
    for (int i=0; i<count; i++)
    {
      data1[i] = rs1.getFloat(1); // every time data[0] = rs1.getFloat(1) 

    }                       
}

尝试用以下代码更改代码

int i = 0;

while (rs1.next()) {
    //System.out.println("aa"+rs1.getFloat(1));
   data1[i] = rs1.getFloat(1);
   i++;

}

相关问题