1

I have the MySQL JDBC library added to my eclipse project but I still cant connect to a database, the program doesn't throw an error, it just freezes on me. Here's the link to the Library I used http://dev.mysql.com/downloads/connector/j and my current code ( username, host, and passwords omitted.

  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;

@Override
    try {
          // This will load the MySQL driver, each DB has its own driver
          Class.forName("com.mysql.jdbc.Driver");
          // Setup the connection with the DB
          connect = DriverManager
              .getConnection("jdbc:mysql://host:8080/feedback?"
                  + "user=******Admin&password=********");

          // Statements allow to issue SQL queries to the database
          statement = connect.createStatement();
          // Result set get the result of the SQL query
          System.out.println("Connected");
        } catch (Exception e) {
          System.out.println("Connection failed");

SOLUTION I had the port wrong, the driver installed improperly AND the database name was wrong. Here was the correct connection line after fixing the driver.

 connect = DriverManager
                  .getConnection("jdbc:mysql://localhost:3306/DataBase?"
                      + "user=****Admin&password=*******");
9
  • Can you use a debugger to find out where the program freezes? Commented Sep 3, 2013 at 20:37
  • Issue is probably in username and password parameters. Commented Sep 3, 2013 at 20:37
  • I run it in eclipse with my personal method of debugging (adding comments as checkpoints) and narrowed it down to the connection line as fault. When i enter intentionally wrong details for the connection it will throw an exception and it is caught in the catch. Commented Sep 3, 2013 at 20:40
  • Just tested with a faulty username and got the same result.. crashed program. The user exists.. I think? I couldnt change the MySQL root password so I created a new user in phpmyadmin Commented Sep 3, 2013 at 20:42
  • 1
    have you enabled remote access for the user? In case the server is remote ... And the port ... 8080, are you sure that's correct? Commented Sep 3, 2013 at 20:46

2 Answers 2

4

Try to connect in the format

DriverManager.getConnection("jdbc:mysql://localhost:port","username" , "password");

Please use the latest driver that fits you mysql version.

Sign up to request clarification or add additional context in comments.

Comments

1

Unless you changed the port of your MySQL server to 8080, your jdbc url is wrong. If you have not changed the default port, then you should use jdbc:mysql://host/feedback?user=******Admin&password=********.

8 Comments

the host is correct, it throws an error if it is wrong. Ive already tested that.
@user2341336 ok, but what about with the port? 8080 is usually a webcontainer (like tomcat), mysql's default port is 3306, which you can omit, if you have not changed it, this is what I am pointing on in my answer
i'll try that, Im using wamp running a webserver off of port 8080
Exception thrown and printed Connection failed.
@user2341336 by the way, if you have your mysql running locally, then you should use localhost instead of host
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.