Guest User

Untitled

a guest
Nov 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package com.java.bd;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.java.classe.Ecole;
  7.  
  8.  
  9. public class EcoleBD {
  10.  
  11. public static Connection getConnection(){
  12. Connection con=null;
  13. try{
  14. Class.forName("com.mysql.jdbc.Driver");
  15. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/soufiane","root","");
  16. }catch(Exception e){System.out.println(e);}
  17. return con;
  18. }
  19. public static int save(Ecole u){
  20. int status=0;
  21. try{
  22. Connection con=getConnection();
  23. PreparedStatement ps=con.prepareStatement(
  24. "insert into ecole(nom_ecole, adresse_ecole) values(?,?)");
  25. ps.setString(1,u.getNom_ecole());
  26. ps.setString(2,u.getAdresse_ecole());
  27. status=ps.executeUpdate();
  28. }catch(Exception e){System.out.println(e);}
  29. return status;
  30. }
  31.  
  32.  
  33. public static List<Ecole> getAllRecords(){
  34. List<Ecole> list=new ArrayList<Ecole>();
  35.  
  36. try{
  37. Connection con=getConnection();
  38. PreparedStatement ps=con.prepareStatement("select * from ecole");
  39. PreparedStatement sp=con.prepareStatement("select * from eleve");
  40. ResultSet rs=ps.executeQuery();
  41. ResultSet sr=sp.executeQuery();
  42. while(rs.next()){
  43. Ecole u=new Ecole();
  44. u.setPk_ecole(rs.getInt("pk_ecole"));
  45. u.setNom_ecole(rs.getString("nom_ecole"));
  46. u.setAdresse_ecole(rs.getString("adresse_ecole"));
  47. list.add(u);
  48.  
  49. }
  50. }catch(Exception e){System.out.println(e);}
  51. return list;
  52. }
  53.  
  54. public static Ecole getRecordById(int pk_ecole){
  55. Ecole u=null;
  56. try{
  57. Connection con=getConnection();
  58. PreparedStatement ps=con.prepareStatement("select * from ecole where pk_ecole=?");
  59. ps.setInt(1,pk_ecole);
  60. ResultSet rs=ps.executeQuery();
  61. while(rs.next()){
  62. u=new Ecole();
  63. u.setPk_ecole(rs.getInt("pk_ecole"));
  64. u.setNom_ecole(rs.getString("nom_ecole"));
  65. u.setAdresse_ecole(rs.getString("adresse_ecole"));
  66. }
  67. }catch(Exception e){System.out.println(e);}
  68. return u;
  69. }
  70.  
  71. public static int lol(Eleve u){
  72. int status=0;
  73. try{
  74. Connection con=getConnection();
  75. PreparedStatement ps=con.prepareStatement(
  76. "insert into eleve(nom_eleve, prenom_eleve, naissance_eleve, fk_ecole) values(?,?,?,?)");
  77. ps.setString(1,u.getNom_eleve());
  78. ps.setString(2,u.getPrenom_eleve());
  79. ps.setString(3,u.getNaissance_eleve());
  80. ps.setInt(4,u.getFk_ecole());
  81. status=ps.executeUpdate();
  82. }catch(Exception e){System.out.println(e);}
  83. return status;
  84. }
  85. }
Add Comment
Please, Sign In to add comment