5

I know this is a common question, but despite adding mysql-connector to my library in IntelliJ IDEA, the IDE still can't seem to locate the class. As you can see in the screenshots below, I have added the mysql-connector jar as a library for my project, yet it doesn't seem to see that it is present:

enter image description here

enter image description here

enter image description here

I haven't been able to find a solution other than 'add the library to the project'. It seems as though there is a missing step somewhere...

2 Answers 2

5

You will also need to add the library as a dependency to the module that needs it.

Choose Project Settings > Modules. Select the Module that needs the library (in your case it seems like you have only one module in your project, ChatBot). Select the Dependencies tab. Click the '+' button and choose Library...). Finally, select the mysql-connector.. library that you added to the project.

Edit: I see now that this wasn't your problem at all. The problem with your code is that you have an unhandled exception from Class.forName(). The method can throw the checked exception: ClassNotFoundException, which must be handled by adding a catch or by adding throws ClassNotFoundException to the method signature of getConnection().

In such cases with error in the code, the easiest way to figure out what's wrong to simply move the caret to the code with the red squigly line and see what IDEA says in the bottom status bar. Alternatively you can hover the mouse pointer above it and the error message is presented as a popup.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, however this did not work. I went to the Project Structure > Modules > Dependencies and the mysql-connecter was already added there. I made sure to toggle the checkbox, but still no go...
@Dylan: Ahh, I didn't really look at your code, just answered the question about a possible missing step. In you case it was something completely different. I updated the answer accordingly.
@Steiner: thanks, taking a second look at this error message I finally understood what it meant and was able to fix it-- I don't know why it wasn't obvious previously.
0

I needed to change the exception type from SQLException in the corresponding catch block where the mysql-connector was being loaded to the base type Exception. That seems to have cleared up the problem.

1 Comment

If there was a compilation error you should have mentioned it in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.