0

我将 Java 与 SQL 以及 Oracle 数据库结合使用来从我上传的电子表格中检索内容。如果我有一列包含每个学生的姓名,并且有一列包含他们的平均值,并且想要获得整个班级的平均值,例如,然后检查单个学生的平均值是否大于班级平均值并将这些在单独的桌子上满足这些标准的学生,我将如何去做?我有一个想法,但我不确定这是否正确。如我错了请纠正我。

//Select student averages so I can get each student's individual score
resultset = st.executeQuery("SELECT Student_Averages FROM table");
//Get the class average 
stavg = "SELECT Avg(Student_Averages)) AS classAverage FROM table";
rs = st.executeQuery(stavg);
//Iterate through their individual scores and store them in a float variable to 
//compare them later.
while(resultset.next()){
    float studentaverage = resultset.getFloat("Student_Averages");
}   
//Store the class average in a float variable            
float classaverage = stavg.getFloat("Student_Averages"); 
//Compare the individual student average to the class average
if(studentaverage >= classaverage){
//Generate new table with student names
}    

我是使用数据库的新手。我不确定如何使用符合要求的学生的姓名生成一个新表。我将不胜感激任何帮助!

4

1 回答 1

0

首先,将此减速移到循环之外

while(resultset.next()){
    float studentaverage = resultset.getFloat("Student_Averages");
} 

第二,既然你做了

stavg = "SELECT Avg(Student_Averages)) AS classAverage FROM table";

你应该改变

float classaverage = stavg.getFloat("Student_Averages");

float classaverage = stavg.getFloat("classAverage");

最后问你的问题:用这个来创建表格

st.executeUpdate("CREATE TABLE IF NOT EXISTS tableName (id INT , studentName VARCHAR(SIZE))");

然后使用它来将数据插入到表中

String insert = String.format("INSERT INTO tableName("id","studentName") VALUES ('%s','%s)" , idOfStudent someStudentName);

st.execute(intsert);
于 2013-07-05T15:26:34.213 回答