2017-03-18 49 views
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); 

    } 

} 
+0

'它应该更新学生,但由于某种原因它不''实际上它不会'更新'学生'对象,它应该更新该学生的特定数据库记录。数据库是否得到更新?你期望发生什么? –

+0

你有没有得到任何错误?你是否在数据库中检查了你的记录,它是否更新? –

+0

使用getRow,我得到我想要更新的学生的ID .....然后插入新的名字,姓氏和其他记录,然后在最后调用StudentManager.update(student);它应该使用getRow方法选择一个id来更新Student。 – Nemus

回答

0

在主类中,我改变了以下....来自:

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); 

对此:

String firstName = InputHelper.getInput("Enter new first name: "); 
    student.setFirstName(firstName); 

    String lastName = InputHelper.getInput("Enter new last name: "); 
    student.setLastName(lastName); 

    Date dob = InputHelper.getDateInput("Enter new date "); 
    student.setDateOfBirth(dob); 

    boolean onBudget = InputHelper.getBooleanInput("Is on budget? "); 
    student.setOnBudget(onBudget); 


    StudentManager.update(student); 

而现在更新方法工作,数据库更新,我可以显示更改。