IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

Problème de connexion BDD MySQL


Sujet :

JDBC Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    406
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 406
    Par défaut Problème de connexion BDD MySQL
    Bonjour à tous,

    J'essaie de me connecter à une base de données se trouvant à une certaine adresse IP. Cependant, j'ai une erreur que je n'arrive pas à résoudre (je suis débutant en jdbc). Voici ma classe qui gère la connexion avec la base de données :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
     
    public class ConnexionBDD {
        // Le driver pour se connecter à la base de données
        private static String driver = "com.mysql.jdbc.Driver";
     
        // L'URL de la base de données
        private static String url = "jdbc:mysql://192.168.2.24/";
     
        // Le login pour la connexion à la base de données
        private static String login = "dodo";
     
        // Le mot de passe pour la connexion à la base de données
        private static String motDePasse = "dada";
     
        // La connexion
        private static Connection connexion = null;
     
        // Le demande de requête
        private static Statement demande = null;
     
        // Le résultat de la requête
        private static ResultSet resultat = null;
     
        /**
         * Retourne la connexion à la base de données.
         *
         * @param baseDeDonnées La base de données sur laquelle se connecter
         * @return connexion Connexion à la base de données
         * @since 1.0
         */
        private static Connection connexion(String baseDeDonnees) {
            // Si la connexion est null
            if (connexion == null) {
    	    try {
                    // On construit l'URL de la base de données
                    url += baseDeDonnees;
     
                    // On récupère la connexion à partir du login et du mot de passe
    		connexion = DriverManager.getConnection(url, login,
                            motDePasse);
     
    	    } catch (SQLException exception) {
                    // On affiche la pile d'appel de l'exception
                    exception.printStackTrace();
     
                    // On affiche un message
    		System.out.println("La connexion à la BDD n'a pas été établie");
    	    }
            }
            // On retourne la connexion
    	return connexion;
        }
     
        /**
         * Exécute une requête SQL de type "SELECT ..." et retourne le résultat.
         *
         * @param baseDeDonnees La base de données sur laquelle se connecter
         * @param requete La requete à exécuter
         * @throws SQLException Exception levée lors de la demande ou la requête
         * @return resultat Résultat de la requete SQL
         * @since 1.0
         */
        public static ResultSet executeRequete(String baseDeDonnees,
                String requete) throws SQLException {
            // On essaie le chargement du driver
    	try {
    		// Chargement du driver de la base de données
    		Class.forName(driver);
    	} catch (ClassNotFoundException exception) {
    		// On affiche la pile d'appel de l'exception
                    exception.printStackTrace();
     
                    // On affiche un message
                    System.out.println("Le driver jdbc n'a pas été trouvé");
    	}
     
    	// On récupère la connexion à la base de données
    	connexion = connexion(baseDeDonnees);
     
    	// On crée une demande de requête
    	demande = connexion.createStatement();
     
    	// On exécute la requête
    	resultat = demande.executeQuery(requete);
     
            // On ferme la connexion
            // Si la connexion n'est pas null
            if (connexion != null) {
                try {
                    // On ferme la connexion
                    connexion.close();
                } catch (SQLException exception) {
                    // On affiche la pile d'appel de l'exception
                    exception.printStackTrace();
     
                    // On affiche un message
                    System.out.println("La connexion à la base de données n'a pas" +
                            " été fermée");
                }
            }
     
            // On retourne le résultat de la requête
    	return resultat;
        }
    }
    En ce qui concerne l'installation de jdbc, j'ai bien ajouté le connecteur (mysql-connector-java-5.0.5-bin.jar) dans les librairies du projet.
    L'exception se produit lors de la ligne suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    // On récupère la connexion à partir du login et du mot de passe
    connexion = DriverManager.getConnection(url, login, motDePasse);
    Et voici mon exception :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
     
    run:
    com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
     
    ** BEGIN NESTED EXCEPTION ** 
     
    java.net.ConnectException
    MESSAGE: Connection refused: connect
     
    STACKTRACE:
     
    java.net.ConnectException: Connection refused: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
            at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
            at java.net.Socket.connect(Socket.java:519)
            at java.net.Socket.connect(Socket.java:469)
            at java.net.Socket.<init>(Socket.java:366)
            at java.net.Socket.<init>(Socket.java:209)
            at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
            at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:267)
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2739)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
            at java.sql.DriverManager.getConnection(DriverManager.java:582)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at fr.apreslaclasse.alcgged.modele.ConnexionBDD.connexion(ConnexionBDD.java:57)
            at fr.apreslaclasse.alcgged.modele.ConnexionBDD.executeRequete(ConnexionBDD.java:96)
            at fr.apreslaclasse.alcgged.modele.GestionnaireDocument.rechercheID(GestionnaireDocument.java:151)
            at fr.apreslaclasse.alcgged.vue.FenetrePrincipale.actionPerformed(FenetrePrincipale.java:203)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6216)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5981)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4583)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
     
     
    ** END NESTED EXCEPTION **
     
     
     
    Last packet sent to the server was 16 ms ago.
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2814)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
            at java.sql.DriverManager.getConnection(DriverManager.java:582)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at fr.apreslaclasse.alcgged.modele.ConnexionBDD.connexion(ConnexionBDD.java:57)
            at fr.apreslaclasse.alcgged.modele.ConnexionBDD.executeRequete(ConnexionBDD.java:96)
            at fr.apreslaclasse.alcgged.modele.GestionnaireDocument.rechercheID(GestionnaireDocument.java:151)
            at fr.apreslaclasse.alcgged.vue.FenetrePrincipale.actionPerformed(FenetrePrincipale.java:203)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6216)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5981)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4583)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4413)
    La connexion à la BDD n'a pas été établie
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at fr.apreslaclasse.alcgged.modele.ConnexionBDD.executeRequete(ConnexionBDD.java:99)
            at fr.apreslaclasse.alcgged.modele.GestionnaireDocument.rechercheID(GestionnaireDocument.java:151)
            at fr.apreslaclasse.alcgged.vue.FenetrePrincipale.actionPerformed(FenetrePrincipale.java:203)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6216)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5981)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4583)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    BUILD SUCCESSFUL (total time: 29 seconds)

  2. #2
    Membre confirmé
    Homme Profil pro
    Developpeur web/Java
    Inscrit en
    Février 2008
    Messages
    98
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Developpeur web/Java
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Février 2008
    Messages : 98
    Par défaut
    Salut,

    As tu essayé de te connecter à une base de donnée en locale.
    Cela pourrait être du au fait que ton utilisateur n'ait pas le droit d'accéder à ta BDD à distance !

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    406
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 406
    Par défaut
    Le truc, c'est que l'utilisateur à tous les droits, j'ai configuré la base de données pour ça.
    Est-ce possible que l'url soit mauvaise ?

  4. #4
    Membre confirmé
    Homme Profil pro
    Developpeur web/Java
    Inscrit en
    Février 2008
    Messages
    98
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Developpeur web/Java
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Février 2008
    Messages : 98
    Par défaut
    Oui c'est vrai que ton url le port de mysql n'est pas précisé !
    L'url devrait être de ce format :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    jdbc:mysql://192.168.2.24:port/BASE

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    406
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 406
    Par défaut
    Ok, et quel port dois-je indiquer ? (sachant que la base de données se trouve sur un serveur SVN).

  6. #6
    Membre confirmé
    Homme Profil pro
    Developpeur web/Java
    Inscrit en
    Février 2008
    Messages
    98
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Developpeur web/Java
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Février 2008
    Messages : 98
    Par défaut
    De mémoire je crois me rappeler que le port par défaut de mysql est le 3306 !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème connexion BDD MySQL
    Par Toytoy00 dans le forum API standards et tierces
    Réponses: 4
    Dernier message: 08/12/2014, 10h52
  2. Problème de connexion BDD MySQL sous Android
    Par eloumghari.imane dans le forum API standards et tierces
    Réponses: 12
    Dernier message: 21/01/2014, 01h46
  3. [MySQL] Problème connexion bdd mysql
    Par boobs60 dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 11/03/2010, 08h48
  4. Problème connexion BDD MySql avec jdbc
    Par libery dans le forum JDBC
    Réponses: 6
    Dernier message: 27/05/2009, 12h25
  5. Problèmes de connexion réseau à MySQL
    Par digital prophecy dans le forum Bases de données
    Réponses: 3
    Dernier message: 05/05/2005, 21h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo