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);
      }
   }
}
3
  • How did u set the classpath of connector jar? and how are you executing your program? Commented Aug 20, 2014 at 18:14
  • stackoverflow.com/questions/14098404/… Same problem it seems Commented Aug 20, 2014 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 Commented Aug 20, 2014 at 18:28

3 Answers 3

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
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.