我的程序提示用户输入一个ID,然后为他们选择的ID输入一个新的金额,当他们输入新的金额时,它会向数据库写一个新行,我想要做的就是当他们输入新的我希望它删除有关用户的现有数据,因此它不会仅使用新值将相同的信息写入数据库。
Player updatedPlayer = null;
System.out.println("Enter the player ID:");
String playerId = FileUtility.getInput().nextLine();
System.out.println("Here are the players");
//theList = loadCampersFromDatabase(theList);
for (Player camper : PlayerDAO.selectAllById(playerId)) {
System.out.println(camper);
System.out.println("Enter the new amount paid");
newAmount = FileUtility.getInput().nextInt();
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String sql = "DELETE FROM Camper WHERE id = ?, INSERT INTO `Camper`(`id`, `firstName`, `lastName`, `parentsName`,`phoneNumber`,`email`,`amountPaid`) "
+ "VALUES (?,?,?,?,?,?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setInt(1, camper.getRegistrationId());
preparedStatement.setString(2, camper.getFirstName());
preparedStatement.setString(3, camper.getLastName());
preparedStatement.setString(4, camper.getParentsName());
preparedStatement.setInt(5, camper.getPhoneNumber());
preparedStatement.setString(6, camper.getEmail());
preparedStatement.setInt(7, MainClass.getNewAmount());
// execute update SQL stetement
preparedStatement.executeUpdate();
System.out.println("Record is updated to DBUSER table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}