0

I am trying to connect my Azure Database using JDBC. But it fails to connect. I have used server name,username,database name and password correctly.

using Mysql Connector v8.0.13

import java.sql.*;
import java.util.Properties;


public class MyConnection {

    public static Connection getConnection() {


        System.out.println("MySQL JDBC driver detected in library path.");

        Connection connection = null;

        try
        {


            String url ="jdbc:mysql://<servername>:3306/<databaseName>?useSSL=true&requireSSL=false"; 
            connection = DriverManager.getConnection(url, <username>, <password>);
        }
        catch (SQLException e)
        {
            //throw new SQLException("Failed to create connection to database.", e);
        }
        if (connection != null) 
        { 
            System.out.println("Successfully created connection to database.");

        }
        else {
            System.out.println("Failed to create connection to database.");
        }
        System.out.println("Execution finished.");


        return connection;
    }
}

I was expecting to show "Successfully created connection to database." but its showing "Failed to create connection to database."

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • You have your exception trap commented out. If it’s throwing one, it is being swallowed. If it wasn’t being swallowed, it could contain a lot of good information to tell you what’s going on. – Paul Sturm Jul 14 '19 at 21:48
  • Did you followed this Azure doucment [Azure Database for MySQL: Use Java to connect and query data](https://learn.microsoft.com/en-us/azure/mysql/connect-java#get-connection-information)? – Leon Yue Jul 15 '19 at 02:15
  • Thanks to Everyone. I able to connect the database. It was showing "Time Zone" error. I followed the Stirng url like this: String url ="jdbc:mysql://:3306/?useSSL=true&requireSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; – Lomat Haider Jul 15 '19 at 06:50

1 Answers1

0

Just as summary for the comments and my additional content.

As @LomatHaider said, the issue caused from Java code throwed an error about Time Zone. According to the MySQL offical document MySQL Server Time Zone Support, as below.

Time Zone Variables

MySQL Server maintains several time zone settings:

  • The system time zone. When the server starts, it attempts to determine the time zone of the host machine automatically and uses it to set the system_time_zone system variable. The value does not change thereafter.

    To explicitly specify the system time zone for MySQL Server at startup, set the TZ environment variable before you start mysqld. If you start the server using mysqld_safe, its --timezone option provides another way to set the system time zone. The permissible values for TZ and --timezone are system dependent. Consult your operating system documentation to see what values are acceptable.

  • The server current time zone. The global time_zone system variable indicates the time zone the server currently is operating in. The initial time_zone value is 'SYSTEM', which indicates that the server time zone is the same as the system time zone.

So if never set Time Zone variable for MySQL, the default time zone setting in MySQL is followed to the OS time zone. As I known, the default time zone on Azure is UTC, then the time zone of client connection on-premise or on local may will be different with MySQL on Azure, the confliction caused this issue information like as below.

java.sql.SQLException: The server time zone value 'Malay Peninsula Standard Time' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

So the solution to fix it is as the two options below:

  1. Follow the MySQL offical document to SET GLOBAL time_zone = timezone; by the SUPER privilege.
  2. Add the additional parameters ?useTimezone=true&serverTimezone=UTC in connection string to force use the same time zone value for JDBC connection session.

There is a blog MySQL JDBC Driver Time Zone Issue :: \[SOLVED\] introduced the same issue with this one, and to fix it by the second option above.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43