2012-02-15 2557 views
22

如何解析JDBC URL(oracle或sqlserver)以獲取主機名,端口和數據庫名稱。 URL的格式不同。如何解析JDBC URL以獲取主機名,端口等?

+0

你的意思是你想解析一個jdbc url。你可以使用正則表達式。您可以舉一個例子 – Ank 2012-02-15 02:25:16

+1

jdbc:jtds:sqlserver:// hostname:port/dbname或jdbc:jtds:sqlserver:// hostname:port; databaseName = dbname或jdbc:oracle:thin:@hostname:port:dbname – 2012-02-15 02:41:45

回答

35

開始是這樣的:從上面的

 
String url = "jdbc:derby://localhost:1527/netld;collation=TERRITORY_BASED:PRIMARY"; 
String cleanURI = url.substring(5); 

URI uri = URI.create(cleanURI); 
System.out.println(uri.getScheme()); 
System.out.println(uri.getHost()); 
System.out.println(uri.getPort()); 
System.out.println(uri.getPath()); 

輸出:

 
derby 
localhost 
1527 
/netld;collation=TERRITORY_BASED:PRIMARY 
+0

謝謝非常多!我可以通過這種方式輕鬆獲得主機,端口,但是我必須解析獲取數據庫名稱的路徑 – 2012-02-15 02:52:12

+0

是的,您確定。 – brettw 2012-02-15 04:30:22

+8

你不能解析_some server_出jdbc:oracle:thin:@ some.server:1521:XXX那個 – 2012-07-18 17:24:01

6

這並沒有爲我工作。我想出了這些方法,假設主機名和端口總是通過冒號連接在一起。這個假設適用於我在工作中需要處理的所有數據庫(Oracle,Vertica,MySQL等)。但它可能不適用於不能連接到網絡端口的東西。

String url = null; // set elsewhere in the class 
final public String regexForHostAndPort = "[.\\w]+:\\d+"; 
final public Pattern hostAndPortPattern = Pattern.compile(regexForHostAndPort); 
public String getHostFromUrl() { 
    Matcher matcher = hostAndPortPattern.matcher(url); 
    matcher.find(); 
    int start = matcher.start(); 
    int end = matcher.end(); 
    if(start >= 0 && end >= 0) { 
     String hostAndPort = url.substring(start, end); 
     String [] array = hostAndPort.split(":"); 
     if(array.length >= 2) 
      return array[0]; 
    } 
    throw new IllegalArgumentException("couldn't find pattern '" + regexForHostAndPort + "' in '" + url + "'"); 
} 

public int getPortFromUrl() { 
    Matcher matcher = hostAndPortPattern.matcher(url); 
    matcher.find(); 
    int start = matcher.start(); 
    int end = matcher.end(); 
    if(start >= 0 && end >= 0) { 
     String hostAndPort = url.substring(start, end); 
     String [] array = hostAndPort.split(":"); 
     if(array.length >= 2) 
      return Integer.parseInt(array[1]); 
    } 
    throw new IllegalArgumentException("couldn't find pattern '" + regexForHostAndPort + "' in '" + url + "'"); 
} 
1

我在我的項目中使用這個類。 使用非常簡單。

/** 
* Split di una url JDBC nei componenti. 
* Estrae i componenti di una uri JDBC del tipo: <br> 
* String url = "jdbc:derby://localhost:1527/netld;collation=TERRITORY_BASED:PRIMARY"; <br> 
* nelle rispettive variabili pubbliche. 
* @author Nicola De Nisco 
*/ 
public class JdbcUrlSplitter 
{ 
    public String driverName, host, port, database, params; 

    public JdbcUrlSplitter(String jdbcUrl) 
    { 
    int pos, pos1, pos2; 
    String connUri; 

    if(jdbcUrl == null || !jdbcUrl.startsWith("jdbc:") 
     || (pos1 = jdbcUrl.indexOf(':', 5)) == -1) 
     throw new IllegalArgumentException("Invalid JDBC url."); 

    driverName = jdbcUrl.substring(5, pos1); 
    if((pos2 = jdbcUrl.indexOf(';', pos1)) == -1) 
    { 
     connUri = jdbcUrl.substring(pos1 + 1); 
    } 
    else 
    { 
     connUri = jdbcUrl.substring(pos1 + 1, pos2); 
     params = jdbcUrl.substring(pos2 + 1); 
    } 

    if(connUri.startsWith("//")) 
    { 
     if((pos = connUri.indexOf('/', 2)) != -1) 
     { 
     host = connUri.substring(2, pos); 
     database = connUri.substring(pos + 1); 

     if((pos = host.indexOf(':')) != -1) 
     { 
      port = host.substring(pos + 1); 
      host = host.substring(0, pos); 
     } 
     } 
    } 
    else 
    { 
     database = connUri; 
    } 
    } 
}