Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I wanted to print the contents in a database but whenever I run this program I got this error saying that

Class not found java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
SQL exception occuredjava.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydatabase

I have installed MySQL from this link http://dev.mysql.com/downloads/windows/installer/ its a 248mb file and installed completely. I can access my database within MySQL but can't able to access from netbeans. I separately downloaded the mysql-connector-java-5.1.4.jar and set the CLASSPATH but now also I got this error.

import java.sql.*;

public class jdbcResultSet {
   public static void main(String[] args) {
      try {
         Class.forName("com.mysql.jdbc.Driver");
      }
      catch(ClassNotFoundException e) {
         System.out.println("Class not found "+ e);
      }
      try {  
         Connection con = DriverManager.getConnection
         ("jdbc:mysql://localhost:3306/mydatabase","root",
         "root");
         Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery
         ("SELECT * FROM employee");
         System.out.println("id  name    job");
         while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String job = rs.getString("job");
            System.out.println(id+"   "+name+"    "+job);
         }
      }
      catch(SQLException e){
         System.out.println("SQL exception occured" + e);
      }
   }
}
share|improve this question
    
How did u set the classpath of connector jar? and how are you executing your program? – SparkOn Aug 20 '14 at 18:14
    
stackoverflow.com/questions/14098404/… Same problem it seems – PT_C Aug 20 '14 at 18:17
    
I downloaded the mysql-connector-java-5.1.4.jar file and placed in "C:\Program Files\MySQL\Java Connector" directory and edit System Environment Variable from control panel then set CLASSPATH as C:\Program Files\MySQL\Java Connector – Sarath S Nair Aug 20 '14 at 18:28
up vote 0 down vote accepted

The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp, -classpath, -jar arguments. It is ignored by IDEs.

So if you are running the application from your IDE place the mysql-connector.jar in your IDE's classpath

share|improve this answer

I downloaded the mysql-connector-java-5.1.4.jar file and placed in "C:\Program Files\MySQL\Java Connector" directory and edit System Environment Variable from control panel then set CLASSPATH as C:\Program Files\MySQL\Java Connector

Add the jar file, not the directory that the jar file is in.

Instead of,

C:\Program Files\MySQL\Java Connector

the CLASSPATH should include,

C:\Program Files\MySQL\Java Connector\mysql-connector-java-5.1.4.jar
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.