Reputation: 89
So I'm working on my project, I wanted to pass the changes through it and update two columns on a row.
I asked the user for their input
System.out.println("Which one of the reviews you would like to change?");
List<Student> studentList = studentDao.getStudentRatings();
for (Student students : studentList) {
System.out.println(students);
}
System.out.println("Enter your student ID");
int student_idnumber = input.nextInt();
input.nextLine();
System.out.println("Enter your first name");
String firstname = input.nextLine();
System.out.println("Enter your last name");
String lastname = input.nextLine();
System.out.println("Enter your new rating");
int student_ratingnumber = input.nextInt();
input.nextLine();
System.out.println("Enter your new comments");
String comment = input.nextLine();
studentDao.changeReview(student_idnumber, firstname,lastname, student_ratingnumber,comment);
then I pass these onto my studentDao
public void changeReview(int student_idnumber, String firstname, String lastname, int student_ratingnumber,
String comment) {
try(Connection connection = ConnectionUtil.getConnection()){
String sqlString = "UPDATE student_things SET student_comments = ? , student_rating = ? where student_id = ?;";
PreparedStatement preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setInt(1, student_idnumber);
preparedStatement.setString(2, firstname);
preparedStatement.setString(3, lastname);
preparedStatement.setString(5, comment);
preparedStatement.setInt(6, student_ratingnumber);
preparedStatement.executeUpdate();
System.out.println("Review was updated");
}
catch (SQLException e) {
System.out.println("Change review failed");
}
But then it fails every single time, as it would not update the table. I tried to run the same commands in postgres and the commands works, I tried to change the parameters for my preparedstatement. Not sure what I am doing wrong.
Upvotes: 1
Views: 49
Reputation: 856
The order of the parameters must match The order of The question marks. Especially shoild your student_id be last.
Upvotes: 2