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=*******");
Chris
  • 2,435
  • 6
  • 26
  • 49
  • Can you use a debugger to find out where the program freezes? – Sotirios Delimanolis Sep 03 '13 at 20:37
  • Issue is probably in username and password parameters. – Branislav Lazic Sep 03 '13 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. – Chris Sep 03 '13 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 – Chris Sep 03 '13 at 20:42
  • @brano88 This is the correct way of specifying credentials (the other is to specify them as parameters, but both work perfectly). If credentials are incorrect an exception will be thrown. – BackSlash Sep 03 '13 at 20:43
  • Just checked and the user does exist in my MySQL DB. – Chris Sep 03 '13 at 20:44
  • 1
    have you enabled [remote access](http://stackoverflow.com/questions/8380797/enable-remote-mysql-connection) for the user? In case the server is remote ... And the port ... 8080, are you sure that's correct? – Katona Sep 03 '13 at 20:46
  • The host was set to % for the user with all privileges. I'll double check – Chris Sep 03 '13 at 20:52
  • The server is running locally on my laptop – Chris Sep 03 '13 at 20:55

2 Answers2

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.

Ever Think
  • 683
  • 8
  • 22
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=********.

Katona
  • 4,816
  • 23
  • 27
  • the host is correct, it throws an error if it is wrong. Ive already tested that. – Chris Sep 03 '13 at 21:00
  • @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 – Katona Sep 03 '13 at 21:02
  • i'll try that, Im using wamp running a webserver off of port 8080 – Chris Sep 03 '13 at 21:02
  • Exception thrown and printed Connection failed. – Chris Sep 03 '13 at 21:03
  • @user2341336 by the way, if you have your mysql running locally, then you should use `localhost` instead of `host` – Katona Sep 03 '13 at 21:03
  • how can I run it with say the root user that has no password? MySQL command line errors when I try and set it so I gave up. – Chris Sep 03 '13 at 21:04
  • the real connection is localhost:8080 in the code, I omitted it for reasons I myself don't even understand – Chris Sep 03 '13 at 21:04
  • @user2341336 I think the correct url is `jdbc:mysql://localhost/feedback?user=******Admin&password=********` then – Katona Sep 03 '13 at 21:09