/** * Met à jour un Computer de la base * * @param comp le Computer à mettre à jour */ public void updateComputer(Computer comp) { Logger log = LoggerFactory.getLogger(this.getClass()); Connection connection = null; try { connection = connectionFactory.getConnection(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); log.error("Erreur lors de la demande de connection."); } // ajoutez ici le code d'update d'un Computer String query = "UPDATE computer SET name = ?, introduced = ?, discontinued = ?, company_id = ? WHERE id = ?;"; int results = 0; PreparedStatement pstmt = null; try { pstmt = connection.prepareStatement(query); pstmt.setString(1, comp.getName()); pstmt.setTimestamp(2, new Timestamp(comp.getIntroducedDate().getMillis())); pstmt.setTimestamp(3, new Timestamp(comp.getDiscontinuedDate().getMillis())); if (comp.getCompany().getId() != null) pstmt.setLong(4, comp.getCompany().getId()); else pstmt.setNull(4, Types.NULL); pstmt.setLong(5, comp.getId()); System.out.println("La requete: " + pstmt.toString()); results = pstmt.executeUpdate(); System.out.println("Mis à jour bien effectué..."); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Probleme dans la requete de mis à jour..."); } finally { try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
/** Insert un ordinateur/computer dans la base */ public Long insertComputer(Computer cp) { Logger log = LoggerFactory.getLogger(this.getClass()); Connection connection = null; try { connection = connectionFactory.getConnection(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); log.error("Erreur lors de la demande de connection."); } Long id = null; // ajoutez ici le code d'insertion d'un produit String query = "INSERT INTO computer(name,introduced,discontinued,company_id) VALUES(?,?,?,?);"; int results = 0; PreparedStatement pstmt = null; try { pstmt = connection.prepareStatement(query); pstmt.setString(1, cp.getName()); // pstmt.setTimestamp(2, new Timestamp(cp.getIntroducedDate().getMillis())); pstmt.setDate(2, new java.sql.Date(cp.getIntroducedDate().getMillis())); // pstmt.setTimestamp(3, new Timestamp(cp.getDiscontinuedDate().getMillis())); pstmt.setDate(3, new java.sql.Date(cp.getDiscontinuedDate().getMillis())); if (cp.getCompany().getId() != null) pstmt.setLong(4, cp.getCompany().getId()); else pstmt.setNull(4, Types.NULL); System.out.println("La requete: " + pstmt.toString()); results = pstmt.executeUpdate(); System.out.println("Insertion bien effectué..."); try { // On recupère l'id généré ResultSet rsId = pstmt.getGeneratedKeys(); while (rsId.next()) { id = rsId.getLong(1); } // fermeture de rsId ConnectionFactory.closeObject(rsId); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); System.out.println("Probleme dans la génération des id Computer..."); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Probleme dans la requete d'insertion..."); } finally { try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return id; }