0

I am using Eclipse LUNA package and MySQL 6.0 Workbench.

I pasted "mysql-connector-java-5.0.8-bin.jar" file in Referenced Library file.

My coding:

package a1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
class a2 {
  public static void main(String[] args) {
    String dbURL = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "root";
    Connection dbCon = null;
    Statement stmt = null;
    ResultSet rs = null;
    String query = "select * from k111";
    try {
      dbCon = DriverManager.getConnection(dbURL, username, password);
      stmt = dbCon.prepareStatement(query);
      rs = stmt.executeQuery(query);
      System.out.println("Success");
      while (rs.next()) {
        System.out.println(query);
      }
    } catch (SQLException ex) {
      System.out.println(ex);
    } finally {
      rs.close();
      stmt.close();
      dbCon.close();
    }
  }
}

My database name is "test". My table name is k111.

I need to print "Success" as per the program to know whether the database connectivity is successful.

My output:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

starball
  • 20,030
  • 7
  • 43
  • 238
kalyan
  • 37
  • 1
  • 6

1 Answers1

0

Your jdbc URL might be wrong try something like that:

String dbURL = "jdbc:mysql://localhost:3306/test";   

Supply url to your mysql folder

dharr
  • 328
  • 1
  • 11
  • This is also not working...... Also, my database name is "test" .... So only I put test in that place..... May I know the reason to change it to "mysql"....???? – kalyan Jul 15 '14 at 15:27
  • Oh sorry haha you might take a look on that http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql/2840358#2840358 – dharr Jul 15 '14 at 15:34
  • ok..... when I add "Class.forName("com.mysql.jdbc.Driver");" in my code, I got cleared that exception........ Thanks for your kind response – kalyan Jul 15 '14 at 15:41
  • Sorry for my mistake :) – dharr Jul 15 '14 at 15:47