我正在使用准备好的语句插入数据库。我插入的一些值是NULL,因为尚未进行比赛,因此总分是NULL,NULL。
void insertFixtures(List<String[]> fixtures) throws SQLException{
String query = "REPLACE INTO games (team1_id, team2_id, score1, score2, created_at, winner) VALUES (? ,?, ?, ?, ?, ?)";
Connection con = DBConnector.connect();
PreparedStatement stmt = con.prepareStatement(query);
for (String[] s : fixtures) {
try{
stmt.setString(1,s[0]);
stmt.setString(2,s[1]);
Integer score1 = s[2] != null ? Integer.parseInt(s[2]) : null;
Integer score2 = s[3] != null ? Integer.parseInt(s[3]) : null;
stmt.setInt(3, score1);
stmt.setInt(4, score2);
stmt.setString(5,s[4]);
String date = s[4];
int winner;
int dateValue = Integer.parseInt(date);
if (score1 > score2 && dateValue != 0) {
winner = 1;
} else if (score2 > score1 && dateValue != 0) {
winner = 2;
} else if(dateValue != 0) {
winner = 0;
} else {
winner = 3;
}
String gameWinner = Integer.toString(winner);
} catch (NumberFormatException e) {
e.printStackTrace();
}
stmt.execute();
}
stmt.close();
con.close();
}
InsertFixtures获取列表字符串数组,并使用for循环将其插入到我的数据库中。 我遇到的问题是:
This is the print stack trace of the NullPointerException i get.
java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at footballresults.MashapeClient.insertFixtures(MashapeClient.java:130)
at footballresults.MashapeClient.main(MashapeClient.java:190)
第130行是代码:
Integer score1 = s[2] != null ? Integer.parseInt(s[2]) : null;
在这里,我试图将数组中的字符串解析为整数,除非它等于null我希望将其作为null插入数据库。由于所玩游戏的dateValue为0,因此if语句不应将其移到获胜者中。
这是我尝试插入我的数据库中的字符串的示例:
Fixture 45 42 1 0 1554642300
Fixture 49 48 null null 0
任何帮助都是有用的。 谢谢