0
JavaとMySQLでCRUDアプリケーションを作成しましたが、更新されません。CRUDアプリケーションで更新方法が更新されない
StudentManager.displayAllStudents();
すべての学生を表示します。 INTstudentId = 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);
}
}
'それは学生を更新すべきですが何らかの理由でそれが実際には' Student'オブジェクトを更新してはいけません。それはその学生の特定のデータベースレコードを更新するはずです。 DBは更新されますか?あなたは何を期待していたのですか? –
エラーが発生しましたか?あなたはあなたのレコードをデータベースでチェックしましたか? –
getRowを使って、私は更新したいと思う学生のIDを取得します.....その後、私はStudentManager.update(student)を呼び出すときに新しい名字、姓およびその他のレコードを挿入し、最後に挿入します。 getRowメソッドで選択したIDでStudentを更新する必要があります。 – Nemus