0

I'm trying to connect to a MySQL database via the MySQL database connector using Java 1.8_45, IntelliJ IDEA 15 Ultimate, and MySQL Connector/J driver version 5.1.37 . Here's the code (sensitive information removed)

import java.sql.* ;

public class Main  {
    public static void main (String[] args) {

        Connection connect = null;

        try {
            String url = "jdbc:mysql://my_univ.edu:3306/demo";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connect = DriverManager.getConnection(url, "user", "pass"); // returning null
            System.out.println("Database connection established");
        } catch(Exception e) { // catch block is never executed
            System.err.println(e.getMessage());
            e.printStackTrace();
        } finally { // finally block is never executed
            if(connect != null) {
                try {
                    connect.close();
                    System.out.println("Database connection terminated");
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("Foo"); // this line is never executed
    }
}

I set a breakpoint on connect = DriverManager.getConnection(url, "user", "pass") and it appears as though getConnection is implemented as follows:

public Connection connect(String url, Properties info) throws SQLException {
    Properties parsedProps = this.parseFabricURL(url, info);
    if(parsedProps == null) {
        return null;
    } else {
        parsedProps.setProperty("fabricProtocol", "http");
        if(Util.isJdbc4()) {
            try {
                Constructor e = Class.forName("com.mysql.fabric.jdbc.JDBC4FabricMySQLConnectionProxy").getConstructor(new Class[]{Properties.class});
                return (Connection)Util.handleNewInstance(e, new Object[]{parsedProps}, (ExceptionInterceptor)null);
            } catch (Exception var5) {
                throw (SQLException)(new SQLException(var5.getMessage())).initCause(var5);
            }
        } else {
            return new FabricMySQLConnectionProxy(parsedProps);
        }
    }
}

This is the line that is failing: Properties parsedProps = this.parseFabricURL(url, info);

Properties parseFabricURL(String url, Properties defaults) throws SQLException {
    return !url.startsWith("jdbc:mysql:fabric://") ? null : super.parseURL(url.replaceAll("fabric:", ""), defaults);
}

The issue

  1. No idea what fabric is or why I need it. Based on these tutorials I shouldn't have it:
  2. My code stops executing after connect = DriverManager.getConnection(...)
    • Note: no exception is being thrown. connect is set to null and the code never exits

Not sure what going on with this code segment and any help is appreciated. Also, is this the only way to connect to a MySQL database on a remote server? Are there better, simpler ways of doing this?

Community
  • 1
  • 1
djthoms
  • 3,026
  • 2
  • 31
  • 56
  • 1
    What exception do you get? And which MySQL Connector/J are you using? I am not sure if MySQL Fabric support is in the normal driver or in a separate driver, if the last you might be using the wrong driver. It is expected that a JDBC driver returns `null` if it doesn't support an URL. That is how `DriverManager` and JDBC works. The driver manager tries all drivers one by one until one returns a non-null connection. If no driver accepts the URL, `DriverManager` throws an SQLException (_"No suitable driver found for ..."_) – Mark Rotteveel Nov 28 '15 at 09:11
  • @MarkRotteveel body has been updated with the Connector/J version. No exceptions are being thrown – djthoms Nov 28 '15 at 19:00
  • I am unable to recreate your issue. I just copied and pasted your code into my copy of IntelliJ IDEA Community Edition and it worked fine for me. Yes, part of the URL parsing process checks for a `:fabric` subprotocol in case you are using [MySQL Fabric](https://www.mysql.com/products/enterprise/fabric.html), but that is (apparently) normal. – Gord Thompson Nov 28 '15 at 22:38
  • @GordThompson interesting, I've been able to recreate this issue on three different machines (OS X 10.11 MacBook Pro 15" IntelliJ Ult. 15, OS X 10.11 MacBook Air IntelliJ CE 15, and Windows 10 IntelliJ CE 14.1.1) – djthoms Nov 28 '15 at 22:48
  • 1
    Can you use a machine with the MySQL client software on it to see if you can connect to the MySQL server using that? (Frankly, I'm puzzled by the whole "the code never exits" thing. It should exit *somehow*, even if the connection attempt times out as it would if one of the connection parameters was wrong or the MySQL server was simply not responding.) – Gord Thompson Nov 29 '15 at 00:22
  • 1
    It is not possible for `java.sql.DriverManager.getConnection()` to ever return null; so are you sure that you are debugging properly? The only reason I can see for `connect` being null is that you set it to null initially and you catch the `SQLException` and only print it. – Mark Rotteveel Nov 29 '15 at 09:00

0 Answers0