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

后端 未结 13 2760
梦毁少年i
梦毁少年i 2020-11-21 11:36

There is a VERY similar question to mine but in my case I don\'t have any duplicate jars in my build path, so the solution does not work for me. I\'ve searched google for a

相关标签:
13条回答
  • 2020-11-21 11:44

    for this error:

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

    you need to:

    Import java.sql.*;
    Import com.mysql.jdbc.Driver;
    

    even if its not used till app running.

    0 讨论(0)
  • 2020-11-21 11:49

    Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application war or put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.

    0 讨论(0)
  • 2020-11-21 11:55

    The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.

    Rather than an empty main(), try something like this, adapted from the included documentation:

    public class LoadDriver {
        public static void main(String[] args) throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
        }
    }
    

    On my platform, I'd do this:

    $ ls mysql-connector-java-5.1.12-bin.jar 
    mysql-connector-java-5.1.12-bin.jar
    $ javac LoadDriver.java 
    $ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver
    

    On your platform, you need to use ; as the path separator, as discussed here and here.

    0 讨论(0)
  • 2020-11-21 12:01

    Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute

    0 讨论(0)
  • 2020-11-21 12:02

    The only solution worked for me is putting the .jar file under WEB-INF/lib . Hope this will help.

    0 讨论(0)
  • 2020-11-21 12:02

    assuming your project is maven based, add it to your POM:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
    

    Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.

    0 讨论(0)
提交回复
热议问题