0

I'm a beginner in JDBC and I want to connect to a MySQL instance on my local machine. Below is the MySQL Instance I'm trying to connect to. It exists on my local machine. It has a database called my_db2 present on it. And I want to work with it's tables.

MySql Workbench A table from my_db2

To connect to the above database, I wrote the following code.

import java.sql.*;

public class TestMySQL {
    public static void main(String[] args) {

        try(
            Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3308/my_db2","root","B03091999b");
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("Select * from department");)
        {
            while(rs.next()){
                System.out.println(rs.getString("DeptName"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

I also added the mysql-connector library using IntelliJ. mysql-connector added using Maven

Yet, when I try to connect to the database, I keep getting this error.

java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3308/my_db2
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at TestMySQL.main(TestMySQL.java:7)

Even though, I've installed the mysql-connector driver using IntelliJ, I keep getting this error. I tried loading the driver explicitly too as follows:

import java.sql.*;

public class TestMySQL {
    public static void main(String[] args) {



        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3308/my_db2","root","B03091999b");
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("Select * from department");
            while(rs.next()){
                System.out.println(rs.getString("DeptName"));
            }

        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
    }
}

At this point, I started getting the following error.

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at TestMySQL.main(TestMySQL.java:10)

As per my knowledge, I followed all the steps properly, to add the mysql-connector file to the libraries. I'm not sure what's going wrong. Please do help me solve this problem. Any help would be appreciated.

  • If you get a `ClassNotFoundException` then you do not have the driver on your runtime classpath. – Mark Rotteveel May 29 '21 at 10:13
  • @MarkRotteveel I have added the driver in the Modules -> Dependencies section of the Project Structure. I see a small dropdown under the "Scope" heading of the Dependencies section. Should I change it to "Runtime" instead of "Compile"? Is that what you're referring to? – heisenberg737 Jun 01 '21 at 07:35
  • It depends on how you actually **run** your application and if the driver is on the classpath at that time. – Mark Rotteveel Jun 01 '21 at 08:12
  • @MarkRotteveel I'm just trying to do what I wrote in my code above. Nothing else. As I already mentioned, I'm a beginner at all this. So if I just wanna perform what's written in my code above, what should I do? – heisenberg737 Jun 01 '21 at 08:18
  • @MarkRotteveel also, looking at the screenshot of my Project Structure, do you think I've not loaded my driver? I'm confused about that too, because as per what I read around, this is the only way to add a Driver to the classpath. – heisenberg737 Jun 01 '21 at 08:19
  • Exactly how do you run your application? – Mark Rotteveel Jun 01 '21 at 08:32
  • @MarkRotteveel I just run it as a normal Java application, by using "Run" button at the top in IntelliJ and running the main function. – heisenberg737 Jun 01 '21 at 08:34
  • I see the problem now, you shouldn't add it under Modules, but under Libraries. As the "External Libraries" tree to the left shows, MySQL Connector/J is not on your classpath. – Mark Rotteveel Jun 01 '21 at 08:42

1 Answers1

2

You should load the driver first:

Class.forName("com.mysql.cj.jdbc.Driver");

Update

As @Mark Rotteveel said, if you get a ClassNotFoundException, then the driver may not load into your runtime classpath. You can add the dependency statement compile ("mysql:mysql-connector-java:8.0.23") into your build.gradle and then try again.

刘维兵
  • 109
  • 1
  • 5
  • This hasn't been necessary since 2006 with Java 6 and a JDBC 4.0 compliant driver, though it is a good way to troubleshoot if the driver is actually on the classpath. – Mark Rotteveel May 29 '21 at 08:46
  • I did load the driver and it throws a ClassNotFoundException at me, even though the library is present in the Project Structure and the Dependencies section. – heisenberg737 Jun 01 '21 at 07:31
  • I don't know why but it looks that the driver doesn't load into the classpath. Since you use gradle, you can remove your own adding and simply add the statement `compile ("mysql:mysql-connector-java:8.0.23") ` into your build.gradle, and then reimport the dependencies. Try again and I think it would be ok. @heisenberg737 – 刘维兵 Jun 01 '21 at 08:40
  • @刘维兵 Thank you so much! I wrote the statement in my build.gradle file and voila! My connection started working instantly. It would be better if you update your answer to reflect this change. Thanks again! – heisenberg737 Jun 01 '21 at 09:03
  • @heisenberg737 Congratulations... I will update the answer. – 刘维兵 Jun 01 '21 at 09:33