0

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);
      }
   }
}
Sarath S Nair
  • 603
  • 2
  • 14
  • 29
  • How did u set the classpath of connector jar? and how are you executing your program? – SparkOn Aug 20 '14 at 18:14
  • http://stackoverflow.com/questions/14098404/class-not-found-exception-java-lang-classnotfoundexception-com-mysql-jdbc-driv 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

3 Answers3

1

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
Sandy
  • 81
  • 1
  • 5
0

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

SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249