Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.49 KB | None | 0 0
  1. import java.sql.*;
  2. import oracle.jdbc.*;
  3.  
  4. public class ConnectionBD
  5. {
  6.     public Connection conn;
  7.  
  8.     public ConnectionBD()
  9.     {
  10.  
  11.         try
  12.         {
  13.           Class.forName("oracle.jdbc.driver.OracleDriver");
  14.           // Driver chargé
  15.         }
  16.         catch(ClassNotFoundException e)
  17.         {
  18.             System.out.println(e);
  19.         }
  20.  
  21.         // Local
  22.         String url = "jdbc:oracle:thin:@172.17.200.251:1521:orcl";
  23.  
  24.         // Distant
  25.          //String url = "jdbc:oracle:thin:@205.237.246.251:1521:orcl";
  26.  
  27.         String user = "DITULLIO";
  28.         String mpd = "oracle";
  29.  
  30.         try
  31.         {
  32.             conn =  DriverManager.getConnection(url,user,mpd);
  33.             System.out.println("Vous êtes connecté à la BD");
  34.         }
  35.         catch(SQLException e)
  36.          {
  37.             System.out.println(e);
  38.         }
  39.     }
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. ////////////////////////////////////////////////////////////////// MÉTHODES POUR LES JOUEURS //////////////////////////////////////////////////////////////////
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43.     public void AjouterJoueur(String Pseudo, String NomJoueur, String PrenomJoueur)
  44.     {
  45.         try
  46.         {
  47.         CallableStatement Stm = conn.prepareCall("{ call GESTIONJOUEURS.AJOUTERJOUEUR(?,?,?)}");
  48.         Stm.setString(1, Pseudo);
  49.         Stm.setString(2, NomJoueur);
  50.         Stm.setString(3, PrenomJoueur);
  51.         Stm.executeUpdate();
  52.         }
  53.         catch(SQLException e){System.out.println(e);}
  54.     }
  55.  
  56.     public void SupprimerJoueur(String Pseudo)
  57.     {
  58.         try
  59.         {
  60.             CallableStatement Stm = conn.prepareCall("{? =  call GESTIONJOUEURS.SUPPRIMERJOUEUR(?)}");
  61.             Stm.setString(1, Pseudo);
  62.             Stm.executeUpdate();
  63.         }
  64.         catch(SQLException e){System.out.println(e);}
  65.     }
  66.  
  67.     public void InitialiserPosition(int NumeroTour,String Pseudo,int PositionX,int PositionY)
  68.     {
  69.         try
  70.         {
  71.             CallableStatement Stm = conn.prepareCall("{ ? = call GESTIONJOUEURS.INITIALISERPOSITION(?, ?, ?, ?)}");
  72.             Stm.setInt(1, NumeroTour);
  73.             Stm.setString(2, Pseudo);
  74.             Stm.setInt(3, PositionX);
  75.             Stm.setInt(4,PositionY);
  76.             Stm.executeUpdate();
  77.         }
  78.         catch(SQLException e){System.out.println(e);}
  79.     }
  80.  
  81.     public void ModifierPosition(int NumeroTour,String Pseudo,int PositionX,int PositionY)
  82.     {
  83.          try
  84.         {
  85.             CallableStatement Stm = conn.prepareCall("{? = call GESTIONJOUEURS.MODIFIERPOSITION(?, ?, ?, ?)}");
  86.             Stm.setInt(1, NumeroTour);
  87.             Stm.setString(2, Pseudo);
  88.             Stm.setInt(3, PositionX);
  89.             Stm.setInt(4,PositionY);
  90.             Stm.executeUpdate();
  91.         }
  92.         catch(SQLException e){System.out.println(e);}
  93.     }
  94. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. ////////////////////////////////////////////////////////////////// MÉTHODES POUR LA PARTIE //////////////////////////////////////////////////////////////////
  96. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97.     public void InitialiserTout()
  98.     {
  99.         try
  100.         {
  101.         CallableStatement Stm =conn.prepareCall("{call GESTIONPARTIE.Initialisertout() }");
  102.         Stm.executeUpdate();
  103.         }
  104.         catch(Exception e){System.out.println(e);}
  105.     }
  106.  
  107.      public int GetScore(String PseudoJoueur)
  108.     {
  109.         int NbScore=0;
  110.         try
  111.         {
  112.         CallableStatement stm3 =conn.prepareCall("{ ? = call GESTIONJOUEURS.GETSCORE('" + PseudoJoueur + "') }");
  113.         stm3.registerOutParameter(1, OracleTypes.INTEGER);
  114.         stm3.execute();
  115.         NbScore = stm3.getInt(1);
  116.         }
  117.         catch(Exception e){System.out.println(e) ;}
  118.         return NbScore;
  119.     }
  120.  
  121.     public void AjouterScore( char CodeCat,String Pseudo)// a chaque bonne reponse
  122.     {
  123.         try
  124.         {
  125.             System.out.println("CodeCat: " + CodeCat + " Pseudo: " + Pseudo);
  126.             CallableStatement Stm = conn.prepareCall("{call GESTIONPARTIE.AJOUTERSCORE('" + CodeCat + "','" + Pseudo + "')}");
  127.             //String temp = String.valueOf(CodeCat);
  128.             Stm.executeUpdate();
  129.         }
  130.         catch(SQLException e){System.out.println(e);}
  131.     }
  132.  
  133.     public void SetQuestionRepondue(String Statut , String NumeroQuestion) //une fois répondu, je doit setter ca
  134.     {
  135.         try
  136.         {
  137.             CallableStatement Stm = conn.prepareCall("{? = call GESTIONPARTIE.QUESTIONREPONDUE(?,?)}");
  138.             Stm.setString(1, Statut);
  139.             Stm.setString(2, NumeroQuestion);
  140.             Stm.executeUpdate();
  141.         }
  142.         catch(SQLException e){System.out.println(e);}
  143.     }
  144.  
  145.     public String VerifierReponse(String NumeroReponse) // J'envoie: V01A   Renvoie Y pour Yes et N
  146.     {
  147.         try
  148.         {
  149.             CallableStatement Stm = conn.prepareCall("{ ? = call GESTIONPARTIE.VERIFIERREPONSE('" + NumeroReponse + "')}");
  150.             System.out.println(NumeroReponse);
  151.             Stm.registerOutParameter(1, OracleTypes.CHAR);
  152.             Stm.execute();
  153.             Stm.executeUpdate();
  154.             String reponse = Stm.getString(1);
  155.             return reponse;
  156.  
  157.         }
  158.         catch(SQLException e)
  159.         {
  160.  
  161.             System.out.println(e);
  162.             return "erreur";
  163.         }
  164.  
  165.     }
  166.  
  167.     public int CompterQuestionsRestantes(String Categorie) //appeler automatiquement
  168.     {
  169.         int NombreNonRepondues=0;
  170.         try
  171.         {
  172.         CallableStatement stm3 =conn.prepareCall("{ ? = call GESTIONPARTIE.CompterQuestions('" + Categorie + "') }");
  173.         stm3.registerOutParameter(1, OracleTypes.INTEGER);
  174.         stm3.execute();
  175.         NombreNonRepondues = stm3.getInt(1);
  176.         }
  177.         catch(Exception e){System.out.println(e);}
  178.         return NombreNonRepondues;
  179.     }
  180.  
  181.     public String ObtenirQuestion(String Categorie) // Ex: "V" , "B" , "R" , "J"
  182.     {
  183.         boolean Valide=false;
  184.         int Random = 0;
  185.         String Statut = null;
  186.         int NbRestantes = 20;
  187.         String NumQuestion = null;
  188.         if(NbRestantes > 0)
  189.         {
  190.                 while(!Valide)
  191.                 {
  192.                     Random = (int)(Math.random() * (20+1-1)) + 1;
  193.                     if(Random >= 1 && Random < 10)
  194.                     {
  195.                         NumQuestion = Categorie + "0" + Random;
  196.  
  197.                     }
  198.                     else
  199.                     {
  200.                         NumQuestion = Categorie + Random;
  201.                     }
  202.  
  203.                     try
  204.                     {
  205.                         System.out.println("Question choisie au hasard : " + NumQuestion);
  206.                         //NumQuestion = "V01";
  207.                         CallableStatement Stm =conn.prepareCall("{ ? = call GESTIONPARTIE.verifierrepondue('" + NumQuestion + "') }");
  208.                         Stm.registerOutParameter(1, OracleTypes.CHAR);
  209.                         Stm.execute();
  210.                         Statut = Stm.getString(1);
  211.                         if(Statut.equals("N"))
  212.                         {
  213.                             Valide = true;
  214.                         }
  215.  
  216.                     }
  217.                     catch(Exception e){System.out.println(e);}
  218.                 }
  219.                 // Avec le numéro de la question trouvée , il va chercher les 4 choix de cette question
  220.                 try
  221.                 {
  222.                   // Appel de la fonction pour avoir l'énoncé de la question
  223.                   CallableStatement Stm1 = conn.prepareCall("{ ? = call GESTIONPARTIE.ObtenirQuestion('" + NumQuestion + "') }");
  224.  
  225.                   Stm1.registerOutParameter(1, OracleTypes.VARCHAR);
  226.                   Stm1.execute();
  227.                   String Suite = ":" + Stm1.getString(1);
  228.  
  229.                   // Appel de la fonction pour avoir les choix de réponses de la question
  230.                   CallableStatement Stm2 = conn.prepareCall("{ ? = call GESTIONPARTIE.ObtenirChoixReponses('" + NumQuestion + "') }",
  231.                                                             ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  232.                   Stm2.registerOutParameter(1, OracleTypes.CURSOR);
  233.                   Stm2.execute();
  234.                   ResultSet rest = (ResultSet) Stm2.getObject(1);
  235.                   NumQuestion = NumQuestion + Suite + "|";
  236.                   int Cpt=1;
  237.                   while(rest.next())
  238.                   {
  239.  
  240.                    System.out.println("debut du while");
  241.                    if(Cpt==1)
  242.                    NumQuestion = NumQuestion + "A[" + rest.getString(1) + "]";
  243.                    else if(Cpt==2)
  244.                    NumQuestion = NumQuestion + "B[" + rest.getString(1) + "]";
  245.                    else if(Cpt==3)
  246.                    NumQuestion = NumQuestion + "C[" + rest.getString(1) + "]";
  247.                    else if(Cpt==4)
  248.                    NumQuestion = NumQuestion + "D[" + rest.getString(1) + "]";
  249.                    Cpt++;
  250.                   }
  251.                 }
  252.                 catch(Exception e){System.out.println(e);}
  253.  
  254.         }
  255.       return NumQuestion;
  256.     }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement