Java EE / problème de connexion à la BDD
Résolu
Conan3
Messages postés
7
Date d'inscription
Statut
Membre
Dernière intervention
-
Conan3 Messages postés 7 Date d'inscription Statut Membre Dernière intervention -
Conan3 Messages postés 7 Date d'inscription Statut Membre Dernière intervention -
Salut !
Je suis en train de créer une app web avec Java EE sous l'IDE NetBeans.
Mais, j'ai un problème de connexion à la base de données.
J'ai besoin de votre aider.
Merci d'avance.
Voici le code qui se charge de la connexion :
Je ne sais pas exactement où est le problème.
En effet, lorsque je lance le programme, il m'affiche cette expression : Impossible de communiquer avec la base de données 2
qui se trouve dans la ligne 90 du code suivant :
Je suis en train de créer une app web avec Java EE sous l'IDE NetBeans.
Mais, j'ai un problème de connexion à la base de données.
J'ai besoin de votre aider.
Merci d'avance.
Voici le code qui se charge de la connexion :
package dao; import java.util.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DaoFactory { private String url; private String username; private String password; DaoFactory(String url, String username, String password) { this.url = url; this.username = username; this.password = password; } public static DaoFactory getInstance() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { } DaoFactory instance = new DaoFactory( "jdbc:mysql://localhost:3306/inscriptions", "root", ""); return instance; } public Connection getConnection() throws SQLException { Connection connexion = DriverManager.getConnection(url, username, password); connexion.setAutoCommit(false); return connexion; } // Récupération du Dao public UtilisateurDao getUtilisateurDao() { return new UtilisateurDaoImpl(this); } }
Je ne sais pas exactement où est le problème.
En effet, lorsque je lance le programme, il m'affiche cette expression : Impossible de communiquer avec la base de données 2
qui se trouve dans la ligne 90 du code suivant :
package dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import beans.Utilisateur; public class UtilisateurDaoImpl implements UtilisateurDao { private final DaoFactory daoFactory; UtilisateurDaoImpl(DaoFactory daoFactory) { this.daoFactory = daoFactory; } @Override public void ajouter(Utilisateur utilisateur) throws DaoException { Connection connexion = null; PreparedStatement preparedStatement = null; try { connexion = daoFactory.getConnection(); preparedStatement = connexion.prepareStatement("INSERT INTO inscription(nom, prenom, email, motDePasse, pseudo ) VALUES(?, ?, ?, ?, ?);"); preparedStatement.setString(1, utilisateur.getNom()); preparedStatement.setString(2, utilisateur.getPrenom()); preparedStatement.setString(3, utilisateur.getEmail()); preparedStatement.setString(4, utilisateur.getMotDePasse()); preparedStatement.setString(5, utilisateur.getPseudo()); preparedStatement.executeUpdate(); connexion.commit(); } catch (SQLException e) { try { if (connexion != null) { connexion.rollback(); } } catch (SQLException e2) { } throw new DaoException("Impossible de communiquer avec la base de données 1"); } finally { if ( preparedStatement != null ) { try { preparedStatement.close(); } catch ( SQLException e ) { System.out.println( "échec de la fermeture du preparedStatement 1 : " + e.getMessage() ); } } if (connexion != null) { try{ connexion.close(); }catch (SQLException e) { throw new DaoException("échec de la fermeture de la connexion 1 : "); } } } } @Override public List<Utilisateur> lister() throws DaoException { List<Utilisateur> utilisateurs = new ArrayList <Utilisateur>(); Connection connexion = null; PreparedStatement preparedStatement= null; ResultSet resultat = null; try { connexion = daoFactory.getConnection(); preparedStatement = connexion.prepareStatement("SELECT nom, prenom, email, motDePasse, pseudo FROM inscription;"); resultat = preparedStatement.executeQuery(); while (resultat.next()) { String nom = resultat.getString("nom"); String prenom = resultat.getString("prenom"); String email = resultat.getString("email"); String motDePasse = resultat.getString("motDePasse"); String pseudo = resultat.getString("pseudo"); Utilisateur utilisateur = new Utilisateur(); utilisateur.setNom(nom); utilisateur.setPrenom(prenom); utilisateur.setEmail(email); utilisateur.setMotDePasse(motDePasse); utilisateur.setPseudo(pseudo); utilisateurs.add(utilisateur); } } catch (SQLException e) { throw new DaoException("Impossible de communiquer avec la base de données 2"); } finally { if ( resultat != null ) { try { resultat.close(); } catch ( SQLException e ) { System.out.println( "échec de la fermeture du Resultat 1 : " + e.getMessage() ); } } if ( preparedStatement != null ) { try { preparedStatement.close(); } catch ( SQLException e ) { System.out.println( "échec de la fermeture du preparedStatement 2 : " + e.getMessage() ); } } if (connexion != null) { try{ connexion.close(); }catch (SQLException e) { throw new DaoException("échec de la fermeture de la connexion 2 : " + e.getMessage()); } } } return utilisateurs; } }
A voir également:
- Java EE / problème de connexion à la BDD
- Gmail connexion - Guide
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
2 réponses
Bonjour,
Sans certitude...
ta ligne de code
ne devrait-elle pas plutot etre
Sans certitude...
ta ligne de code
Connection connexion = DriverManager.getConnection(url, username, password);
ne devrait-elle pas plutot etre
Connection connexion = DriverManager.getConnection(this.url, this.username, this.password);
J'ai cherché et j'ai trouvé des avertissements dans les logs de GlassFish comme suivant :
Launching GlassFish on Felix platform
févr. 10, 2018 12:55:28 PM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner createBundleProvisioner
INFOS: Create bundle provisioner class = class com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner.
févr. 10, 2018 12:55:28 PM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner$DefaultCustomizer getLocations
AVERTISSEMENT: Skipping entry because it is not an absolute URI.
févr. 10, 2018 12:55:28 PM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner$DefaultCustomizer getLocations
AVERTISSEMENT: Skipping entry because it is not an absolute URI.
févr. 10, 2018 12:55:28 PM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner startBundles
AVERTISSEMENT: Can not start bundle file:/C:/Program%20Files/glassfish-4.1.1/glassfish/modules/core.jar because it is not contained in the list of installed bundles.
Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishRuntime@b84374 in service registry.
#!## LogManagerService.postConstruct : rootFolder=C:\Program Files\glassfish-4.1.1\glassfish
#!## LogManagerService.postConstruct : templateDir=C:\Program Files\glassfish-4.1.1\glassfish\lib\templates
#!## LogManagerService.postConstruct : src=C:\Program Files\glassfish-4.1.1\glassfish\lib\templates\logging.properties
#!## LogManagerService.postConstruct : dest=C:\Users\Windows7\AppData\Roaming\NetBeans\8.2\config\GF_4.1.1\domain1\config\logging.properties
Infos: Running GlassFish Version: GlassFish Server Open Source Edition 4.1.1 (build 1)
Infos: Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter
Infos: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
Infos: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
Infos: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
Infos: Authorization Service has successfully initialized.
Infos: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
Infos: JTS5014: Recoverable JTS instance, serverId = [100]
Avertissement: Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-1, realClassName=org.glassfish.grizzly.http2.Http2AddOn
Grave: Application previously deployed is not at its original location any more: file:/C:/Users/Windows7/Desktop/EnterpriseApplication1/EnterpriseApplication1-war/build/web/
Infos: Grizzly Framework 2.3.23 started in: 219ms - bound to /0.0.0.0:8080
Avertissement: Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-2, realClassName=org.glassfish.grizzly.http2.Http2AddOn
Infos: Grizzly Framework 2.3.23 started in: 23ms - bound to /0.0.0.0:8181
Avertissement: Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=admin-listener, realClassName=org.glassfish.grizzly.http2.Http2AddOn
Infos: Grizzly Framework 2.3.23 started in: 4ms - bound to /0.0.0.0:4848
Infos: Grizzly Framework 2.3.23 started in: 8ms - bound to /0.0.0.0:3700
Infos: visiting unvisited references
Infos: Java security manager is disabled.
Infos: Entering Security Startup Service.
Infos: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.
Infos: Security Service(s) started successfully.
Infos: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
Infos: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181
Infos: Created HTTP listener admin-listener on host/port 0.0.0.0:4848
Infos: Created virtual server server
Infos: Created virtual server __asadmin
Infos: Setting JAAS app name glassfish-web
Infos: Virtual server server loaded default web module
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: Loading application [PFE_JEE] at /PFE_JEE
Infos: Loading application PFE_JEE done in 7 956 ms
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: Loading application [PFE_JEE-war] at /PFE_JEE-war
Infos: Loading application PFE_JEE-war done in 582 ms
Infos: GlassFish Server Open Source Edition 4.1.1 (1) startup time : Felix (17 141ms), startup services(11 551ms), total(28 692ms)
Infos: Grizzly Framework 2.3.23 started in: 8ms - bound to /0.0.0.0:7676
Infos: HV000001: Hibernate Validator 5.1.2.Final
Infos: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://Windows7-PC:8686/jndi/rmi://Windows7-PC:8686/jmxrmi
Avertissement: Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-2, realClassName=org.glassfish.grizzly.http2.Http2AddOn
Infos: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181
Infos: Grizzly Framework 2.3.23 started in: 16ms - bound to /0.0.0.0:8181
Avertissement: Instance could not be initialized. Class=interface org.glassfish.grizzly.http.server.AddOn, name=http-listener-1, realClassName=org.glassfish.grizzly.http2.Http2AddOn
Infos: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
Infos: Grizzly Framework 2.3.23 started in: 9ms - bound to /0.0.0.0:8080
Infos: Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@830d9a as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@1fae596.
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: Loading application [PFE_JEE] at /PFE_JEE
Infos: PFE_JEE was successfully deployed in 1 066 milliseconds.
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: Loading application [PFE_JEE] at /PFE_JEE
Infos: PFE_JEE was successfully deployed in 12 670 milliseconds.
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: visiting unvisited references
Infos: Loading application [PFE_JEE] at /PFE_JEE
Infos: PFE_JEE was successfully deployed in 2 977 milliseconds.
Il m'affiche la même erreur.