2012-02-15 8 views
22

होस्टनाम, पोर्ट और डेटाबेस नाम प्राप्त करने के लिए मैं जेडीबीसी यूआरएल (ऑरैकल या एसक्लसेवर) का विश्लेषण कैसे कर सकता हूं। यूआरएल के प्रारूप अलग हैं।होस्टनाम, बंदरगाह आदि प्राप्त करने के लिए जेडीबीसी यूआरएल का विश्लेषण कैसे करें?

कुछ इस तरह के साथ
+0

आप जेडीबीसी यूआरएल को पार्स करना चाहते हैं तो इसका क्या मतलब है। आप रेगेक्स का उपयोग कर सकते हैं। क्या आप एक उदाहरण दे सकते हैं – Ank

+1

jdbc: jtds: sqlserver: // hostname: port/dbname या jdbc: jtds: sqlserver: // hostname: port; डेटाबेस नाम = dbname या jdbc: oracle: पतला: @ होस्टनाम: पोर्ट: dbname –

उत्तर

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

धन्यवाद बहुत कुछ! मैं इस तरह से मेजबान, आसानी से बंदरगाह प्राप्त कर सकता हूं, लेकिन मुझे डाटाबेसनाम –

+0

प्राप्त करने के लिए पथ को पार्स करना है हां, आपको यकीन है कि। – brettw

+8

आप jdbc से _some server_ को पार्स करने में सक्षम नहीं होंगे: ओरेकल: पतला: @ some.server: 1521: XXX –

6

वह मेरे लिए काम नहीं किया। मैं इन तरीकों के साथ आया, इस धारणा के आधार पर कि मेजबाननाम और बंदरगाह हमेशा एक कोलन द्वारा एक साथ जुड़ जाते हैं। यह धारणा उन सभी डेटाबेसों के लिए रखती है जिन्हें मुझे काम पर सौदा करना है (ओरेकल, वर्टिका, माईएसक्यूएल, आदि)। लेकिन शायद यह ऐसी किसी चीज़ के लिए काम नहीं करता है जो नेटवर्क पोर्ट तक नहीं पहुंचता है।

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; 
    } 
    } 
} 
संबंधित मुद्दे