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
- No idea what
fabric
is or why I need it. Based on these tutorials I shouldn't have it: - My code stops executing after
connect = DriverManager.getConnection(...)
- Note: no exception is being thrown.
connect
is set to null and the code never exits
- Note: no exception is being thrown.
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?