public void createStudent(Student student) { Connection conn = null; try { conn = getDatabaseConnection(); String sql = "INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, student.getStudId()); pstmt.setString(2, student.getName()); pstmt.setString(3, student.getEmail()); pstmt.setDate(4, new java.sql.Date(student.getDob().getTime())); pstmt.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if(conn!= null){ try { conn.close(); } catch (SQLException e){ } } } }
public void updateStudent(Student student) { Connection conn = null; try { conn = getDatabaseConnection(); conn = getDatabaseConnection(); String sql = "UPDATE STUDENTS SET NAME=?,EMAIL=?,DOB=? WHERE STUD_ID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, student.getName()); pstmt.setString(2, student.getEmail()); pstmt.setDate(3, new java.sql.Date(student.getDob().getTime())); pstmt.setInt(4, student.getStudId()); pstmt.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e.getCause()); } finally { if(conn!= null){ try { conn.close(); } catch (SQLException e){ } } } }