0

I am trying to connect to DB using JDBC My code :-

    public static Connection getConnection() {

    String dbName = "test";

    String userName = "test";

    String password = "";

    Connection connection = null;

    try {
        Class.forName("com.myqsl.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost/" + dbName, userName, password);

    } catch (Exception e) {
        // connection = null;
        System.out.println("error");
        e.printStackTrace();
    }
    return connection;
}

And I am getting java.lang.ClassNotFoundException. I tried attaching rt.jar to the class file editor source but now getting Source attachment does not contain the source for the file drivermanager.class just don't know how to proceed

Note:- using eclipse and not on debugging mode.

  • Can you add the exception as well ? – soufrk Jun 15 '18 at 06:58
  • I guess you miss the mysql-connector in your classpath. https://mvnrepository.com/artifact/mysql/mysql-connector-java – Jens Jun 15 '18 at 06:59
  • java.lang.ClassNotFoundException: com.myqsl.jdbc.Driver at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at overview.connectionDB.getConnection(connectionDB.java:18) @soufrk – Utsav Singh Jun 15 '18 at 07:06
  • 1
    it is `mysql`, not `myqsl`... voting to close as typo. BTW: Loading the driver like this is usually not necessary and hasn't been for 10+ years, and if you do it, you only need to do it once (eg in an initializer), not every time you get a connection. – Mark Rotteveel Jun 15 '18 at 07:09
  • @MarkRotteveel even after fixing the typo I am getting the same error – Utsav Singh Jun 15 '18 at 07:11
  • Then you don't have the MySQL Connector/J driver on your class path. Add it. See https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database (especially the second answer) – Mark Rotteveel Jun 15 '18 at 07:12

0 Answers0