I can't load the JDBC driver for MySQL

前端 未结 3 1244
萌比男神i
萌比男神i 2021-01-13 02:47

I\'ve been trying to load the JDBC MySQL connector with the following code:

import java.sql.*;

public class dbTest{
   public static void main(String[] args         


        
相关标签:
3条回答
  • 2021-01-13 03:13

    On IntelliJ this is how I solved this problem:

    File > Project Structure > Libraries > +

    Locate the jdbc connector. For me it was on C:\Users\MyName.InteliJIdea13\config\jdbc-drivers

    0 讨论(0)
  • 2021-01-13 03:19

    I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath

    The exception tells you that you didn't do it correctly.

    How are you setting CLASSPATH? If it's an environment variable, you're going to learn that IDEs and app servers ignore it. Don't use it.

    Don't put it in the /ext directory of your Java JDK, either.

    The right way to do it depends on how you're using it:

    1. If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.
    2. IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.
    3. If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.
    0 讨论(0)
  • 2021-01-13 03:29

    There are two classpaths in java. Build path and run path. Build path is used when compiling .java files into .class files. In a language like C you have a linker stage that fills in all the missing symbols when you run the linker on a bunch of object files. Thats why for .exe(windows) or other native binaries(linux) there is no run path. Java is slightly different because the compiled .class definitions get loaded by the jvm as they are needed.

    What the net out of this is that you have to supply a runtime classpath to the jvm. At the command line you use java.exe which searches a few places by default including $CLASSPATH, the current directory/lib, and whatever you supply to the -cp option.

    IDEs are different from the command line because they are attempting to shield you from some of the nastiness of running java.exe and supplying the locations where all the .class files are(which would be onerous on a large project).

    Most IDE's have some sort of "Run Configuration" tab that allows you to specify certain libraries or locations with classes that will be used when you run your application. Below is how to set the run path in eclipse,netbeans, and intellij.

    http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html

    http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html

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