java怎么动态获取 jdbc:mysql://11.4.1.222/acl?autoReconnect=true&useUnicode=true&characterEncoding=utf-8 这种类型中的11.4.1.222 和 acl
时间: 2024-05-15 18:13:35 浏览: 21
可以使用Java中的正则表达式来动态获取这种类型中的IP地址和数据库名称。示例代码如下:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JDBCUrlParser {
public static void main(String[] args) {
String url = "jdbc:mysql://11.4.1.222/acl?autoReconnect=true&useUnicode=true&characterEncoding=utf-8";
String regex = "jdbc:mysql://(.*?)/(.*?)\\?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
String ip = matcher.group(1); // 获取IP地址
String dbName = matcher.group(2); // 获取数据库名称
System.out.println("IP地址:" + ip);
System.out.println("数据库名称:" + dbName);
}
}
}
```
输出结果为:
```
IP地址:11.4.1.222
数据库名称:acl
```
这里使用了正则表达式`jdbc:mysql://(.*?)/(.*?)\\?`来匹配URL中的IP地址和数据库名称,其中`(.*?)`表示使用非贪婪模式来匹配,以避免在URL中出现多个`/`时匹配到错误的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)