problème JDBC
Répondre à la discussion
Affichage des résultats 1 à 2 sur 2

problème JDBC



  1. #1
    Zero Cold

    problème JDBC


    ------

    Salut à tous !

    J'essaie d'effectuer une insertion dans la base de données, mais l'opération échoue toujours. Est-ce que quelqu'un peut m'aider la-dessus ?
    Merci d'avance

    Voici mon code :
    Code:
    package webapp;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class JDBC {
    
        private int           statut;
        private String        id;
        private String        titreEmp;
        private String        specialiteEmp;
        private String        experienceEmp;
        private String        titreCand;
        private String        specialiteCand;
        private String        experienceCand;
        private int           flag;
    
        private String[]      infoCand = new String[3];
        private String[]      infoEmp  = new String[3];
        Map<String, String[]> mapCand  = new HashMap<String, String[]>();
        Map<String, String[]> mapEmp   = new HashMap<String, String[]>();
    
        /* La liste qui contiendra tous les résultats de nos essais */
        public Map<String, String[]> executerTests() {
            /* Chargement du driver JDBC pour MySQL */
            try {
    
                Class.forName( "com.mysql.jdbc.Driver" );
    
            } catch ( ClassNotFoundException e ) {
    
            }
    
            /* Connexion à la base de données */
            String url = "jdbc:mysql://localhost:3306/bdd_sdzee";
            String utilisateur = "zero";
            String motDePasse = "abcd";
            Connection connexion = null;
            Statement statement = null;
            ResultSet resultat_2 = null;
            ResultSet resultat_1 = null;
    
            try {
    
                connexion = DriverManager.getConnection( url, utilisateur, motDePasse );
    
                /* Création de l'objet gérant les requêtes */
                statement = connexion.createStatement();
                flag = 0;
    
                resultat_1 = statement.executeQuery(
                        "select * from candidat;" );
                while ( resultat_1.next() ) {
                    infoCand[0] = resultat_1.getString( "titre" );
                    infoCand[1] = resultat_1.getString( "specialite" );
                    infoCand[2] = resultat_1.getString( "experience" );
                    id = Integer.toString( resultat_1.getInt( "id_cand" ) );
                    mapCand.put( id, infoCand );
                }
                resultat_2 = statement.executeQuery(
                        "select * from employeur;" );
                while ( resultat_2.next() ) {
                    infoCand[0] = resultat_2.getString( "titre" );
                    infoCand[1] = resultat_2.getString( "specialite" );
                    infoCand[2] = resultat_2.getString( "experience" );
                    id = Integer.toString( resultat_2.getInt( "id_emp" ) );
                    mapEmp.put( id, infoCand );
                }
    
                Iterator<Entry<String, String[]>> itCand = mapCand.entrySet().iterator();
                Iterator<Entry<String, String[]>> itEmp = mapEmp.entrySet().iterator();
    
                while ( itCand.hasNext() ) {
                    while ( itEmp.hasNext() ) {
                        Map.Entry<String, String[]> mapentry_1 = (Map.Entry<String, String[]>) itCand.next();
                        Map.Entry<String, String[]> mapentry_2 = (Map.Entry<String, String[]>) itEmp.next();
                        infoCand = mapentry_1.getValue();
                        infoEmp = mapentry_2.getValue();
                        if ( infoCand[0].compareTo( infoEmp[0] ) == 0 && infoCand[1].compareTo( infoEmp[1] ) == 0
                                && infoCand[2].compareTo( infoEmp[2] ) == 0 ) {
                            statement.executeUpdate( "insert into affectation (id_emp,id_cand) values (2, 4);" );
                        }
    
                    }
                    itEmp = mapEmp.entrySet().iterator();
                }
            } catch ( SQLException e ) {
    
            } finally {
    
                if ( resultat_1 != null ) {
                    try {
                        resultat_1.close();
                    } catch ( SQLException ignore ) {
                    }
                }
                if ( resultat_2 != null ) {
                    try {
                        resultat_2.close();
                    } catch ( SQLException ignore ) {
                    }
                }
    
                if ( statement != null ) {
                    try {
                        statement.close();
                    } catch ( SQLException ignore ) {
                    }
                }
    
                if ( connexion != null ) {
                    try {
                        connexion.close();
                    } catch ( SQLException ignore ) {
                    }
                }
            }
    
            return mapEmp;
        }
    }

    -----

  2. #2
    pm42

    Re : problème JDBC

    Pourquoi utiliser le même statement pour faire l'update que le select et alors que le dit select est encore en train d'être lu ?
    Pourquoi ne pas faire un commit explicite ?
    Pourquoi ne pas afficher les exceptions éventuelles dans le catch(SqlException...) ? Ce qui permettrait de savoir ce qui se passe ?

    Et si tu es en Java 7 ou 8, pourquoi ne pas utiliser la syntaxe try(Connection connection = ...) et try(ResultSet resultSet = ...) plutôt que ce code horrible avec les if(xxx != null

    https://docs.oracle.com/javase/tutor...urceClose.html

Discussions similaires

  1. [Java - JDBC] Erreur lors de création de table
    Par Lechero dans le forum Programmation et langages, Algorithmique
    Réponses: 7
    Dernier message: 18/03/2014, 06h29
  2. Java serialization and JDBC
    Par nicom974 dans le forum Programmation et langages, Algorithmique
    Réponses: 0
    Dernier message: 04/06/2013, 07h01
  3. Réponses: 2
    Dernier message: 04/12/2012, 14h08
  4. urgent:je chereche un JDBC oracle-tomcat
    Par invite714d16fd dans le forum Logiciel - Software - Open Source
    Réponses: 0
    Dernier message: 05/05/2006, 15h40