Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.99 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 void AjouterScore(char CodeCat,String Pseudo)// a chaque bonne reponse
  108.     {
  109.         try
  110.         {
  111.             CallableStatement Stm = conn.prepareCall("{ call GESTIONPARTIE.AJOUTERSCORE(?,?)}");
  112.             Stm.setObject(1, CodeCat);
  113.             Stm.setString(2, Pseudo);
  114.             Stm.executeUpdate();
  115.         }
  116.         catch(SQLException e){System.out.println(e);}
  117.     }
  118.  
  119.     public void SetQuestionRepondue(String Statut , String NumeroQuestion) //une fois répondu, je doit setter ca
  120.     {
  121.         try
  122.         {
  123.             CallableStatement Stm = conn.prepareCall("{ call GESTIONPARTIE.QUESTIONREPONDUE(?,?)}");
  124.             Stm.setString(1, Statut);
  125.             Stm.setString(2, NumeroQuestion);
  126.             Stm.executeUpdate();
  127.         }
  128.         catch(SQLException e){System.out.println(e);}
  129.     }
  130.  
  131.     public void VerifierReponse(String NumeroReponse) // J'envoie: V01A   Renvoie Y pour Yes et N
  132.     {
  133.         try
  134.         {
  135.             CallableStatement Stm = conn.prepareCall("{ call GESTIONPARTIE.VERIFIERREPONSE('" + NumeroReponse + "')}");
  136.             Stm.registerOutParameter(1, OracleTypes.CHAR);
  137.             Stm.execute();
  138.             Stm.executeUpdate();
  139.         }
  140.         catch(SQLException e){System.out.println(e);}
  141.     }
  142.  
  143.     public int CompterQuestionsRestantes(String Categorie) //appeler automatiquement
  144.     {
  145.         int NombreNonRepondues=0;
  146.         try
  147.         {
  148.         CallableStatement stm3 =conn.prepareCall("{ ? = call GESTIONPARTIE.CompterQuestions('" + Categorie + "') }");
  149.         stm3.registerOutParameter(1, OracleTypes.INTEGER);
  150.         stm3.execute();
  151.         NombreNonRepondues = stm3.getInt(1);
  152.         }
  153.         catch(Exception e){System.out.println(e);}
  154.         return NombreNonRepondues;
  155.     }
  156.  
  157.     public void ObtenirQuestion(String Categorie) // Ex: "V" , "B" , "R" , "J", si pu de questions, ca renvoie rien
  158.     {
  159.         boolean Valide=false;
  160.         int Random = 0;
  161.         String Statut = null;
  162.         int NbRestantes = CompterQuestionsRestantes(Categorie);
  163.         String NumQuestion = null;
  164.         if(NbRestantes > 0)
  165.         {          
  166.                 while(!Valide)
  167.                 {
  168.                     Random = (int)(Math.random() * (20+1-1)) + 1;
  169.                     if(Random <= 1 && Random < 10)
  170.                     {
  171.                         NumQuestion = Categorie + "0" + Random;
  172.                        
  173.                     }
  174.                     else
  175.                     {
  176.                         NumQuestion = Categorie + Random;
  177.                     }
  178.  
  179.                     try
  180.                     {
  181.                         CallableStatement Stm =conn.prepareCall("{ ? = call GESTIONPARTIE.verifierrepondue('" + NumQuestion + "') }");
  182.                         Stm.registerOutParameter(1, OracleTypes.CHAR);
  183.                         Stm.execute();
  184.                         Statut = Stm.getString(1);
  185.                         if(Statut.equals("N"))
  186.                         {
  187.                             Valide = true;
  188.                         }
  189.                     }
  190.                     catch(Exception e){System.out.println(e);}
  191.                 }
  192.                 // Avec le numéro de la question trouvée , il va chercher les 4 choix de cette question
  193.                 try
  194.                 {
  195.                     String Choix1;
  196. //                  String Choix2;
  197. //                  String Choix3;
  198. //                  String Choix4;
  199.                   CallableStatement Stm2 = conn.prepareCall("{ ? = call GESTIONPARTIE.ObtenirChoixReponses('" + NumQuestion + "') }",
  200.                                                             ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  201.                   Stm2.registerOutParameter(1, OracleTypes.CURSOR);
  202.                   Stm2.execute();
  203.                   ResultSet rest = (ResultSet) Stm2.getObject(1);
  204.                   while(rest.next())
  205.                   {
  206.                    // Reste à faire : Envoyer les choix un par un au client
  207.                    Choix1 = rest.getString(1);
  208.                    //A: salut B: kdskd C:dsdskd D:dksgkd
  209.                   }                
  210.                 }
  211.                 catch(Exception e){System.out.println(e);}
  212.         }
  213.  
  214.     }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement