MySql jdbc connection error
Im trying to connect my Java program to mysql using JDBC. i wrote the connection code and set the URL, username and password, but the connection keeps failing.
Im getting a classnotfound exception when I try to load the mysql driver. i have installed the MySQL connector/j .jar file, added the driver to my vsc project ,wrote the connection code using Class.forName("com.mysql.cj.jdbc.Driver"). But the program still cant find the driver.
Also, vsc is confusing me. whenever i try to create a java project, the explorer panel creates a new project every single time and it becomes a loop of multiple empty projects.
could someone guide me on what i might be doing wrong?
23 Replies
⌛ This post has been reserved for your question.
Hey @ays! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
show the code you use to connect
and the exact error
like what does the error say
Also, how did you install the MySQL connector/J jar file? Did you add it to the classpath/modulepath to the project?
Also,
com.mysql.cj.jdbc.Driver isn't really necessary/doesn't make a difference nowadaysAlso, have you tried Eclipse or IntelliJ
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:421)
at java.base/java.lang.Class.forName(Class.java:412)
at App.main(App.java:6)

no
jar file? i added it to the referenced libraries
That should work at least in IntelliJ and Eclipse :/
But maybe i'm overlooking smth
I think we had a similar problem, that the DriverManager did not find the Driver. We circumvented it by instantiating the Driver directly and call
driver.connect(url, propertiesWithCredentials).that's normally fine
Can you show the referenced libraries?
How exactly did you add it? How do you run the application? Does it run a specific command? If so, can you show it?

should i try this on it
Maybe, in general IDEs are more chill to use for Java than VSC imo^^
Download IntelliJ IDEA
Download the latest version of IntelliJ IDEA, the leading IDE for professional development in Java and Kotlin. Available for Windows, macOS, and Linux.
Community Edition is free^^ (You have to scroll down, website is a bit stoopid)
thanks! :>
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
I have a step by step tutorial in german from my uni i could share on how to set up the SQL thingy
You could try
The
Class.forName is what has been throwing the exception..?Oh indeed, haven't payed attention to the stack trace.
I think it would be useful to expand it in the referenced libraries and see the exact command used for running the application
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.