Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import javax.swing.JFrame;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
- public class APP {
- public static String[] RecupererTaches(int choix) {
- // Information d'accès à la base de données
- String url = "jdbc:mysql://localhost/taches";
- String login = "root";
- String passwd = "";
- Connection cn = null;
- Statement st = null;
- ResultSet rs = null;
- String[] datalib = new String[500];
- int i = 0;
- try {
- // Etape 1 : Chargement du driver
- Class.forName("com.mysql.jdbc.Driver");
- // Etape 2 : récupération de la connexion
- cn = (Connection) DriverManager.getConnection(url, login, passwd);
- // Etape 3 : Création d'un statement
- st = (Statement) cn.createStatement();
- String sql="";
- if (choix ==0){
- sql = "SELECT libelleTACHE FROM tache order by datedebutTACHE";
- }
- else if (choix ==1){
- sql = "SELECT libelleTACHE FROM tache where datefinTACHE < now() order by datedebutTACHE";
- }
- else if (choix ==2){
- sql = "SELECT libelleTACHE FROM tache where datedebutTACHE > now() order by datedebutTACHE";
- }
- else if (choix ==3){
- sql = "SELECT libelleTACHE FROM tache where datedebutTACHE < now() AND datefinTACHE > now() order by datedebutTACHE";
- }
- // Etape 4 : exécution requête
- rs = st.executeQuery(sql);
- // Si récup données alors étapes 5 (parcours Resultset)
- while (rs.next()) {
- datalib[i] = rs.getString("libelleTACHE");
- i++;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- // Etape 6 : libérer ressources de la mémoire.
- cn.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return datalib;
- }
- public static String[] RecupererInfo(String libelletache) {
- // Information d'accès à la base de données
- String url = "jdbc:mysql://localhost/taches";
- String login = "root";
- String passwd = "";
- Connection cn = null;
- Statement st = null;
- ResultSet rs = null;
- String[] infos = new String[500];
- try {
- // Etape 1 : Chargement du driver
- Class.forName("com.mysql.jdbc.Driver");
- // Etape 2 : récupération de la connexion
- cn = (Connection) DriverManager.getConnection(url, login, passwd);
- // Etape 3 : Création d'un statement
- st = (Statement) cn.createStatement();
- String sql = "SELECT * FROM tache where libelleTache='" + libelletache + "'";
- // Etape 4 : exécution requête
- rs = st.executeQuery(sql);
- // Si récup données alors étapes 5 (parcours Resultset)
- while (rs.next()) {
- infos[0] = rs.getString("idTACHE");
- infos[1] = rs.getString("libelleTACHE");
- infos[2] = rs.getString("datedebutTACHE");
- infos[3] = rs.getString("datefinTACHE");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- // Etape 6 : libérer ressources de la mémoire.
- cn.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return infos;
- }
- public static void MiseAJour(String newDateDebut,String newDateFin,String id){
- // Information d'accès à la base de données
- String url = "jdbc:mysql://localhost/taches";
- String login = "root";
- String passwd = "";
- Connection cn = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- // Etape 1 : Chargement du driver
- Class.forName("com.mysql.jdbc.Driver");
- // Etape 2 : récupération de la connexion
- cn = (Connection) DriverManager.getConnection(url, login, passwd);
- // Etape 3 : Création d'un statement
- st = (Statement) cn.createStatement();
- String sql = "UPDATE `tache` SET `datedebutTACHE` = '"+newDateDebut+"', `datefinTACHE` = '"+newDateFin+"' WHERE `tache`.`idTACHE` = "+id+";";
- // Etape 4 : exécution requête
- st.executeUpdate(sql);
- // Si récup données alors étapes 5 (parcours Resultset)
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- // Etape 6 : libérer ressources de la mémoire.
- cn.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void Supprimer(String id){
- // Information d'accès à la base de données
- String url = "jdbc:mysql://localhost/taches";
- String login = "root";
- String passwd = "";
- Connection cn = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- // Etape 1 : Chargement du driver
- Class.forName("com.mysql.jdbc.Driver");
- // Etape 2 : récupération de la connexion
- cn = (Connection) DriverManager.getConnection(url, login, passwd);
- // Etape 3 : Création d'un statement
- st = (Statement) cn.createStatement();
- String sql = "DELETE FROM `tache` WHERE `tache`.`idTACHE` ="+id+"";
- // Etape 4 : exécution requête
- st.executeUpdate(sql);
- // Si récup données alors étapes 5 (parcours Resultset)
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- // Etape 6 : libérer ressources de la mémoire.
- cn.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement