anupam
anupam

Reputation: 3

MySql (MAMP) jdbc connection error in eclipse

I am trying to set up the connection with MySQL in Eclipse. I have tried the following -

  1. I have added the "my-sql-connector.jar" to the build path and class path.
  2. I have tried adding it in the WEB-INF.
  3. Tried using class.forName.

After trying all the above, still I am not able to set up the connection. I keep getting the following error -

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

Here is my code -

public class Main {

    private static final String USERNAME = "user1";
    private static final String PASSWORD = "user1";
    private static final String CONN_STRING =
            "jdbc:mysql://localhost/sampledb";

    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            System.out.println("Connected");
        } catch (SQLException e) {

            System.err.println(e);
        } finally { 
            if (conn != null) {
                conn.close();
            }
        }

I have seen multiple posts on this issue and I have tried all of the suggestions. I am new to Java and trying to get a hands on Java and JDBC, but I am stuck with it for last 2 days. Can someone please guide me with this issue?

Upvotes: 0

Views: 928

Answers (1)

user5310198
user5310198

Reputation: 36

Did you try registering your Driver? And check if your driver is proper.

Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

Upvotes: 2

Related Questions