0

I'm trying to add an entry to a MySQL database running on my local machine via a Java program. I cannot for the life of me get the program to connect to the database I get a recurring error:

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

Everything I have read is saying the JDBC is not on the class path. I have added it to the root folder of the Java file, put it in the lib folder, and the Java file in the src folder. I tried using IntelliJ instead of Visual Studio Code to no luck.

I'm by no means an expert, and I'm sorry if this is something stupid. I have attached my code below.

package src;

import java.sql.*;

public class insertCustomer{

    public static void main(String[] args) {
        

    //database URL
        final String DATABASE_URL = "jdbc:mysql://localhost/dtca";

        Connection connection = null;
        PreparedStatement pstat = null;
        String firstname = "Daniel";
        String lastname = "Turner";
        String addressLine1 = "Garryhinch Cross";
        String addressLine2 = "Garryhinch";
        String city = "Portarlington";
        String county = "Laois";
        String postcode = "Eire";
        String email = "danielt@live.ie";
        String contactNumber = "0838068795";
        int i=0;

        try {

//establish connection to database

connection = DriverManager.getConnection(DATABASE_URL, "root","**********");

//create Prepared Statement for inserting into table


pstat = connection.prepareStatement( "INSERT INTO Customers (FirstName, LastName, AddressLine1, AddressLine2, City, County, Postcode, E-mail, ContactNumber) VALUES (?,?,?,?,?,?,?,?,?)");
pstat.setString(1,firstname);
pstat.setString(2,lastname);
pstat.setString(3,addressLine1);
pstat.setString(4,addressLine2);
pstat.setString(5,city);
pstat.setString(6,county);
pstat.setString(7,postcode);
pstat.setString(8,email);
pstat.setString(9,contactNumber);
    
i = pstat.executeUpdate();
System.out.println(i + " record successfully added to the database");

}


catch(SQLException sqlException ) {
sqlException . printStackTrace () ;
}
finally {
try{
pstat. close () ;
connection. close () ;
}
catch ( Exception exception ){
exception . printStackTrace () ;
}
}
} // end main
} // end class
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Perhaps attach the error if the answer doesn't solve for you - I have a mysql configured like below. – Mr R Mar 21 '21 at 11:52
  • 2
    Please show how you are executing your program. This happens when the driver is not on the runtime classpath (e.g. you need to use `java -cp .;mysql-connector-j.jar your.Program`). As an aside having a package called `src` seems to indicate that your project layout is malformed. – Mark Rotteveel Mar 21 '21 at 12:17
  • 1
    My guess is that the driver is not being loaded into the classpath. Try to run the program with the `-verbose:class` argument to show all loaded classes and see if the mysql driver is present in the output. – Adonias Alcântara Mar 21 '21 at 13:26

1 Answers1

-1

First of all you are missing port. Usually for mysql it is 3306, so your url should look like: DATABASE_URL = "jdbc:mysql://localhost:3306/dtca";

What is more you should register your driver before using it. You can have a look at this tutorial: JDBC

Stile
  • 31
  • 6
  • Going through the tutorial now i did previously try it with the port added to the string, this is for some of my course work and I was talking with a class mate and he didn't specify the port and has nearly identical code and it is working. Thanks for the fast response – DannTurner Mar 21 '21 at 12:04
  • 2
    Specifying the port is entirely unnecessary. When not specified, the MySQL Connector/J driver will default to port 3306. – Mark Rotteveel Mar 21 '21 at 12:14