0
我已經用Java和MySQL製作了CRUD應用程序,但它不會更新。在CRUD應用程序中不更新的更新方法
StudentManager.displayAllStudents();
顯示所有學生。然後用INT
studentId = InputHelper.getIntegerInput("Select row to update: ");
我選擇Sudent的ID我想更新,,然後用
StudentManager.update(student);
它應該更新 學生,但由於某些原因,它不
這是輸入輔助類:
public class InputHelper {
public static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
public static double getDoubleInput(String prompt) {
String input = getInput(prompt);
return Double.parseDouble(input);
}
public static int getIntegerInput(String prompt) {
String input = getInput(prompt);
return Integer.parseInt(input);
}
public static Date getDateInput(String prompt) {
String input = getInput(prompt);
return Date.valueOf(input);
}
public static boolean getBooleanInput(String prompt) {
String input = getInput(prompt);
return Boolean.parseBoolean(input);
}
}
這是學生管理器類:
public class StudentManager {
private static final Student student = new Student();
private static final Connection conn = DBConnectionUtil.getConnection();
private static ResultSet rs = null;
public static Student getRow(int studentId){
String sql = "SELECT * FROM student WHERE id = ?";
try(PreparedStatement ps = conn.prepareStatement(sql);){
ps.setInt(1, studentId);
rs = ps.executeQuery();
if(rs.next()){
student.setStudentId(studentId);
student.setFirstName(rs.getString(2));
student.setLastName(rs.getString(3));
student.setDateOfBirth(rs.getDate(4));
student.setOnBudget(rs.getBoolean(5));
return student;
}else{
System.err.println("No rows found");
return null;
}
} catch (SQLException ex) {
DBConnectionUtil.proccessException(ex);
}finally{
if(rs != null){
try {
rs.close();
} catch (SQLException ex) {
DBConnectionUtil.proccessException(ex);
}
}
}
return null;
}
........
public static void update(Student student){
String sql = "UPDATE student SET firstName = ?, lastName = ?, dateOfBirth = ?, onBudget = ? WHERE id = ?";
try(PreparedStatement ps = conn.prepareStatement(sql);){
ps.setString(1, student.getFirstName());
ps.setString(2, student.getLastName());
ps.setDate(3, student.getDateOfBirth());
ps.setBoolean(4, student.isOnBudget());
ps.setInt(5, student.getStudentId());
int rowsUpdated = ps.executeUpdate();
if(rowsUpdated > 0){
System.out.println("User updated");
}else{
System.err.println("Something went wrong.");
}
} catch (SQLException ex) {
DBConnectionUtil.proccessException(ex);
}
}
}
這是主類:
public class Main {
public static void main(String[] args) throws SQLException {
StudentManager.displayAllStudents();
int studentId = InputHelper.getIntegerInput("Select row to update: ");
Student student = StudentManager.getRow(studentId);
if(student == null){
System.err.println("Row not found");
}
student.setFirstName(InputHelper.getInput("Enter new first name: "));
student.setLastName(InputHelper.getInput("Enter new last name: "));
student.setDateOfBirth(InputHelper.getDateInput("Enter new date of birth: "));
student.setOnBudget(InputHelper.getBooleanInput("Is on budget? "));
StudentManager.update(student);
}
}
'它應該更新學生,但由於某種原因它不''實際上它不會'更新'學生'對象,它應該更新該學生的特定數據庫記錄。數據庫是否得到更新?你期望發生什麼? –
你有沒有得到任何錯誤?你是否在數據庫中檢查了你的記錄,它是否更新? –
使用getRow,我得到我想要更新的學生的ID .....然後插入新的名字,姓氏和其他記錄,然後在最後調用StudentManager.update(student);它應該使用getRow方法選擇一個id來更新Student。 – Nemus