I am getting a ClassNotFoundException when I attempt to define a driver for a SQLServer database in java code.
Can anyone clarify why this might be, I've seen similar questions but none of their answers seem to work.
Thanks.
I am getting a ClassNotFoundException when I attempt to define a driver for a SQLServer database in java code.
Can anyone clarify why this might be, I've seen similar questions but none of their answers seem to work.
Thanks.
That is a compilation error.
When you call Class.forName()
the method may throw the checked exception ClassNotFoundException
. Since it is a checked exception your code must either handle it in the current method, or declare it in the method's throws
clause.
I suggest that you read the Java tutorial's lesson on exceptions and exception handling, or the Q&A that I have marked this as a dup of.
Note that if this exception actually occurs when your application is run, it means that the Class.forName
was unable to load the JDBC driver class that you named. This typically means that the JAR containing the driver class is not on the runtime classpath. Unless your application can proceed without talking to the database at all(!), this is an exception that it won't be able to recover from.
So basically the screenshot says that you have to catch ClassNotFoundException
:
try {
Class.forName("...");
} catch (ClassNotFoundException e) {
// log exception here
}
or re-throw it in your method signature
void doSmth() throws ClassNotFoundexception {
...
Class.forName("...");
...
}