0

My code in java servlet is:

Class.forName("com.mysql.jdbc.Driver");
        Connection connectionToSql = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/table_name", "root", "root");
        PreparedStatement ask = connectionToSql.prepareStatement("SELECT COUNT(*) FROM table_name");
        ResultSet result = ask.executeQuery();

But it throws exception e and the System.out.println(e.getMessage()); prints com.mysql.jdbc.Driver. The same code works in a standard java class, but fails in servlet. I can't find out where is the problem? help please.

Here is the full error message..

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
quiz.QuestionsPreparer.prepareQuestions(QuestionsPreparer.java:81)
quiz.QuestionsPreparer.doPost(QuestionsPreparer.java:42)
quiz.QuestionsPreparer.doGet(QuestionsPreparer.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
Sargsyan Grigor
  • 533
  • 1
  • 5
  • 22
  • 1
    Print the full stacktrace and EDIT your question to add it. FWIW, I expect that the problem is that you haven't included the mysql driver JAR file in your WAR file. – Stephen C May 07 '16 at 00:02
  • full stacktrace also prints com.mysql.jdbc.Driver – Sargsyan Grigor May 07 '16 at 00:04
  • That is not a full stacktrace. That is just an error message. Show us the full stacktrace. Use `e.printStacktrace(...)`. – Stephen C May 07 '16 at 00:05
  • Also, consider using a current MySQL driver so that you don't have to use 'Class.forName()' to load it. Even if you do have to use 'Class.forName()' for an obsolescent driver, it shouldn't be every time you make a connection, but exactly once per program run. – Lew Bloch May 07 '16 at 00:11
  • I use Class.forName just one time – Sargsyan Grigor May 07 '16 at 00:12

2 Answers2

0

Without taking the look at your project folder structure and code, it is difficult to give you a solution.

But Below Statement from the link gives you the importance of Web Application folder structure. https://tomcat.apache.org/tomcat-5.5-doc/appdev/deployment.html

/WEB-INF/classes/ - This directory contains Java class files from your application.

/WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

you can unzip the war file and take a loot at the WEB_INF/lib for mysql jar files.

Peru
  • 1,827
  • 1
  • 12
  • 4
-1

You can Do this way:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Connection conn = null;
...
try {
    conn =
       DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                                   "user=minty&password=greatsqldb");

    // Do something with the Connection

   ...
} catch (SQLException ex) {
    // handle any errors
    System.out.println("SQLException: " + ex.getMessage());
    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
}
Juan_H
  • 367
  • 4
  • 15