Guest User

Untitled

a guest
Dec 11th, 2019
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package model.dal;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.mysql.jdbc.PreparedStatement;
  6.  
  7. import model.bo.Personne;
  8. import java.sql.*;
  9.  
  10. public abstract class PersonneDAO {
  11.  
  12. public static Connection getConnection() {
  13. Connection connection = null;
  14. // ------------------------------
  15. String url = "jdbc:mysql://localhost:3306/personnedb" + "?autoReconnect=true&useSSL=false";
  16. String user = "root";
  17. String pwd = "";
  18. try {
  19. connection = DriverManager.getConnection(url, user, pwd);
  20. connection.setAutoCommit(false);
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. // ------------------------------
  25. return connection;
  26. }
  27.  
  28. public static ArrayList<Personne> selectAll() {
  29. ArrayList<Personne> liste = new ArrayList<Personne>();
  30. Connection connection = getConnection();
  31. // ------------------------------
  32. String requete = "select * from personne";
  33. try {
  34. Statement state = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  35. ResultSet result = state.executeQuery(requete);
  36. while (result.next()) {
  37. String prenom = result.getString("prenom");
  38. String nom = result.getString("nom");
  39. int id = result.getInt("id");
  40. // System.out.println(prenom);
  41. Personne p = new Personne(nom, prenom, id);
  42. liste.add(p);
  43. }
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. // ------------------------------
  48. return liste;
  49. }
  50. public static Personne selectOne(int id) {
  51. Personne p = new Personne();
  52. String requete = "SELECT * FROM personne WHERE id = ?";
  53. Connection connection = getConnection();
  54. try {
  55. PreparedStatement prepare = (PreparedStatement) connection.prepareStatement(requete);
  56. // je viens binder le 1er ?
  57. prepare.setInt(1, id);
  58. ResultSet rs = prepare.executeQuery();
  59. // je sors le resultat de la pile
  60. rs.next();
  61. p.setId(rs.getInt("id"));
  62. p.setNom(rs.getString("nom"));
  63. p.setPrenom(rs.getString("prenom"));
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. return p;
  68. }
  69. public static void delete(int id) {
  70. String requete = "DELETE FROM personne WHERE id = ?";
  71. Connection connection = getConnection();
  72. try {
  73. PreparedStatement prepare = (PreparedStatement) connection.prepareStatement(requete);
  74. // je viens binder le 1er ?
  75. prepare.setInt(1, id);
  76. prepare.execute();
  77. prepare.close();
  78. connection.commit();
  79.  
  80.  
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. public static void insert(Personne p) {
  86. String requete = "INSERT INTO personne (nom,prenom) VALUES (?,?)";
  87. Connection connection = getConnection();
  88. try {
  89. PreparedStatement prepare = (PreparedStatement) connection.prepareStatement(requete);
  90. // je viens binder le 1er ?
  91. prepare.setString(1, p.getNom());
  92. prepare.setString(2, p.getPrenom());
  93. prepare.execute();
  94. prepare.close();
  95. connection.commit();
  96.  
  97.  
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. public static void update(Personne p) {
  103. String requete = "UPDATE personne SET nom = ?,prenom=? WHERE id=?";
  104. Connection connection = getConnection();
  105. try {
  106. PreparedStatement prepare = (PreparedStatement) connection.prepareStatement(requete);
  107. // je viens binder le 1er ?
  108. prepare.setString(1, p.getNom());
  109. prepare.setString(2, p.getPrenom());
  110. prepare.setInt(3, p.getId());
  111. prepare.execute();
  112. prepare.close();
  113. connection.commit();
  114.  
  115.  
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120.  
  121. }
RAW Paste Data