1

My code looks like this-(working in normal text editor)

import java.sql.*;
import java.sql.DriverManager;

class JDBCTest {

    private static final String url = "jdbc:mysql://localhost/learn";

    private static final String user = "root";

    private static final String password = "B!SHu12345";

    public static void main(String args[]) {
        try {
        Class.forName("com.mysql.jdbc.Driver");  
            Connection con = DriverManager.getConnection(url, user, password);
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from bishu where id =101");
        while(rs.next()){
            String s=rs.getString("id");
            System.out.println(s);  
        }
            System.out.println("Success");

        } catch (Exception e) {
            e.printStackTrace();
       System.out.println(e.getMessage());
        }
    }
}

At run time it gives an exception as follows-

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:195)
 at JDBCTest.main(first.java:14)

Mysql connector was downloaded and saved in the path- Java/jre1.8.0_91/lib/ext/

I have gone through many similar solutions available, but wasn't able to find a valid one.

hardillb
  • 54,545
  • 11
  • 67
  • 105
SKG
  • 332
  • 3
  • 23
  • Where are you running `java` from? If from a JDK directory, I think the MySQL connector should be placed there, not in the JRE folder. – Tim Biegeleisen Aug 14 '17 at 11:36
  • Add the jdbc driver to your classpath using command prompt. – Supun Amarasinghe Aug 14 '17 at 11:36
  • You should add third party libraries in a folder inside your project. Then add them directly to the classpath or create a library with them. So it's more comfortable to work with. – tec Aug 14 '17 at 11:36
  • Yeah, I tried it using inside JDK/lib directory, still it doesn't work. I am not able to understand where to place my mysql-connector-java-5.1.43 folder and set the class path. – SKG Aug 14 '17 at 11:45
  • You should not put stuff in the JDK `lib` or `lib/ext` folders. – Mark Rotteveel Aug 14 '17 at 11:47
  • Could you please tell where should I put mysql-connector-java-5.1.43 folder? and set which classpath? – SKG Aug 14 '17 at 11:49

1 Answers1

0

Add the MYSQL JDBC Driver from the library of your IDE.or place this JDBC driver file inside the lib directory of your project. Your question looks similar to this question

Amal lal T L
  • 400
  • 5
  • 20