Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. package fr.jimnz.ZediaVille.Objets;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11. public class ConnectionDb {
  12.  
  13. private final String dbName = "zediaville";
  14. private final String dbUsername="root";
  15. private final String dbPassword="";
  16. String url = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUsername + "&password=" + dbPassword + "&useUnicode=true&characterEncoding=UTF-8";
  17.  
  18. static Connection connection;
  19.  
  20. public Boolean enable() {
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. } catch (ClassNotFoundException e) {
  24. e.printStackTrace();
  25. System.err.println("jdbc driver unavailable!");
  26. return false;
  27. }
  28. try {
  29. connection = DriverManager.getConnection(url,dbUsername,dbPassword);
  30. return true;
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. return false;
  35. }
  36.  
  37. public Boolean disable() {
  38. try {
  39. if (connection!=null && !connection.isClosed()){
  40. connection.close();
  41. }
  42. } catch(Exception e) {
  43. e.printStackTrace();
  44. }
  45. return false;
  46. }
  47.  
  48. public String select(String requete, String champ) {
  49. try {
  50. PreparedStatement stmt = connection.prepareStatement(requete);
  51. ResultSet results = stmt.executeQuery();
  52. if (!results.next()) {
  53. return "FALSE";
  54. } else {
  55. return results.getString(champ);
  56. }
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. }
  60. return "FALSE";
  61. }
  62.  
  63. public ArrayList<String> selectArrayResults(String requete, String champ) {
  64. try {
  65. ArrayList<String> resultats = new ArrayList<String>();
  66. PreparedStatement stmt = connection.prepareStatement(requete);
  67. ResultSet results = stmt.executeQuery();
  68. while ( results.next() ) {
  69. resultats.add(results.getString(champ));
  70. }
  71.  
  72. return resultats;
  73.  
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. return null;
  78. }
  79.  
  80. public int selectCount(String requete) {
  81. try {
  82. PreparedStatement stmt = connection.prepareStatement(requete);
  83. ResultSet results = stmt.executeQuery();
  84. if (!results.next()) {
  85. return 0;
  86. } else {
  87. return results.getInt(1);
  88. }
  89. } catch (SQLException e) {
  90. e.printStackTrace();
  91. }
  92. return 0;
  93. }
  94.  
  95. public ArrayList<String> selectArrayChamps(String requete, ArrayList<String> champs) {
  96. try {
  97. ArrayList<String> resultats = new ArrayList<String>();
  98. Statement stmt = connection.createStatement();
  99. ResultSet results = stmt.executeQuery(requete);
  100.  
  101. while ( results.next() ) {
  102. for (String s : champs) {
  103. resultats.add(results.getString(s));
  104. }
  105. }
  106.  
  107. return resultats;
  108.  
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. }
  112.  
  113. return null;
  114. }
  115.  
  116. public Boolean insert(String requete) {
  117. try {
  118. Statement statement = connection.createStatement();
  119. int results = statement.executeUpdate(requete);
  120. if (results == 1) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128. return false;
  129. }
  130.  
  131. public Boolean update(String requete) {
  132. try {
  133. Statement statement = connection.createStatement();
  134. int results = statement.executeUpdate(requete);
  135. if (results > 0) {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. } catch (SQLException e) {
  141. e.printStackTrace();
  142. }
  143. return false;
  144. }
  145.  
  146. public Boolean delete(String requete) {
  147. try {
  148. Statement statement = connection.createStatement();
  149. int results = statement.executeUpdate(requete);
  150. if (results > 0) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. } catch (SQLException e) {
  156. e.printStackTrace();
  157. }
  158. return false;
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement