Come analizzare un URL JDBC (oracle o sqlserver) per ottenere nome host, porta e nome del database. I formati dell'URL sono diversi.Come analizzare un URL JDBC per ottenere nome host, porta ecc.?
risposta
Inizia con qualcosa di simile:
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());
uscita da quanto sopra:
derby localhost 1527 /netld;collation=TERRITORY_BASED:PRIMARY
grazie molto! Posso ottenere host, port facilmente in questo modo, ma devo analizzare il percorso per ottenere il nome del database –
Sì, certo. – brettw
non sarai in grado di analizzare _some server_ da jdbc: oracle: thin: @ some.server: 1521: XXX con quello –
che non ha funzionato per me. Mi sono inventato questi metodi, partendo dal presupposto che il nome host e la porta siano sempre uniti da due punti. Questo presupposto vale per tutti i database con i quali lavoro (Oracle, Vertica, MySQL, ecc.). Ma probabilmente non funziona per qualcosa che non raggiunge una porta di rete.
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 + "'");
}
Uso questa classe nei miei progetti. L'uso è davvero semplice.
/**
* 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;
}
}
}
- 1. Come analizzare un URL Web?
- 2. Come posso ottenere un nome host (con porta) che sia un servlet a
- 3. Come impostare il nome dell'applicazione in un url JDBC Postgresql?
- 4. come analizzare il nome dell'immagine da un'immagine url
- 5. : Nome host errato per gli helper URL in rspec
- 6. Come ottenere il nome host completo + numero di porta in Application_Start di Global.aspx?
- 7. Beautiful Soup per analizzare l'url per ottenere altri dati URL
- 8. ColdFusion: ottenere parametro url per nome
- 9. URI non valido: Il nome host Impossibile analizzare
- 10. Come analizzare un URL Zend per i parametri?
- 11. Ottenere nome di dominio di un URL in C#/.NET
- 12. Come accedere al nome host, alle intestazioni http, ecc. Da un server websocket Java?
- 13. MVC4 bundle per nome host
- 14. Caratteri validi di un nome host?
- 15. Nome host alias per localhost
- 16. ZF2: Zend Framework 2 URL completo incluso nome host
- 17. Come ottenere JSON in linea per analizzare
- 18. nome host errata eccezione
- 19. Come analizzare l'URL per ottenere l'URL di base? - Rails 3.1
- 20. Riscrittura Url con IIS 6/7 - Riscrivi nome host {HTTP_HOST}
- 21. URL JDBC per client Oracle XA
- 22. Come ottenere il nome host nel modello freemarker?
- 23. Come ottenere il nome host in base all'indirizzo IP?
- 24. Posso mappare un nome host * e * una porta con/etc/hosts?
- 25. Come analizzare un URL di dati nel nodo?
- 26. Come posso analizzare un URL di YouTube utilizzando XSLT?
- 27. Proxy HTTP Apache basato sul nome host
- 28. Ottenere Nome file da URL in C#
- 29. Come accedere alla porta host dal contenitore docker
- 30. Come utilizzare TinyXml per analizzare un elemento specifico
Che cosa intendi per analisi di un URL jdbc. Puoi usare regex. Puoi dare un esempio – Ank
jdbc: jtds: sqlserver: // nomehost: porta/nomebd o jdbc: jtds: sqlserver: // nomehost: porta; databaseName = nomebc o jdbc: oracle: thin: @nomehost: porta: nomebaby –