Java Question ResultSet.getDouble gives 4179.0 instead of the expected 4179

Discussion in 'Libraries developers questions' started by DonManfred, Jan 23, 2015.

  1. DonManfred

    DonManfred Expert Licensed User

    Code:
    ResultSet rs = st.executeQuery(query);
    ResultSetMetaData rsmd = rs.getMetaData();
    [..]
    while(rs.next()) {
      Recordcount = Recordcount+
    1;
      
    String csv = "[";
      int numColumns = rsmd.getColumnCount();
      
    for (int i=1; i<numColumns+1; i++) {
        
    if (i!=1){
            csv = csv+
    ",";                                  
        
    }

        String column_name = rsmd.getColumnName(i);
        //app.Log("col:"+column_name+"/"+rsmd.getPrecision(i)+"/"+rsmd.getScale(i));                              
      if(rsmd.getColumnType(i)==java.sql.Types.ARRAY){
          csv = csv+rs.getArray(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.BIGINT){
          csv = csv+rs.getInt(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.BOOLEAN){
          csv = csv+rs.getBoolean(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.BLOB){
          csv = csv+rs.getBlob(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.DOUBLE){
            app.Log("col:"+column_name+"/"+rs.getDouble(column_name));                              
          csv = csv+rs.getDouble(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.FLOAT){
          csv = csv+rs.getFloat(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.INTEGER){
          csv = csv+rs.getInt(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.NVARCHAR){
          csv = csv+rs.getNString(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.VARCHAR){
          csv = csv+rs.getString(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.TINYINT){
          csv = csv+rs.getInt(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.SMALLINT){
          csv = csv+rs.getInt(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.DATE){
          csv = csv+rs.getDate(column_name);                                  
      } else if(rsmd.getColumnType(i)==java.sql.Types.TIMESTAMP){
          csv = csv+rs.getTimestamp(column_name);                                  
      }else{
          csv = csv+rs.getObject(column_name);                                  
      }
    }
    csv = csv+"]";                                  
    l.Add(csv);
    This code here
    Code:
    } else if(rsmd.getColumnType(i)==java.sql.Types.DOUBLE){
            app.Log("col:"+column_name+"/"+rs.getDouble(column_name));                              
          csv = csv+rs.getDouble(column_name);
    give the following log-output in the b4a ide
    Why does
    Code:
    rs.getDouble(column_name)
    return 4736 instead of 4736.0
     
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    How do you know it returns 4736?
     
  3. DonManfred

    DonManfred Expert Licensed User

    Every Database-tool (phpmyAdmin, HeidiSQL, MySQLworkbench shows 4736 as value for this entry.
    I solved using
    Code:
    BigDecimal bd = rs.getBigDecimal(i);
    //app.Log(
    "col:"+column_name+"/"+ bd.toString());
    csv = csv+ bd.toString();
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice