Tuesday, April 16, 2013

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure on Mysql, MAMP and MAC OS

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure on Mysql, MAMP and MAC OS
I just created the a new user in phpmyadmin with all the privileges to all the databases.








and

I used the dependency

    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

Which didn't work and throwed the exception. After some googling find the solution. I just changed the mysql connector dependency version. Now it is working.

    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.24</version>
    </dependency>



    public void testTheApp() throws Exception {
        Connection conn = null; 
        try {
//            WARNING: Could not obtain connection metadata
//            java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:
            System.out.println("AppTest started.... ");
            Class.forName("com.mysql.jdbc.Driver");
            Properties connectionProperties = new Properties();
            connectionProperties.put("user", "java");
            connectionProperties.put("password", "java");
            conn = DriverManager.getConnection("jdbc:mysql://0.0.0.0:8889/java", connectionProperties);
            System.out.println("Connection success ...... ");
        } catch (Exception e) {
            e.printStackTrace();
        } finally 
        { 
            if (conn != null) 
            { 
                try 
                { 
                    conn.close (); 
                    System.out.println ("Database connection terminated"); 
                } 
                catch (Exception e) { /* ignore close errors */ } 
            } 
        }
    }

jdbc:mysql://0.0.0.0:8889/java (Works)jdbc:mysql://172.16.23.59:8889/java (Works)
jdbc:mysql://172.0.0.1:8889/java (Wont work)
Happy Coding :)

No comments:

Post a Comment