无法解析为变量 - JDBC

时间:2013-07-24 18:45:26

标签: java sql jdbc

我正在使用Java和SQL库来处理Oracle数据库。我正在尝试计算平均值与平均值相差一个标准差的学生,添加一列来标记这些学生,并计算有多少学生符合标准并将其添加到新表中。我收到一条错误,指出“allAvgs”变量和“theSD”变量“无法解析为变量”。错误发生在我的if语句中,但我已经实例化了这些变量。为什么我会收到此错误?任何帮助表示赞赏。

try{
         Statement stOne, stTwo, stThree, stFour;
         String SelectAverage = "SELECT MEAN FROM STUDENTS";
          ResultSet rsOne = stOne.executeQuery(SelectAverage);

    String TotalAverage = "SELECT Avg(MEAN) AS averages FROM STUDENTS";
          ResultSet rsTwo = stTwo.executeQuery(TotalAverage);     

          String student_stan_dev = "SELECT STDEV(MEAN) AS standardDeviation FROM STUDENTS";
          ResultSet rsThree = stThree.executeQuery(student_stan_dev);

           int onesdMean = 1;


           //Loop Duration_Sec column
           while(rsOne.next()){

           //Convert values into float values
           float allAvgs = rsOne.getFloat("MEAN");
           float totalAvg = rsTwo.getFloat("averages"); 
           float StDev = rsThree.getFloat("standardDeviation");

           float theSD = allAvgs - (onesdMean * StDev);
           }

        String flaggedStudents = "ALTER TABLE STUDENTS ADD FlaggedStudents INT";
        ResultSet rsFour = stFour.executeUpdate(flaggedStudents);

           if(allAvgs >= theSD){
               String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
               st.executeUpdate(FlagHint);
           }

        String countInstances = "SELECT STUDENTS.NAME, STUDENTS.FlaggedStudents" +
                "COUNT(*)OVER(PARTITION BY STUDENTS) AS cnt FROM STUDENTS";
           st.executeQuery(countInstances);


        st.executeUpdate("CREATE TABLE IF NOT EXISTS StudentCount" +
        "(NAME INT , cnt INT)");

        String insertVals = String.format("INSERT INTO StudentCount" +
                "(NAME , cnt INT") +
                " VALUES ('%s','%s')");

        st.execute(insertVals);enter code here

2 个答案:

答案 0 :(得分:1)

因为它们是在while(){...}循环内声明的,并且仅限于while()循环的块范围。您需要在循环外声明它们以扩展其范围。

// you need to declare the variables outside
while(rsOne.next()){
    // this defines a scope , anything declared here is invisible outside
    // this scope

     float allAvgs = rsOne.getFloat("MEAN");
     float totalAvg = rsTwo.getFloat("averages"); 
     float StDev = rsThree.getFloat("standardDeviation");
     float theSD = allAvgs - (onesdMean * StDev);
}
......
......
// compiler has no idea about allAvgs and theSD here
if(allAvgs >= theSD){
    String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
    st.executeUpdate(FlagHint);
}

答案 1 :(得分:0)

您已在theSD循环块内声明while

//Loop Duration_Sec column
while(rsOne.next()){

    ...
    float theSD = allAvgs - (onesdMean * StDev);
}

但尝试在它之后立即使用它。

...

if(allAvgs >= theSD){ // theSD doesn't exist in this scope
    String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
    st.executeUpdate(FlagHint);
}

它不存在于while循环范围之外。您需要在while循环之外声明(并初始化)它(以及您需要的所有其他内容)。 考虑如果while循环评估为false并且未运行会发生什么。