JDBC { public static String tryExtractServerString(String url) { if(!Strings.startsWith(url, "jdbc:", true)) { return null; } url = Urls.removeQueryString(url); int index = url.indexOf(':',5); if(index < 0) { return null; }else{ url = url.substring(index+1); } index = url.indexOf(';'); if(index > 0) { url = url.substring(0, index); } index = url.indexOf(" if(index >= 0) { url = url.substring(index + 2); }else { index = url.indexOf('@'); if(index >= 0) { url = url.substring(index + 1); } } index = url.indexOf('/'); if(index > 0) { url = url.substring(0, index); } String[] parts = Strings.split(url, ':'); if(parts.length >= 2) { return parts[0] + ":" + parts[1]; }else{ if(url.length() <= 5){ return null; } return url; } } protected  JDBC(); static void closeConnection(Connection connection); static void closeStatementOnly(Statement statement); static void closeStatementAndConnection(Statement statement); static void closeResultSetOnly(ResultSet rs); static void closeResultSetAndConnection(ResultSet rs); static void closeResultSetAndStatement(ResultSet rs); static String tryExtractServerString(String url); static final char PARAMETER_PLACEHOLDER_CHAR; static final String PARAMETER_PLACEHOLDER_STRING; }
@Test public void testExtractServerString() { assertEquals("127.0.0.1", JDBC.tryExtractServerString("jdbc:microsoft:sqlserver: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:microsoft:sqlserver: assertEquals("127.0.0.1", JDBC.tryExtractServerString("jdbc:sqlserver: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:sqlserver: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:oracle:thin:@127.0.0.1:1234:db")); assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:oracle:thin:@ assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:db2: assertEquals("127.0.0.1", JDBC.tryExtractServerString("jdbc:mysql: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:mysql: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:mysql: assertEquals("127.0.0.1", JDBC.tryExtractServerString("jdbc:h2:tcp: assertEquals("127.0.0.1", JDBC.tryExtractServerString("jdbc:h2:tcp: assertEquals("127.0.0.1:1234", JDBC.tryExtractServerString("jdbc:h2:tcp: assertEquals("file:a.db", JDBC.tryExtractServerString("jdbc:h2:file:a.db")); assertNull(JDBC.tryExtractServerString("jdbc:h2:./target/test")); assertNull(JDBC.tryExtractServerString("abcd")); }