简体   繁体   中英

java.lang.ClassNotFoundException: com.sql.jdbc.Driver

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 ic/p-ed it into my the libraries file in my project directory) Thanks for helping:)

    Try this class name :

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

    Refer to the official docs

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM