在JDBC中将列值转换为浮点数

时间:2013-07-18 20:33:09

标签: java sql jdbc

我正在使用带有SQL库的Java来处理我上传到Oracle数据库的电子表格。我计算了与平均分数相差一个标准偏差的学生分数,并使用“getFloat”方法从列中检索浮动值。我这样做的原因是后来使用这些值在if语句中比较它们。但是,当我编译时,我得到一个数据类型不匹配错误。错误发生在以下行中:

float AllScores = selectScore.getFloat("SCORES");
float totalAvg = avgScore.getFloat("SCORE_AVG"); 
float ScoreStDev = sdev.getFloat("standardDeviation");

我知道我收到这些错误是因为我试图从我声明为String的变量中获取浮点值,该变量包含“SELECT”语句。我的问题是,如何从列中检索浮点值以便稍后在我的if语句中进行比较。我的一些代码:

 String selectScore = "SELECT SCORES FROM STUDENTS";
 st.executeQuery(selectScore);

 String avgScore = "SELECT Avg(SCORES) AS SCORE_AVG FROM STUDENTS";
 rs = st.executeQuery(avgScore);
 String sdev = "SELECT STDEV(SCORES) AS standardDeviation FROM STUDENTS";
 st.executeQuery(sdev);

int one = 1;


           //Loop score column
           while(rs.next()){

           //Convert values into float values
           float AllScores = selectScore.getFloat("SCORES");
           float totalAvg = avgScore.getFloat("SCORE_AVG"); 
           float ScoreStDev = sdev.getFloat("standardDeviation");

            //Calculate 1 standard deviation away from mean
           float theSD = AllScores - (one * ScoreStDev);
           }

              if(SCORE > ScoreStDev){
              //Code here

             }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

这是因为您在查询字符串上尝试getFloat而不是从每个ResultSet获取executeQuery()。对于Ex:

String selectScore = "SELECT SCORES FROM STUDENTS";
ResultSet rs = st.executeQuery(selectScore);

....

float AllScores = rs.getFloat("SCORES");