使用Java / SQL基于值生成新表

时间:2013-07-05 15:07:53

标签: java sql database

我将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
}    

我是处理数据库的新手。我不确定如何生成一个新表格,其中包含符合要求的学生的姓名。我将不胜感激任何帮助!

1 个答案:

答案 0 :(得分: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);