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
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @ays! Please use /close or the Close Post button 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd2w ago
show the code you use to connect and the exact error like what does the error say
dan1st
dan1st2w ago
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 nowadays
bassus
bassus2w ago
Also, have you tried Eclipse or IntelliJ
ays
aysOP2w ago
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)
ays
aysOP2w ago
No description
ays
aysOP2w ago
no jar file? i added it to the referenced libraries
bassus
bassus2w ago
That should work at least in IntelliJ and Eclipse :/ But maybe i'm overlooking smth
Madjosz
Madjosz2w ago
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).
dan1st
dan1st2w ago
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?
ays
aysOP2w ago
No description
ays
aysOP2w ago
should i try this on it
bassus
bassus2w ago
Maybe, in general IDEs are more chill to use for Java than VSC imo^^
bassus
bassus2w ago
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.
bassus
bassus2w ago
Community Edition is free^^ (You have to scroll down, website is a bit stoopid)
ays
aysOP2w ago
thanks! :>
JavaBot
JavaBot2w ago
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.
bassus
bassus2w ago
I have a step by step tutorial in german from my uni i could share on how to set up the SQL thingy
Madjosz
Madjosz2w ago
You could try
DriverManager.registerDriver((java.sql.Driver)Class.forName("com.mysql.cj.jdbc.Driver"));
DriverManager.registerDriver((java.sql.Driver)Class.forName("com.mysql.cj.jdbc.Driver"));
dan1st
dan1st2w ago
The Class.forName is what has been throwing the exception..?
Madjosz
Madjosz2w ago
Oh indeed, haven't payed attention to the stack trace.
dan1st
dan1st2w ago
I think it would be useful to expand it in the referenced libraries and see the exact command used for running the application
JavaBot
JavaBot2w ago
💤 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.

Did you find this page helpful?