Database.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package db;
  2. import java.sql.Statement;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Optional;
  8. import java.util.logging.Logger;
  9. import db.table.EspaceTable;
  10. import entity.Espace;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.ResultSetMetaData;
  16. import java.sql.SQLException;
  17. public class Database {
  18. private static final Logger LOGGER = Logger.getLogger(Database.class.getName());
  19. private static Connection db;
  20. static {
  21. ini();
  22. unset();
  23. setup();
  24. execute("Insert into Espace(LIBELLE) Values('test')");
  25. //execute("Insert into Espace(LIBELLE) Values('test')");
  26. EspaceTable et = EspaceTable.getInstance();
  27. Espace e = new Espace();
  28. e.id = 8;
  29. e.libelle = "bjr";
  30. et.save(e);
  31. }
  32. public static void ini() {
  33. try{
  34. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  35. } catch(ClassNotFoundException e){
  36. LOGGER.warning("Unable to find Derby driver");
  37. throw new IllegalStateException("Unable to connect to the database", e);
  38. }
  39. try {
  40. db = DriverManager.getConnection("jdbc:derby:microfolie;create=true");
  41. } catch (SQLException e) {
  42. LOGGER.warning(e.getMessage());
  43. throw new IllegalStateException("Unable to connect to the database", e);
  44. }
  45. }
  46. public static void close() {
  47. if(db != null) {
  48. try {
  49. db.close();
  50. db = null;
  51. } catch (SQLException e) {
  52. LOGGER.warning(e.getMessage());
  53. throw new IllegalStateException("Unable to close database connection", e);
  54. }
  55. }
  56. }
  57. public static void setup() {
  58. if(isSet()) {
  59. return;
  60. }
  61. LOGGER.info("Creating table");
  62. ManageTable.create.forEach((table, sql) -> {
  63. LOGGER.info("Creating table " + table);
  64. execute(sql);
  65. });
  66. }
  67. public static void unset() {
  68. if(!isSet()) {
  69. return;
  70. }
  71. LOGGER.info("Dropping table");
  72. ManageTable.drop.forEach((table, sql) -> {
  73. LOGGER.info("Dropping table " + table);
  74. execute(sql);
  75. });
  76. }
  77. public static boolean query(String sql) {
  78. try(Statement st = db.createStatement()) {
  79. st.executeQuery(sql);
  80. return true;
  81. } catch (SQLException | IllegalStateException e) {
  82. LOGGER.warning(e.getMessage());
  83. return false;
  84. }
  85. }
  86. public static <T> Optional<T> query(String sql, Mapper<T> mapper) {
  87. try(Statement st = db.createStatement()) {
  88. ResultSet result = st.executeQuery(sql);
  89. T obj = mapper.map(result);
  90. return Optional.of(obj);
  91. } catch (SQLException | IllegalStateException e) {
  92. LOGGER.warning(e.getMessage());
  93. return Optional.empty();
  94. }
  95. }
  96. public static <T> Optional<T> query(String sql, List<Object> params, Mapper<T> mapper) {
  97. try(PreparedStatement st = db.prepareStatement(sql)) {
  98. int i = 1;
  99. for(Object param : params) {
  100. st.setObject(i++, param);
  101. }
  102. ResultSet result = st.executeQuery();
  103. T obj = mapper.map(result);
  104. return Optional.of(obj);
  105. } catch (SQLException | IllegalStateException e) {
  106. LOGGER.warning(e.getMessage());
  107. return Optional.empty();
  108. }
  109. }
  110. public static boolean execute(String sql) {
  111. try(Statement st = db.createStatement()) {
  112. int result = st.executeUpdate(sql);
  113. return result == 1;
  114. } catch (SQLException e) {
  115. LOGGER.warning(e.getMessage());
  116. return false;
  117. }
  118. }
  119. public static boolean execute(String sql, List<Object> params) {
  120. try(PreparedStatement st = db.prepareStatement(sql)) {
  121. int i = 1;
  122. for (Object param : params) {
  123. st.setObject(i++, param);
  124. }
  125. int result = st.executeUpdate();
  126. return result == 1;
  127. } catch (SQLException e) {
  128. LOGGER.warning(e.getMessage());
  129. return false;
  130. }
  131. }
  132. public static Map<String, Object> insert(String sql, List<Object> params, String[] idField) {
  133. Map<String, Object> ids = new HashMap<>();
  134. try(PreparedStatement st = db.prepareStatement(sql, idField)) {
  135. // Ajout parametre
  136. int i = 1;
  137. for(Object param : params) {
  138. st.setObject(i++, param);
  139. }
  140. // Execution requete
  141. int result = st.executeUpdate();
  142. if(result != 1) {
  143. return ids;
  144. }
  145. // Recuperation des ids genere
  146. ResultSet rs = st.getGeneratedKeys();
  147. rs.next();
  148. i = 1;
  149. for (String field : idField) {
  150. ids.put(field, rs.getObject(i));
  151. i++;
  152. }
  153. return ids;
  154. } catch (SQLException e) {
  155. LOGGER.warning(e.getMessage());
  156. return ids;
  157. }
  158. }
  159. public static boolean isSet() {
  160. return query("Select * From Espace");
  161. }
  162. public static Connection getDb() {
  163. return db;
  164. }
  165. }