简体   繁体   中英

Java unable to load class for JDBC driver

I'm trying to make a program in intelJ that connects to a SQL database, but I receive this error when it tries to find the drivers.

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

I've checked that the directory I use is correct when using Class.forname

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

and it points to the correct location within the Jar file for the driver. I'd assume that that java is unable to find the class for whatever reason, as I've done the exact same thing with a second project which works fine. Any ideas?

You need to add the driver JAR to the classpath of the project.

If you're using a simple Java project: right click on the project in IntelliJ IDEA > Open Module Settings. Then, Project Settings > Libraries. Click on +. Select Java. Browse to the driver JAR. Click OK and it should work.

You need to add it to the module's dependencies . The Libraries section just lists the libraries of this project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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