简体   繁体   English

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

[英]i want to store multiple float values into array,retrieved from mysql database but it doesn't store all values

i want to store multiple float values into array,retrieved from mysql database but it doesn't store all values. 我想将多个float值存储到数组中,从mysql数据库中检索到它,但是它不存储所有值。 it shows only first value iearray[0] to all. 它只显示所有的第一个值iearray [0]。 while i'm doing System.out.println("bb"+rs2.getFloat(1)); 当我在做System.out.println("bb"+rs2.getFloat(1)); it shows all values correctlly. 它正确显示所有值。 so kindly help me. 请帮助我。 i'm pasting my code below. 我在下面粘贴我的代码。

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);   
          }

and i will shows output like this 我将显示这样的输出

 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            

try something like this : 尝试这样的事情:

int count = 0

Then in while loop do like this : 然后在while循环中这样做:

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

Try doing this for all while loops 尝试在所有while循环中这样做

Reason is you are going to assign value to same index again and again. 原因是您要一次又一次将值分配给相同的索引。

So it print last assigning value as result; 因此,它将最后分配的值作为结果打印出来;

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) 

    }                       
} 

try to change your code with following 尝试通过以下方式更改代码

int i = 0;

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

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM