Hossam Oukli
Hossam Oukli

Reputation: 1296

Can't connect The servlet to the database

I'm actually working on an Servlet/MVC/Mysql mini-project , and I'm facing a very strange problem.

I have a class Cataloguethat contains my model methods, those methods Insert, update, delete from the database, and i have also another class Utility with a static method that connects me to the database.

So when i execute the Catalogue methods in the main method, they work fine, But when i run the same methods trough the servlet i get a null pointer exception.

so here are my logs :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at mod.catal.Utilitaire.<clinit>(Utilitaire.java:7)
at mod.catal.Catalogue.selectCategories(Catalogue.java:46)
at mod.catal.Catalogue.selectCatByKeyword(Catalogue.java:43)
at CatalogueServlet.doPost(CatalogueServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)


déc. 16, 2013 12:54:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet catalServlet threw exception
java.lang.NullPointerException
at mod.catal.Catalogue.selectCategories(Catalogue.java:50)
at mod.catal.Catalogue.selectCatByKeyword(Catalogue.java:43)
at CatalogueServlet.doPost(CatalogueServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

the line that returns the error is :

Connection conn=Utilitaire.getConnection();
PreparedStatement ps=conn.prepareStatement(requete);

the conn returns null, only when i run my application on the web container, otherwise it's works fine as i mentioned earlier.

Also I've already added mysql_connector to eclipse as an external.

Upvotes: 0

Views: 714

Answers (2)

Hossam Oukli
Hossam Oukli

Reputation: 1296

The problem were solved when i added the mysql_connector.jar to my Tomcat/lib folder.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279950

Also I've already added mysql_connector to eclipse as an external.

You must have done it incorrectly since

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

means that that class is not on the classpath. Take the connector and add it to WEB-INF/lib (if that folder doesn't exist, create it).

It's possible that adding it as an external library adds it to the Eclipse-Tomcat plugin build path, but doesn't add it to your exported war that you run on your web container.

Upvotes: 2

Related Questions