How to set jdbc driver classpath

拥有回忆 提交于 2019-12-31 07:43:07

问题


When I copy mysql jdbc driver to JDK's\jre\lib\ext, it execute perfectly well. Now, I want to use the jdbc by specifying its classpath to environment variable. But, after doing so, my program throws exception :

"java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books"

How do I set the classpath?


回答1:


You should not be putting ANY JARs in the jre/lib/ext folder.

You set the CLASSPATH using the -classpath option on javac.exe when you compile and java.exe when you run. Make sure that your code and all 3rd party JARs are in the CLASSPATH when you compile and run. Windows uses the semi-colon as the separator; Linux uses colon.

Maybe you need to start here:

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




回答2:


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 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. 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):

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:

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



来源:https://stackoverflow.com/questions/14804184/how-to-set-jdbc-driver-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!