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