简体   繁体   English

如何设置JDBC驱动程序类路径

[英]How to set jdbc driver classpath

When I copy mysql jdbc driver to JDK's\\jre\\lib\\ext, it execute perfectly well. 当我将mysql jdbc驱动程序复制到JDK的\\ jre \\ lib \\ ext时,它执行得很好。 Now, I want to use the jdbc by specifying its classpath to environment variable. 现在,我想通过指定环境变量的类路径来使用jdbc。 But, after doing so, my program throws exception : 但是,这样做之后,我的程序将引发异常:

"java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books" “ java.sql.SQLException:找不到适用于jdbc:mysql:// localhost / books的驱动程序”

How do I set the classpath? 如何设置类路径?

You should not be putting ANY JARs in the jre/lib/ext folder. 您不应该将任何JAR放在jre / lib / ext文件夹中。

You set the CLASSPATH using the -classpath option on javac.exe when you compile and java.exe when you run. 编译时,在javac.exe上使用-classpath选项设置CLASSPATH;运行时,在java.exe上使用-classpath选项设置。 Make sure that your code and all 3rd party JARs are in the CLASSPATH when you compile and run. 编译和运行时,请确保您的代码和所有第三方JAR都在CLASSPATH中。 Windows uses the semi-colon as the separator; Windows使用分号作为分隔符。 Linux uses colon. Linux使用冒号。

Maybe you need to start here: 也许您需要从这里开始:

http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html

You can include any jar files you need by specifying them in the java command with the -cp switch (which is identical to -classpath . For example if the JDBC driver's name is 'myjdbc.jar' then you would execute your program as follows: 您可以通过在Java命令中使用-cp开关(与-classpath相同)来指定所需的任何jar文件。例如,如果JDBC驱动程序的名称为“ myjdbc.jar”,则可以按以下方式执行程序:

java -cp myjdbc.jar your.package.YourClass

If you have more jar files, you can separate them with a semi-colon on Windows or colon on Linux/Unix. 如果您有更多的jar文件,则可以在Windows上用分号或在Linux / Unix上用冒号分隔。 Commonly the current directly is also included, and we put all needed jar files in a /lib folder, so it would look something like this (on Windows): 通常,当前的电流也直接包含在内,并且我们将所有需要的jar文件放在/lib文件夹中,因此它看起来像这样(在Windows上):

java -cp .;lib/myjdbc.jar your.package.YourClass

Also, if you have lots of jar files, it would be more convenient to put them all in the /lib folder and have something like this: 另外,如果您有很多jar文件,将它们全部放在/lib文件夹中并具有以下内容会更方便:

java -cp .;lib/* your.package.YourClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM