Java MySQL connection not working: No suitable driver found

176 Views Asked by At

So I am trying to make a connection with JDBC using XAMPP, but it doesn't work, What am I doing wrong here?

public static void main(String args[]) {
        try {

            Connection myConn = DriverManager.getConnection(
                    "http://localhost/phpmyadmin/sql.php?db=a3_eindopdracht_2&table=namen&pos=0", "", "");
            Statement myStm = myConn.createStatement();
            ResultSet myRs = myStm.executeQuery("SELECT * FROM namen");
            while (myRs.next()) {
                System.out.println(myRs.getString("voornaam") + " " + myRs.getString("achternaam"));
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
1

There are 1 best solutions below

0
On

Firstly, you don't find Driver. You have to load them, by calling the Drive class like that :

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    // Cannot find driver for MySQL
}    

Then, you are trying to connect to your databse with HTTP protocol. But, DB have they own (with port 3306 by default), so you have to use address like that:

jdbc:mysql://myserver.com/schema

Finally: don't forget to add username and password on last 2 fields of getConnection method