如何使用循环从数据库(Mysql)一个接一个地检索行(值)

时间:2018-04-14 19:46:09

标签: java jdbc

int score;
sql = "SELECT score FROM scores";
rs = stmt.executeQuery(sql);
int score;

while (rs.next()) {
    for (int i = 0; i < al.getNumOfDepartments(); i++) {
        Department a = al.getDepartment(i);

        for (int j = 0; j < jl.getNumOfJudges(); j++) {
            score = rs.getInt("score");
            Judge member = jl.getJudge(j);
            Score s = new Score(score, member);
            a.addScore(s);
        }
    }
}

需要关于此代码的帮助,我正在尝试将评委(在这种情况下为3名评委)给出的分数分配给部门,分数存储在数据库中。问题是一个法官给每个部门分三次得分,但不应该是这样。事实上,每位法官都可以为一个部门分配一个分数。这是一个纯粹的嵌套循环问题,我想,但是我只是一个工程师学生,即使不是专门从事计算机科学,我也无法理解我的自我。感谢帮助!

所以我应该每次检索一行而不是它。

这是代码的其余部分

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Afdeling;
import model.AfdelingenLijst;
import model.JuryLedenLijst;
import model.JuryLid;
import model.Score;
public class afdelingenJuryledenScoresLezen implements ActionListener {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/Audit";
    static final String USER = "root";
    static final String PASS = "root";
     AfdelingenLijst al;
     JuryLedenLijst jl;
     JuryLid lid;

     public afdelingenJuryledenScoresLezen(AfdelingenLijst al, JuryLedenLijst jl) 
        {
         this.al = al;
         this.jl=jl;
        }
            public void actionPerformed(ActionEvent e){


        Connection conn = null;
        Statement stmt = null;

        try{

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT ID, Afdeling FROM Afdelingen";
            ResultSet rs = stmt.executeQuery(sql);
            int ID;
            String naam;
            while (rs.next())
             {
                ID = rs.getInt("ID");
                naam = rs.getString("Afdeling");
                Afdeling a = new Afdeling(ID, naam);
                al.voegAfdelingToe(a);
             }


            sql = "SELECT ID, Jurylid FROM Juryleden";
            rs = stmt.executeQuery(sql);
            int id;
            String jurylid;
            while (rs.next())
             {
                id = rs.getInt("ID");
                jurylid = rs.getString("Jurylid");
                JuryLid lid = new JuryLid(id, jurylid);
                jl.voegJuryLidToe(lid);
             }
            sql = "SELECT score FROM scores";
            rs = stmt.executeQuery(sql);
            int score;
            while (rs.next()){
            for (int i=0; i<al.getAantalAfdelingen(); i++){
                        Afdeling a = al.getAfdeling(i);
                        for (int j=0; j<jl.getAantalJuryLeden(); j++)
                        {
                        score= rs.getInt("score");
                        JuryLid lid = jl.getJuryLid(j);
                        Score s = new Score(score, lid);
                        a.voegScoreToe(s);
                    }
                }
            }

            al.setAfdelingen(al.getAfdelingen());

            rs.beforeFirst();

            rs.close();
            stmt.close();
            conn.close();

            if (stmt != null)
                stmt.close();

            if (conn != null)
                conn.close();

            System.out.println("\nGoodbye!");
         }
         catch (Exception ex) {}
        }
      }

0 个答案:

没有答案