0

I have been starting off with some JDBC a few days ago as of 2019/3. And there was this error occurring when I try to comply the code below in my eclipse IDE.

I actually did some research before this and I have tried:-

  • -Adding external libraries from the project menu
  • -Reinstalling and trying out different ides(thinking it was just eclipse but turns out its something about my system)
  • -reinstalled both jdk and the jdbc connector
  • and still, the problem persists.

    import java.sql.*;
    public class Driver{
        public static void main(String[]args)throws Exception {
    
            String url = "jdbc:mysql://localhost:3306/main";
            String uName = "Ng Jun Han";
            String pW = "password";
            String query = "SELECT first FROM students WHERE id = 1";
    
    
            Class.forName("com.sql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, uName, pW);
            Statement st = con.createStatement();
            ResultSet rs= st.executeQuery(query);
    
            rs.next();
            String name = rs.getString("first");
            System.out.print(name);
    
            st.close();
            con.close();
        }
    }
    

    This is how my project directory looks like

    My biggest concern regarding the topic is about something wrong I did with the installation methods. Mainly because there are not much up-to-date resources to follow.If so, does anyone know the CORRECT way of fixing it?(the driver jar file is located at C:\Program Files\MySQL , and i c/p-ed it into my the libraries file in my project directory) Thanks for helping:)

    Ng Han
    • 3
    • 2

    1 Answers1

    0

    Try this class name :

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

    Refer to the official docs

    Mouad EL Fakir
    • 3,609
    • 2
    • 23
    • 37
    • 1
      Nice share, but i got this instead Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. – Ng Han May 12 '19 at 13:09
    • Yes, I confirm you should use `com.mysql.cj.jdbc.Driver` instead, because the other one is deprecated now, or you can refer to a more up to date documentation : https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html – Mouad EL Fakir May 12 '19 at 13:13
    • https://stackoverflow.com/questions/52344453/loading-class-com-mysql-jdbc-driver-is-deprecated-message . Did some more research and fixed it. Many thanks. – Ng Han May 12 '19 at 13:13