2 pièce(s) jointe(s)
NullPointerException sur initialisation de driver JDBC
Bonjour à tous,
Je suis bloqué depuis un moment sur un bug :
ValidationClient.java
Code:
1 2 3 4 5 6 7 8 9
| /* ... */
/* Essayer d'écrire le client dans la BDD */
/* Initialisation du DAO */
ClientDAO clientDao = new ClientDAOService(DAOFactory.getInstance());
/* Exécution des requêtes de création */
clientDao.creer(client);
/* ... */ |
ClientDAOService.java
Code:
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
| /* ... */
/* Implémentation de la méthode définie dans l'interface UtilisateurDao */
@Override
public void creer( Client client ) throws DAOException
{
Connection connexion = null;
PreparedStatement preparedStatement = null;
ResultSet valeursAutoGenerees = null;
try
{
/* Récupération d'une connexion depuis la Factory */
connexion = (Connection) daoFactory.getConnection();
preparedStatement = initialisationRequetePreparee( connexion, SQL_INSERT, true, client.getId(), client.getNom(), client.getPrenom(), client.getAdresse(), client.getTelephone(), client.getMail(), client.getImage() );
int statut = preparedStatement.executeUpdate();
/* Analyse du statut retourné par la requête d'insertion */
if ( statut == 0 )
{
throw new DAOException( "Échec de la création du client, aucune ligne ajoutée dans la table." );
}
/* Récupération de l'id auto-généré par la requête d'insertion */
valeursAutoGenerees = preparedStatement.getGeneratedKeys();
if ( valeursAutoGenerees.next() )
{
/* Puis initialisation de la propriété id du bean Utilisateur avec sa valeur */
client.setId( new Long(valeursAutoGenerees.getLong( 1 )).toString() );
}
else
{
throw new DAOException( "Échec de la création du client en base, aucun ID auto-généré retourné." );
}
}
catch ( SQLException e )
{
throw new DAOException( e );
}
finally
{
DAOUtilitaire.fermeturesSilencieuses( valeursAutoGenerees, preparedStatement, connexion );
}
}
/* ... */ |
DAOFactory.java
Code:
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
| /* ... */
public static DAOFactory getInstance() throws DAOConfigurationException
{
String url;
String driver;
String username;
String password;
try
{
/* Récupération des propriétés depuis le fichier */
Properties prop = new Properties();
prop.load(new FileInputStream(FICHIER_PROPERTIES));
url = prop.getProperty("url");
driver = prop.getProperty("driver");
username = prop.getProperty("nomutilisateur");
password = prop.getProperty("motdepasse");
}
catch ( IOException e )
{
throw new DAOConfigurationException( "Impossible de charger le fichier properties " + FICHIER_PROPERTIES, e );
}
/* Initialisation du driver */
try
{
Class.forName( driver ); // <- ligne qui pose problème
}
catch ( ClassNotFoundException e )
{
throw new DAOConfigurationException( "Le driver est introuvable dans le classpath.", e );
}
DAOFactory instance = new DAOFactory(url, username, password);
return instance;
}
/* ... */ |
Console de Tomcat
Code:
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
| java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.tp1.bdd.dao.DAOFactory.getInstance(DAOFactory.java:63)
at com.tp1.business.ValidationClient.creerClient(ValidationClient.java:128)
at com.tp1.servlets.CreationClient.doPost(CreationClient.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744) |
Oui! Je sais!
Il faut rajouter le JAR MySQL à la classpath de Tomcat!
Mais...
Je l'ai fait, je l'ai inclus dans le WEB-INF/lib du projet, ce qui devrait suffire, mais comme ça n'a pas suffit je l'ai aussi inclus:
- Dans la classpath du serveur (j'ai essayé en user ET en bootstrap)
- Dans le dossier lib du TOMCAT_HOME
http://www.developpez.net/forums/att...1&d=1386982439
http://www.developpez.net/forums/att...1&d=1386982467
Mais je n'ai pas réussi à résoudre l'erreur...
Quelqu'un saurait-il m'indiquer comment résoudre ce problème ?
Merci d'avance pour votre aide.