Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8.  
  9. public class BD {
  10. private Connection c;
  11.  
  12. BD() {
  13.  
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver").newInstance();
  16. c = java.sql.DriverManager.getConnection(
  17. "jdbc:mysql://smysql/Bfrayer", "frayer", "frayer");
  18. } catch (Exception e) {
  19. System.out.println("Connection ratée: " + e);
  20. System.exit(-1);
  21. }
  22. /*
  23. * try { Class.forName("com.mysql.jdbc.Driver").newInstance(); c =
  24. * java.sql.DriverManager.getConnection( "jdbc:mysql://localhost/IUT",
  25. * "root", "root"); } catch (Exception e) {
  26. * System.out.println("Connection ratée: " + e); System.exit(-1); }
  27. */
  28. }
  29.  
  30. private static Connection connectionBase() {
  31. Connection c = null;
  32. /*
  33. * try { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch
  34. * (ClassNotFoundException e) { // e.printStackTrace(); } c =
  35. * DriverManager .getConnection("jdbc:oracle:thin:@servora:1521:oracle",
  36. * "frayer", "h3vc5d"); } // gestion des exceptions catch (SQLException
  37. * ex) { System.out.println("Msg:" + ex.getMessage() +
  38. * ex.getErrorCode()); }
  39. */
  40. try {
  41. Class.forName("com.mysql.jdbc.Driver").newInstance();
  42. c = java.sql.DriverManager.getConnection(
  43. "jdbc:mysql://localhost/IUT", "root", "root");
  44. } catch (Exception e) {
  45. System.out.println("Connection ratée: " + e);
  46. System.exit(-1);
  47. }
  48. return c;
  49. }
  50.  
  51. public int maxidIndividu(Connection c) throws SQLException {
  52. Statement s = null;
  53. int num = 0;
  54. s = c.createStatement();
  55. ResultSet rs = null;
  56. rs = s.executeQuery("Select MAX(code_indiv) from Individu");
  57. while (rs.next()) {
  58. num = rs.getInt(1);
  59. }
  60. return num + 1;
  61. }
  62.  
  63. public int maxidFilm(Connection c) throws SQLException {
  64. Statement s = null;
  65. int num = 0;
  66. s = c.createStatement();
  67. ResultSet rs = null;
  68. rs = s.executeQuery("Select MAX(code_film) from Film");
  69. while (rs.next()) {
  70. num = rs.getInt(1);
  71. }
  72. return num + 1;
  73. }
  74.  
  75. public void insererPersonne(Connection c, String n, String p,
  76. String date_n, String date_m, int code_a, int code_r)
  77. throws SQLException {
  78. int code = maxidIndividu(c);
  79. PreparedStatement ps = null;
  80.  
  81. ps = c.prepareStatement("insert into Individu values (?,?,?,?,?,?,?)");
  82. ps.setInt(1, code);
  83. ps.setString(2, n);
  84. ps.setString(3, p);
  85. ps.setString(4, date_n);
  86. ps.setString(5, date_m);
  87. ps.setInt(6, code_a);
  88. ps.setInt(7, code_r);
  89. ps.executeUpdate();
  90.  
  91. }
  92.  
  93. public void insererFilm(Connection c, String titreO, String titreF,
  94. String dateF, int duree, int couleur) throws SQLException {
  95. int code = maxidFilm(c);
  96. PreparedStatement ps = null;
  97.  
  98. ps = c.prepareStatement("insert into Film values (?,?,?,?,?,?)");
  99. ps.setInt(1, code);
  100. ps.setString(2, titreO);
  101. ps.setString(3, titreF);
  102. ps.setString(4, dateF);
  103. ps.setInt(5, duree);
  104. ps.setInt(6, couleur);
  105. ps.executeUpdate();
  106.  
  107. }
  108.  
  109. public void consulterFilmsSimple(Connection c) {
  110. String titleO, titleFR;
  111. Date dateF;
  112. int Duree;
  113. Statement s = null;
  114. try {
  115. s = c.createStatement();
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119. ResultSet rs = null;
  120. try {
  121. rs = s
  122. .executeQuery("Select titre_original, titre_francais, date_film, duree from Film");
  123. } catch (SQLException e) {
  124. e.printStackTrace();
  125. }
  126. // parcours du resultset
  127. try {
  128. while (rs.next()) {
  129. titleO = rs.getString("titre_original");
  130. titleFR = rs.getString("titre_francais");
  131. dateF = rs.getDate("date_film");
  132. Duree = rs.getInt("duree");
  133.  
  134. System.out.println(titleO + " " + titleFR + " " + dateF + " ");
  135. }
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140.  
  141. public ArrayList<String> getNomsPers(Connection c) throws SQLException {
  142. ArrayList<String> list = new ArrayList<String>();
  143. Statement s = null;
  144. s = c.createStatement();
  145. ResultSet rs = null;
  146. rs = s.executeQuery("Select nom from Individu");
  147. while (rs.next()) {
  148. list.add(rs.getString("nom"));
  149. }
  150.  
  151. return list;
  152. }
  153.  
  154. public ArrayList<String> getNomsFilm(Connection c) throws SQLException {
  155. ArrayList<String> list = new ArrayList<String>();
  156. Statement s = null;
  157. s = c.createStatement();
  158. ResultSet rs = null;
  159. rs = s.executeQuery("Select titre_original from Film");
  160. while (rs.next()) {
  161. list.add(rs.getString("titre_original"));
  162. }
  163.  
  164. return list;
  165. }
  166.  
  167. public ArrayList<String> getNomsFilm(Connection c, String cond)
  168. throws SQLException {
  169. ArrayList<String> list = new ArrayList<String>();
  170. Statement s = null;
  171. s = c.createStatement();
  172. ResultSet rs = null;
  173. rs = s
  174. .executeQuery("Select titre_original from Film where titre_original like '%"
  175. + cond + "%' ");
  176. while (rs.next()) {
  177. list.add(rs.getString("titre_original"));
  178. }
  179.  
  180. return list;
  181. }
  182.  
  183. public ArrayList<String> getNomsGenre(Connection c) throws SQLException {
  184. ArrayList<String> list = new ArrayList<String>();
  185. Statement s = null;
  186. s = c.createStatement();
  187. ResultSet rs = null;
  188. rs = s.executeQuery("Select nom_genre from Genre");
  189. while (rs.next()) {
  190. list.add(rs.getString("nom_genre"));
  191. }
  192.  
  193. return list;
  194. }
  195.  
  196. public static ArrayList<String[]> consulterTout(Connection c)
  197. throws SQLException {
  198. // Object[][] monRes = new Object[7][3];
  199.  
  200. ArrayList<String[]> liste = new ArrayList<String[]>();
  201.  
  202. int ligne = 0;
  203. Statement s = null;
  204. String rea, film, act, genre, an;
  205. int codeRea, codeFilm, codeAct, codeGenre, codeAn;
  206. s = c.createStatement();
  207. ResultSet rs = null;
  208.  
  209. // On séléctionne tous les réalisateurs :
  210. rs = s
  211. .executeQuery("Select code_realisateur, nom from Individu where code_acteur = 0;");
  212. // parcours du resultset
  213.  
  214. while (rs.next()) {
  215. rea = rs.getString("nom");
  216. codeRea = rs.getInt("code_realisateur");
  217. Statement sR = null;
  218. sR = c.createStatement();
  219. ResultSet rsR = null;
  220. // On récupère tout les films qu'a fait ce réalisateur :
  221. rsR = sR
  222. .executeQuery("SELECT f.titre_original, f.code_film FROM Film f, Realiser r WHERE f.code_film = r.code_film AND r.code_realisateur = "
  223. + codeRea + ";");
  224.  
  225. // parcours du resultset
  226. while (rsR.next()) {
  227. film = rsR.getString("titre_original");
  228. codeFilm = rsR.getInt("code_film");
  229.  
  230. Statement sF = null;
  231. sF = c.createStatement();
  232. ResultSet rsF = null;
  233. // On séléctionne tous les acteurs qui ont joué dans ce film :
  234. rsF = sF
  235. .executeQuery("SELECT i.nom, i.code_acteur FROM Individu i, Jouer j WHERE j.code_acteur = i.code_acteur AND j.code_film = "
  236. + codeFilm + ";");
  237. while (rsF.next()) {
  238. act = rsF.getString("nom");
  239. codeAct = rsF.getInt("code_acteur");
  240.  
  241. Statement sG = null;
  242. sG = c.createStatement();
  243. ResultSet rsG = null;
  244. rsG = sG
  245. .executeQuery("SELECT g.nom_genre, g.code_genre FROM Genre g, Avoir_un a WHERE a.code_genre = g.code_genre AND a.code_film = "
  246. + codeFilm + "; ");
  247. while (rsG.next()) {
  248. genre = rsG.getString("nom_genre");
  249. codeGenre = rsG.getInt("code_genre");
  250.  
  251. Statement sA = null;
  252. sA = c.createStatement();
  253. ResultSet rsA = null;
  254. rsA = sA
  255. .executeQuery("SELECT Year(f.date_film) as annee FROM Film f WHERE f.code_film = "
  256. + codeFilm + "; ");
  257. while (rsA.next()) {
  258. an = rsA.getString("annee");
  259.  
  260. String[] tmp = { rea, film, act, genre, an };
  261. liste.add(tmp);
  262. ligne++;
  263. }
  264.  
  265. }
  266. }
  267.  
  268. }
  269.  
  270. }
  271. return liste;
  272. }
  273.  
  274. public Connection getConnection() {
  275. return c;
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement