0

I am working on Databases I made a project in Eclipse Luna IDE. My database is in mysql. I downloaded the connector/j file and add this jar file to my project. This is jar file full name "mysql-connector-java-5.1.35-bin.jar". But when i run the project it will display this error message "no suitable driver found for jdbc:mysql//localhost/books". books is my database name. Here is my java code.

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class DisplayAuthors {


    static final String DATABASE_URL = "jdbc:mysql//localhost/books";

    public static void main(String[] args)  {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {


            connection = DriverManager.getConnection( DATABASE_URL, "saadi", "saadi" );

            statement = connection.createStatement();

            resultSet = statement.executeQuery( "SELECT AuthorID, FirstName, LastName FROM Authors" );

            ResultSetMetaData metaData = resultSet.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            System.out.println( "Authors Table of Book Database:\n" );

            for ( int i = 1; i < numberOfColumns; i++ ) 
                System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
            System.out.println();

            while ( resultSet.next() ) {
                for ( int i = 1; i <= numberOfColumns; i++ ) 
                    System.out.printf( "%-8s\t", resultSet.getObject( i ) );
                System.out.println();
            }
        }

        catch ( SQLException exception ) {
            exception.printStackTrace();
        }

        finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            }

            catch ( Exception exception ) {
                exception.printStackTrace();
            }
        }
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Saad
  • 97
  • 8

1 Answers1

0

You have an error in the

DATABASE_URL = "jdbc:mysql//localhost/books"

: is missing .Correct one is

DATABASE_URL = "jdbc:mysql://localhost/books"

Also its kind of usual practice to first load the class before connection manager.

Class.forName("com.mysql.jdbc.Driver");

Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29