abcooper
abcooper

Reputation: 173

Why am I receiving this SQLException error stating that no suitable driver can be found?

I'm on a mac, running a MAMP instance of MySQL. I'm trying to use a jdbc driver to connect my java code to a database called 'test', working with a table called 'customer.' I keep getting an error:

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

I'm not sure if the problem is with my code, or if it's a configuration problem with the MAMP instance of MySQL, or if it's something else entirely.

I have an initialize driver method:

public void initializeDriver(){
     try{
      Class.forName("com.mysql.jdbc.Driver");
      }
     catch(ClassNotFoundException e) {
        System.err.println(e.toString());
     }

 }

And I have a connection created in the following way:

public void insertCustomer(String connectionUrl, String connectionUser, String connectionPassword, Customer customer) {

         try{
            Connection conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
            Statement constat = conn.createStatement();
            String query = "INSERT INTO customers (customer_id, email, deliverable, create_date) VALUES (" + customer.id + ", " + customer.emailAddress + ", " + customer.deliverable + ", " + customer.createDate + ")" ;
            constat.executeQuery(query);
            conn.close();

         }
         catch(SQLException e){
             System.out.println(e.toString());
         }


     }

And I have downloaded mysql-connector-java-5.1.20 and set it in my classpath.

If anyone has any suggestions for how I could correct this error, I would be really grateful!

Upvotes: 1

Views: 257

Answers (4)

Karim Massi
Karim Massi

Reputation: 77

you have also a error in this row

    constat.executeQuery(query);

if you want insert some data in data base you have to use this code

constat.executeUpdate(query);

Upvotes: 0

LyB8899
LyB8899

Reputation: 323

Try to add mysql-connector-java-5.1.20.jar to Glassfish (or Tomcat) lib folder.

Upvotes: 0

aleroot
aleroot

Reputation: 72676

You have to put MySQL jdbc connector jar library into the classpath.

Then initialize the driver before opening the connection with code like the following :

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

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272397

You will need the corresponding mysql JDBC driver jar in your classpath or loadable by your container. See the doc for ConnectorJ and note the installation instructions.

Upvotes: 0

Related Questions