从文本文件更新数据库表未显示正确的数据

时间:2017-04-25 14:51:06

标签: java sql database eclipse text-files

尝试从eclipse和文本文件更新数据库表时,使用我的SQL语句不会将数字放在正确的列中。它会更新记录但不会更新所需的结果,所以我只能假设它在setInt或类似的参数中。任何帮助表示赞赏。

        String query;
        PreparedStatement statement;


        query = "UPDATE matches SET home_scores_tries = ?, away_scores_tries = ?,  home_penalties = ?, away_penalties = ?, home_conversion = ?, away_conversion = ? WHERE match_id = ?";


        statement = con.prepareStatement(query);

        ArrayList<Scores> listScores = getListScoresFromTextFile("/Users/Ashley/Documents/workspace/Programming Group Project/src/FileUpload/Round_1.txt");

        for(int i = 0; i<listScores.size(); i++){


            statement.setInt(1, listScores.get(i).getHome_scores_tries());
            statement.setInt(2, listScores.get(i).getAway_scores_tries());
            statement.setInt(3, listScores.get(i).getHome_penalties());
            statement.setInt(4, listScores.get(i).getAway_penalties());
            statement.setInt(5, listScores.get(i).getHome_conversion());
            statement.setInt(6, listScores.get(i).getAway_conversion());
            statement.setInt(7, listScores.get(i).getMatch_id());





            statement.executeUpdate();
            System.out.println("Insert Success");

        }





    } catch (Exception err) {
        System.out.println(err.getMessage());
    }

}

public static ArrayList<Scores> getListScoresFromTextFile(String filePath) {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    ArrayList<Scores> listScores = new ArrayList<Scores>();
    try {
        fis = new FileInputStream(filePath);
        isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);

        String line = null;

        String[] strScores = null;

        // loop and get all data from text file

        while (true) {
            line = br.readLine();
            // check line empty, exit loop
            if (line == null) {
                break;
            } else {
                strScores = line.split(",");
                listScores.add(new Scores (Integer.parseInt(strScores[0]),Integer.parseInt(strScores[1]),
                        Integer.parseInt(strScores[2]),Integer.parseInt(strScores[3]),Integer.parseInt(strScores[4]),
                        Integer.parseInt(strScores[5]),Integer.parseInt(strScores[6])));

            }
        }
    } catch (Exception e) {
        System.out.println("Read File Error");
        e.printStackTrace();
    } finally {

        try {
            br.close();
            isr.close();
            fis.close();
        } catch (IOException e) {

        }

    }
    return listScores;

}
}

和txt文件如下

1,2,3,4,5,6,1
1,2,3,4,5,6,2
1,2,3,4,5,6,3  
1,2,3,4,5,6,4
1,2,3,4,5,6,5
1,2,3,4,5,6,6
1,2,3,4,5,6,7
1,2,3,4,5,6,8
1,2,3,4,5,6,9
1,2,3,4,5,6,10
1,2,3,4,5,6,11
1,2,3,4,5,6,12
1,2,3,4,5,6,13
1,2,3,4,5,6,14
1,2,3,4,5,6,15


public class Scores {


private int match_id;
private int home_scores_tries;
private int home_penalties;
private int home_conversion;
private int away_scores_tries;
private int away_penalties;
private int away_conversion;
/**
 * @return the match_id
 */
public int getMatch_id() {
    return match_id;
}
/**
 * @param match_id the match_id to set
 */
public void setMatch_id(int match_id) {
    this.match_id = match_id;
}
/**
 * @return the home_scores_tries
 */
public int getHome_scores_tries() {
    return home_scores_tries;
}
/**
 * @param home_scores_tries the home_scores_tries to set
 */
public void setHome_scores_tries(int home_scores_tries) {
    this.home_scores_tries = home_scores_tries;
}
/**
 * @return the home_penalties
 */
public int getHome_penalties() {
    return home_penalties;
}
/**
 * @param home_penalties the home_penalties to set
 */
public void setHome_penalties(int home_penalties) {
    this.home_penalties = home_penalties;
}
/**
 * @return the home_conversion
 */
public int getHome_conversion() {
    return home_conversion;
}
/**
 * @param home_conversion the home_conversion to set
 */
public void setHome_conversion(int home_conversion) {
    this.home_conversion = home_conversion;
}
/**
 * @return the away_scores_tries
 */
public int getAway_scores_tries() {
    return away_scores_tries;
}
/**
 * @param away_scores_tries the away_scores_tries to set
 */
public void setAway_scores_tries(int away_scores_tries) {
    this.away_scores_tries = away_scores_tries;
}
/**
 * @return the away_penalties
 */
public int getAway_penalties() {
    return away_penalties;
}
/**
 * @param away_penalties the away_penalties to set
 */
public void setAway_penalties(int away_penalties) {
    this.away_penalties = away_penalties;
}
/**
 * @return the away_conversion
 */
public int getAway_conversion() {
    return away_conversion;
}
/**
 * @param away_conversion the away_conversion to set
 */
public void setAway_conversion(int away_conversion) {
    this.away_conversion = away_conversion;
}
public Scores(int match_id, int home_scores_tries, int home_penalties, int home_conversion, int away_scores_tries,
        int away_penalties, int away_conversion) {
    super();
    this.match_id = match_id;
    this.home_scores_tries = home_scores_tries;
    this.home_penalties = home_penalties;
    this.home_conversion = home_conversion;
    this.away_scores_tries = away_scores_tries;
    this.away_penalties = away_penalties;
    this.away_conversion = away_conversion;
}

}

This is the result from the database table

1 个答案:

答案 0 :(得分:0)

1,2,3,4,5,6,13
1,2,3,4,5,6,14
1,2,3,4,5,6,15

在我看来,“匹配ID”是数据中的最后一列。

listScores.add(new Scores (Integer.parseInt(strScores[0]),
    Integer.parseInt(strScores[1]),
    Integer.parseInt(strScores[2]),
    Integer.parseInt(strScores[3]),
    Integer.parseInt(strScores[4]),
    Integer.parseInt(strScores[5]),
    Integer.parseInt(strScores[6])));

但您只需解析数据并按解析数据的顺序创建Scores对象。

public Scores(int match_id, 
    int home_scores_tries, 
    int home_penalties, 
    int home_conversion, 
    int away_scores_tries, 
    int away_penalties, 
    int away_conversion)

问题是你的Scores对象期望第一个值是“匹配ID”。