Andreas
Andreas

Reputation: 189

XAMPP jdbc drivers do not load

I made a simple MySQL connection program but it doesn't work.

package main;
import java.sql.*;

public class Main {

    static Connection con = null;
    static Statement stmt = null;
    static ResultSet rs = null;
    static String url = "jdbc:mysql://localhost:3306/phpmyadmin";
    static String user = "root";
    static String password = "(i dont show this ;)";

    public static void main(String[] args) {

        try {
            System.out.println("Connecting database...");
            con = DriverManager.getConnection(url, user, password);
            System.out.println("Database connected!");
        } catch (SQLException e) {
            throw new RuntimeException("Cannot connect the database!", e);
        } finally {
            System.out.println("Closing the connection.");
            if (con != null) try { con.close(); } catch (SQLException ignore) {}
        }

    }

}

I got this error:

Connecting database...
Closing the connection.
Exception in thread "main" java.lang.RuntimeException: Cannot connect the database!
at main.Main.main(Main.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/phpmyadmin
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at main.Main.main(Main.java:19)

I made the buildpath, and I moved the files (from the connector) from the lib folder to \xampp\mysql\lib. I also started tomcat (I didn't change any configs) and it is still not working.

Upvotes: 2

Views: 5431

Answers (1)

Deepak
Deepak

Reputation: 2895

You are missing some important steps like following

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

You can use the below link for complete instructions

http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

 import java.sql.DriverManager;
 import java.sql.Connection;
 import java.sql.SQLException;

 public class JDBCExample {

   public static void main(String[] argv) {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    System.out.println("Where is your MySQL JDBC Driver?");
    e.printStackTrace();
    return;
}

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
    connection = DriverManager
    .getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");
    e.printStackTrace();
    return;
}

  if (connection != null) {
    System.out.println("You made it, take control your database now!");
  } else {
    System.out.println("Failed to make connection!");
  }
}
} 

Upvotes: 5

Related Questions