问题
In Netbeans ide 7.0, I am facing this particular problem. I wrote the following line of code in my program
Class.forName("com.mysql.jdbc.Driver").newInstance();
This particular line is throwing an exception. And when I connected the same mysql db server from the "Services -> Databases -> Drivers -> MySQL(Connector/Jdriver)" in the netbeans 7.0, it got connected. When I went into the properties of that connection I found that it is connecting the mysql server using the same class which I used in the line of code.
I also read some of the forums such as
http://www.javakb.com/Uwe/Forum.aspx/java-setup/11895/ClassNotFoundException-com-mysql-jdbc-Driver-from-netbeans
but this 1 is for netbeans on Ubuntu. What about Windows?
also, went through all of the responses in here
http://forums.mysql.com/read.php?60,42473,42473#msg-42473
I don't know what is wrong? Please help.
回答1:
You need to download the connector from here: http://www.mysql.com/products/connector/ and include it in your classpath
回答2:
Solution : Well the problem here was different. My netbeans was able to connect MySQL server through its own predefined services. That was because the path of that class was defined in the driver it was using. But in the project I was building, the class-path for MySQL driver was not set. So I just added the same driver "MySQL JDBC Driver" to my Project.
It can be done, just go to the Project navigator which is on the left side. There you'll find the Libraries attached to your Project at the bottom of Project navigator. Then right click on the library, select "Add Library..." and then select the particular library from the Global Libraries present. In my case the connector class was present in the netbeans but the library was not added to my project.
Thank you btw for your precious time.
回答3:
yes
- You don't have mysql-connector.jar in your Classpath. as stated earlier this jar file contains "com.mysql.jdbc.Driver" class it must be present in classpath in order to successful connection to mysql database. you can downlad mysql-connector.jar from mysql.com.
- mysql-connector.jar is in your classpath but somehow your classpath is getting overridden. Classpath is tricky in Java and classpath specified in jar may override CLASSPATH path variable. See how classpath works in Java to understand this issue in detail.
- mysql-connector.jar is in classpath but current user doesn't have read permission. This problem often happens in Unix or Linux operating system which has sophisticated file and directory permission based on user, group and owner level. just get the right permission and run your program again.
Read more: http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html#ixzz2Ply4zLFF
来源:https://stackoverflow.com/questions/6699796/struggling-with-class-not-found-exception-com-mysql-jdbc-driver-in-java