Sarath S Nair
Sarath S Nair

Reputation: 613

Class not found java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

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);
      }
   }
}

Upvotes: 0

Views: 7148

Answers (3)

Sandy
Sandy

Reputation: 106

First Check can you import import com.microsoft.sqlserver.jdbc.SQLServerDriver;

if it say com.microsft can not be resolved to a type that means u have to add your jar files to java build path and after that to deployment path from project properties.


java build path

  • Right click on project=>Properties=>Java Build Path=> Librariess=>ADD External Jar file

deployment path

  • Right click on project=>Properties=>Deployment Assembly=> ADDs=>Java Build Path Entries==> mssql_jdbc

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201409

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

Upvotes: 0

SparkOn
SparkOn

Reputation: 8946

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

Upvotes: 0

Related Questions