Java 从未显示正确数据的文本文件更新数据库表
当尝试从eclipse和文本文件更新数据库表时,使用MySQL语句不会将数字放在正确的列中。它会更新记录,但不会更新想要的结果,因此我只能假设它在setInt或类似的参数中。感谢您的帮助Java 从未显示正确数据的文本文件更新数据库表,java,sql,database,eclipse,text-files,Java,Sql,Database,Eclipse,Text Files,当尝试从eclipse和文本文件更新数据库表时,使用MySQL语句不会将数字放在正确的列中。它会更新记录,但不会更新想要的结果,因此我只能假设它在setInt或类似的参数中。感谢您的帮助 String query; PreparedStatement statement; query = "UPDATE matches SET home_scores_tries = ?, away_scores_tries = ?, home_penalti
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;
}
}
}
在我看来,“匹配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])));
但是,您只需解析数据,并按照解析数据的顺序创建分数
对象
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”。您可以与它的getter和setter共享Scores类吗?是否已更新代码以包括getter和setter现在。它会更新记录,但不会更新所需的结果
——那么发生了什么事情,您希望看到什么?
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])));
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)