class not found exception com.mysql.jdbc.driver [d

2019-06-13 15:37发布

问题:

This question already has an answer here:

  • How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception 13 answers

File structure of my project is:

         -src
            |
           -pkg
            |
             -CoreServlet.java(servlet)
             -Main.java
             -Core.java(jdbc code is here)

core.java class:

package com.pkg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class core{
    private Connection connect = null;
    private Statement statement =null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;
    String qwerty;

    public void readDataBase() {
        String userName = "ansh";
        String password = "12345";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/glbitm", userName,password);
            statement = connect.createStatement();
            resultSet = statement.executeQuery("select * from teachers");
            resultSet.next();      
            qwerty = resultSet.getString(1);

           } catch (Exception e) {
            System.out.println(e);
        }
    }

}

coreServlet.java class :

   package com.pkg;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class coreServlet extends HttpServlet{ 
  /**
     * 
     */
    private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException,IOException{
        core dao = new core();
        dao.readDataBase();
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  pw.println("<html>");
  pw.println("<head><title>Hello World</title></head>");
  pw.println("<div>"+dao.qwerty+"</div>");
  pw.println("<body>");
  pw.println("<h1>Hello World</h1>");
  pw.println("</body></html>");
  }
}

When I am accessing dao.qwerty in my coreServlet.java in my tomcat server. I am getting class not found exception com.mysql.jdbc.driver and value of dao.qwerty is printed as null. Where I am doing wrong ?

回答1:

You have not set the mysql-connector in your path.Set that first and everything else will be done

What is JConnector ?

Java does not know SQL so every statement you want to execute using the sql driver will get converted to sql constructs and the result that is returned my mysql is also converted into java understandable constructs.

hope it helps



回答2:

You need to add the mysql connector in classpath.

http://mirrors.ibiblio.org/pub/mirrors/maven2/mysql/mysql-connector-java/5.1.4/mysql-connector-java-5.1.4.jar

This connector jar acts as a mediator between database and the application for the flow of data. You can extract the class files from jar and see the details.



回答3:

The java.lang.ClassNotFoundException is thrown when your code attempts to execute the following line Class.forName("com.mysql.jdbc.Driver").newInstance();

This is a checked exception which always needs to be either caught inside a try/catch or if try/catch is not implemented, then this exception needs to be declared in the method.

There have been multiple resolutions provided in this thread, but from my experience if you are able to connect to your database from eclipse using data source explorer, then most likely reason is "mysql-connector-java-5.1.28-bin.jar" is not copied into the your tomcat's lib directory.

The mysql connector for java can be downloaded from http://dev.mysql.com/downloads/connector/j/