Advertisement
Guest User

APP JAVA BDD

a guest
Apr 19th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1. import java.sql.*;
  2. import javax.swing.JFrame;
  3. import com.mysql.jdbc.Connection;
  4. import com.mysql.jdbc.Statement;
  5.  
  6. public class APP {
  7.     public static String[] RecupererTaches(int choix) {
  8.  
  9.         // Information d'accès à la base de données
  10.         String url = "jdbc:mysql://localhost/taches";
  11.         String login = "root";
  12.         String passwd = "";
  13.         Connection cn = null;
  14.         Statement st = null;
  15.         ResultSet rs = null;
  16.         String[] datalib = new String[500];
  17.         int i = 0;
  18.  
  19.         try {
  20.  
  21.             // Etape 1 : Chargement du driver
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.  
  24.             // Etape 2 : récupération de la connexion
  25.             cn = (Connection) DriverManager.getConnection(url, login, passwd);
  26.  
  27.             // Etape 3 : Création d'un statement
  28.             st = (Statement) cn.createStatement();
  29.             String sql="";
  30.             if (choix ==0){
  31.              sql = "SELECT libelleTACHE FROM tache order by datedebutTACHE";
  32.             }
  33.             else if (choix ==1){
  34.                  sql = "SELECT libelleTACHE FROM tache where datefinTACHE < now() order by datedebutTACHE";
  35.                 }
  36.             else if (choix ==2){
  37.                  sql = "SELECT libelleTACHE FROM tache where datedebutTACHE > now() order by datedebutTACHE";
  38.                 }
  39.             else if (choix ==3){
  40.                  sql = "SELECT libelleTACHE FROM tache where datedebutTACHE < now() AND  datefinTACHE > now() order by datedebutTACHE";
  41.                 }
  42.  
  43.             // Etape 4 : exécution requête
  44.             rs = st.executeQuery(sql);
  45.  
  46.             // Si récup données alors étapes 5 (parcours Resultset)
  47.  
  48.             while (rs.next()) {
  49.                 datalib[i] = rs.getString("libelleTACHE");
  50.                 i++;
  51.  
  52.             }
  53.         } catch (SQLException e) {
  54.             e.printStackTrace();
  55.         } catch (ClassNotFoundException e) {
  56.             e.printStackTrace();
  57.         } finally {
  58.             try {
  59.                 // Etape 6 : libérer ressources de la mémoire.
  60.                 cn.close();
  61.                 st.close();
  62.             } catch (SQLException e) {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.         return datalib;
  67.     }
  68.  
  69.     public static String[] RecupererInfo(String libelletache) {
  70.  
  71.         // Information d'accès à la base de données
  72.         String url = "jdbc:mysql://localhost/taches";
  73.         String login = "root";
  74.         String passwd = "";
  75.         Connection cn = null;
  76.         Statement st = null;
  77.         ResultSet rs = null;
  78.         String[] infos = new String[500];
  79.  
  80.         try {
  81.  
  82.             // Etape 1 : Chargement du driver
  83.             Class.forName("com.mysql.jdbc.Driver");
  84.  
  85.             // Etape 2 : récupération de la connexion
  86.             cn = (Connection) DriverManager.getConnection(url, login, passwd);
  87.  
  88.             // Etape 3 : Création d'un statement
  89.             st = (Statement) cn.createStatement();
  90.  
  91.             String sql = "SELECT * FROM tache where libelleTache='" + libelletache + "'";
  92.  
  93.             // Etape 4 : exécution requête
  94.             rs = st.executeQuery(sql);
  95.  
  96.             // Si récup données alors étapes 5 (parcours Resultset)
  97.  
  98.             while (rs.next()) {
  99.                 infos[0] = rs.getString("idTACHE");
  100.                 infos[1] = rs.getString("libelleTACHE");
  101.                 infos[2] = rs.getString("datedebutTACHE");
  102.                 infos[3] = rs.getString("datefinTACHE");
  103.  
  104.             }
  105.         } catch (SQLException e) {
  106.             e.printStackTrace();
  107.         } catch (ClassNotFoundException e) {
  108.             e.printStackTrace();
  109.         } finally {
  110.             try {
  111.                 // Etape 6 : libérer ressources de la mémoire.
  112.                 cn.close();
  113.                 st.close();
  114.             } catch (SQLException e) {
  115.                 e.printStackTrace();
  116.             }
  117.         }
  118.         return infos;
  119.  
  120.     }
  121.     public static void MiseAJour(String newDateDebut,String newDateFin,String id){
  122.        
  123.         // Information d'accès à la base de données
  124.                 String url = "jdbc:mysql://localhost/taches";
  125.                 String login = "root";
  126.                 String passwd = "";
  127.                 Connection cn = null;
  128.                 Statement st = null;
  129.                 ResultSet rs = null;
  130.  
  131.                 try {
  132.  
  133.                     // Etape 1 : Chargement du driver
  134.                     Class.forName("com.mysql.jdbc.Driver");
  135.  
  136.                     // Etape 2 : récupération de la connexion
  137.                     cn = (Connection) DriverManager.getConnection(url, login, passwd);
  138.  
  139.                     // Etape 3 : Création d'un statement
  140.                     st = (Statement) cn.createStatement();
  141.  
  142.                     String sql = "UPDATE `tache` SET `datedebutTACHE` = '"+newDateDebut+"', `datefinTACHE` = '"+newDateFin+"' WHERE `tache`.`idTACHE` = "+id+";";
  143.  
  144.                     // Etape 4 : exécution requête
  145.                     st.executeUpdate(sql);
  146.  
  147.                     // Si récup données alors étapes 5 (parcours Resultset)
  148.                    
  149.                 } catch (SQLException e) {
  150.                     e.printStackTrace();
  151.                 } catch (ClassNotFoundException e) {
  152.                     e.printStackTrace();
  153.                 } finally {
  154.                     try {
  155.                         // Etape 6 : libérer ressources de la mémoire.
  156.                         cn.close();
  157.                         st.close();
  158.                     } catch (SQLException e) {
  159.                         e.printStackTrace();
  160.                     }
  161.                 }
  162.     }
  163. public static void Supprimer(String id){
  164.        
  165.         // Information d'accès à la base de données
  166.                 String url = "jdbc:mysql://localhost/taches";
  167.                 String login = "root";
  168.                 String passwd = "";
  169.                 Connection cn = null;
  170.                 Statement st = null;
  171.                 ResultSet rs = null;
  172.  
  173.                 try {
  174.  
  175.                     // Etape 1 : Chargement du driver
  176.                     Class.forName("com.mysql.jdbc.Driver");
  177.  
  178.                     // Etape 2 : récupération de la connexion
  179.                     cn = (Connection) DriverManager.getConnection(url, login, passwd);
  180.  
  181.                     // Etape 3 : Création d'un statement
  182.                     st = (Statement) cn.createStatement();
  183.  
  184.                     String sql = "DELETE FROM `tache` WHERE `tache`.`idTACHE` ="+id+"";
  185.  
  186.                     // Etape 4 : exécution requête
  187.                     st.executeUpdate(sql);
  188.  
  189.                     // Si récup données alors étapes 5 (parcours Resultset)
  190.                    
  191.                 } catch (SQLException e) {
  192.                     e.printStackTrace();
  193.                 } catch (ClassNotFoundException e) {
  194.                     e.printStackTrace();
  195.                 } finally {
  196.                     try {
  197.                         // Etape 6 : libérer ressources de la mémoire.
  198.                         cn.close();
  199.                         st.close();
  200.                     } catch (SQLException e) {
  201.                         e.printStackTrace();
  202.                     }
  203.                 }
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement