Hi, i am trying to connect to MySQL database mysql using JAVA .. using code given below,

import java.sql.*;

public class dbExample{
  public static void main(String[] args) {
    System.out.println("Listing all table name in Database!");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "mydb";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";
    try{
      Class.forName(driver);
      con = DriverManager.getConnection(url+db, user, pass);
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

I've picked this example from internet .. and there lot more example i tried using this same driver String driver = "com.mysql.jdbc.Driver"; But this code all other i tried gives me error Driver Class not found ... at this statment Class.forName(driver); ..... and similarly on trying this driver sun.jdbc.odbc.JdbcOdbcDriver the above mentioned statment Class.fonName ... executes but then it gives error on statment con = DriverManager.getConnection(url+db, user, pass); error is No suitable drive found for jdbc:mysql://localhost:3306/mydb I'm very much confused and tried it toooooo many time in diffrents ways that i could but still unable to create successfull database connection.
I've tried this using notepad / Netbeans both and both on WINDOWS7 and XP ... and both give same error to me .... :( :( :( :(

Please Please PLease give me any solution .... I shell be very very Thanksfull to you.

Did you download MySQL.jar file ?
If so is this .jar in your PATH variable or in the External Jar folder of your IDE if you use an IDE ?

@'pbl,

No .. I didn't added MySQL.jar,
I've just created database using 'MySQL Command Line Client' and create application using netbeans IDE.
Can you please little explane about this MySQL.jar issue ... may be this was my mistake.

THANKS 'pbl'

THANKS A LLLLLOT 'pbl' MY PROBLEM SOLVED BY ADDING MySQL.jar
Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.