我有一个数据库表,根据他们的表现记录学生的分数和评论:
| NAME | SCORE | REMARKS |
+--------+-------+---------+
| james | 150.0 | |
| jimmy | 90.0 | |
| josh | 200.0 | |
我想根据学生的分数更新“备注”栏目,如果学生分数低于100,备注文字将为“失败”,从100到160:“平均通过”和161至250“通行证”。
我写了这段代码:
String student_score, remark;
double type;
try {
String sql = "select SCORE from class";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
student_score = rs.getString("SCORE");
type = Double.parseDouble(student_score);
while (rs.next()) {
if (type >= 1 && type <= 100) {
remark = "fail";
String sql1 = "update class set Remarks='" + remark + "'" + " WHERE SCORE='" + type + "'";
pst = conn.prepareStatement(sql1);
pst.execute();
} else if (type >= 101 && type <= 160) {
remark = "average pass";
String sql1 = "update class1 set Remarks='" + remark + "'" + " WHERE SCORE='" + type + "'";
pst = conn.prepareStatement(sql1);
pst.execute();
} else if (type >= 161 && type <= 250) {
remark = "pass";
String sql1 = "update class set Reemarks='" + remark + "'" + " WHERE SCORE='" + type + "'";
pst = conn.prepareStatement(sql1);
pst.execute();
}
}
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
但是有了这个,我只填充了第一行,其余的仍然是空的:
| NAME | SCORE | REMARKS |
+-------+-------+------------+
| james | 150.0 |average pass|
| jimmy | 90.0 | |
| josh |200.0 | |
答案 0 :(得分:1)
移动
student_score = rs.getString("SCORE");
type = Double.parseDouble(student_score);
到while
循环
while (rs.next()) {
student_score = rs.getString("SCORE");
type = Double.parseDouble(student_score);
...
}
答案 1 :(得分:0)
满足您的要求的最简单方法是直接在SQL中完成所有操作:
String sql1 = "update class set Remarks='fail' WHERE SCORE BETWEEN 1 AND 100";
String sql2 = "update class set Remarks='average pass' WHERE SCORE BETWEEN 101 AND 160";
String sql3 = "update class set Remarks='pass' WHERE SCORE BETWEEN 161 AND 250";
// execute the queries
这不是&#34;最佳做法&#34;这样做,但它相当于你的解决方案,同时更紧凑,更容易重构。