0

I have settled MAMP on my machine and last time when I used that code on my old computer and with another database that works pretty well. Now I cannot understand where I am wrong.

public class JDBC {
private static String DBURL = "jdbc:mysql://localhost:3306/BookReservationDatabase";
private static String DBLOGIN = "root";
private static String DBPASSWORD = "root";

public static void main(String[] args) {
    // Step1: register JDBC driver
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    // Step 2: Establish the connection to the database
    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
        // Step 3: Create statements (SQL queries)
        stmt = conn.createStatement();
        // Step 4: Execute statements and retrieve results
        rset = stmt.executeQuery("SELECT * FROM Users");
        // Step 5: Analyze results
        while (rset.next()) {

        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // Step 6: Release resources
        try {
            if (rset != null)
                rset.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (stmt != null)
                stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}

My database name is BookReservationDatabase, I use root as a login and password on PhpMyAdmin.

I have got the following errors :

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at features.JDBC.main(JDBC.java:26)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301)
    ... 15 more

The error must be among DBURL,DBLOGIN and DBPASSWORD. Because the code generate error at the connection

conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
MWiesner
  • 8,868
  • 11
  • 36
  • 70
Ilan
  • 729
  • 3
  • 8
  • 17
  • 1
    According to error stacktrace, you are not able to make DB connection with given credentials. Please check your url, username and password. Make suer your MySQL server is running on 3306 port and username and password is correct – hiren Apr 14 '18 at 09:31
  • @Hiren The error is not about the credentials. This would in a more specific exception about invalid credentials. – MWiesner Apr 14 '18 at 09:36
  • When I start Mamp, on the start web page there are some information about paramèters to use in order to connect to the database : Host => localhost ; Login => root ; Password => root and Port => 3306. For me the credential are all correct – Ilan Apr 14 '18 at 09:38

1 Answers1

4

java.net.ConnectException: Connection refused (Connection refused)

clearly signals a communication error on the machine on which you run this code. This could be caused by many reasons, thus I merely list the most likely ones:

  1. MySQL is not running at all.
    • Did you start it? Verify by inspecting running process lists (depends on OS).
  2. MySQL is not listening on Port 3306 which is the default port.
    • Did you change it? If so, change your code/config accordingly.
  3. In case a firewall is active, check the rules that could prevent connections on port 3306.

Hope it helps.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • see also a more detailed answer by BalusC: https://stackoverflow.com/a/2985169/2849346 – MWiesner Apr 14 '18 at 09:38
  • 1. On the MAMP windows, I can see that MySQL is running because of the green icon. 2. This point correct my problem ;) I have gone to PhpMyAdmin console and tap in SHOW GLOBAL VARIABLES LIKE 'PORT'; I can see that the port number is wrong so I correct it. – Ilan Apr 14 '18 at 09:49
  • Glad, it helped you. :) – MWiesner Apr 14 '18 at 09:50