简体   繁体   中英

Mysql DB connection with JDBC freezing program

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=*******");

Try to connect in the format

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

Please use the latest driver that fits you mysql version.

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=******** .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM