开发者

cant connect to jdbc driver mysql

While I am trying to connect to mysql database using java class I am getting this error. Please help I have set the CLASSPATH: C:\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8.jar

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
             at java.net.URLClassLoader$1.run(Unknown Source)
             at java.net.URLClassLoader$1.run(Unknown Source)
             at java.security.AccessController.doPrivileged(Native Method)
             at java.net.URLClassLoader.findClass(Unknown Source)
             at java.lang.ClassLoader.loadClass(Unknown Source)
             at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
             at java.lang.ClassLoader.loadClass(Unknown Source)
             at java.lang.Class.forName0(Native 开发者_StackOverflow社区Method)
             at java.lang.Class.forName(Unknown Source)
    at connect.main(connect.java:17)
Cannot connect to database server
Exception in thread "main" java.lang.NullPointerException
    at connect.main(connect.java:29)


Add the driver to the classpath:

java -cp .;C:\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8.jar connect


If you are getting that error, your classpath is not correct. Either you are not including it properly to the java process (java -cp <classpath> YourClass) or the mysql connector jar is not located where you are pointing at.


Remove the CLASSPATH environment variable and test your code as suggested by @morja and @pap. In case you want to set environment variable CLASSPATH, then the value of CLASSPATH must be: (Please verify the location of .jar or for the sake of simplicity shorten the folder and .jar file e.g c:\driver\mysqldriver.jar)

.;C:\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8.jar
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜