import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JdbcUrlParseUtils {
/*
* mysql
* jdbc:mysql://${host}:${port}/${database}
* postgresql
* jdbc:postgresql://${host}:${port}/${database}
* sqlserver
* jdbc:sqlserver://${host}:${port};databaseName=${database}
* jdbc:microsoft:sqlserver://${host}:${port};databaseName=${database}
* Oracle
* service name方式
* jdbc:oracle:thin:@//${host}:${port}/${database}
* sid方式
* jdbc:oracle:thin:@${host}:${port}:${database}
* tns name方式
* jdbc:oracle:thin:@${tnsname}
*/
public static final String DB = "database";
public static final String HOST = "host";
public static final String PORT = "port";
public static final String ORACLE_SID = "sid";
public static final String ORACLE_SERVER_NAME = "service name";
public static final String ORACLE_TNS_NAME = "tns name";
public static final String ORACLE_CONN_TYPE = "oracleConnType";
public static Map<String, String> parseMysqlUrl(String url) {
Pattern mysqlPattern = Pattern.compile("jdbc:mysql://([.\\w]+):(\\d+)/(\\w+)(\\?.*)?");
Matcher matcher = mysqlPattern.matcher(url);
String host = null;
String port = null;
String db = null;
if (matcher.find()) {
host = matcher.group(1);
port = Integer.parseInt(matcher.group(2)) + "";
db = matcher.group(3);
}
Map<String, String> map = new HashMap<>();
map.put(DB, db);
map.put(HOST, host);
map.put(PORT, port);
return map;
}
public static Map<String, String> parsePostgreUrl(String url) {
Pattern postgrePattern = Pattern.compile("jdbc:postgresql://([.\\w]+):(\\d+)/(\\w+)(\\?.*)?");
Matcher matcher = postgrePattern.matcher(url);
String host = null;
String port = null;
String db = null;
if (matcher.find()) {
host = matcher.group(1);
port = Integer.parseInt(matcher.group(2)) + "";
db = matcher.group(3);
}
Map<String, String> map = new HashMap<>();
map.put(DB, db);
map.put(HOST, host);
map.put(PORT, port);
return map;
}
public static Map<String, String> parseSqlserverUrl(String url) {
Pattern sqlserverPattern = Pattern.compile("jdbc:(microsoft:)?sqlserver://([.\\w]+):(\\d+);DatabaseName=(\\w+)(;.*)?");
Matcher matcher = sqlserverPattern.matcher(url);
String host = null;
String port = null;
String db = null;
if (matcher.find()) {
host = matcher.group(2);
port = Integer.parseInt(matcher.group(3)) + "";
db = matcher.group(4);
}
Map<String, String> map = new HashMap<>();
map.put(DB, db);
map.put(HOST, host);
map.put(PORT, port);
return map;
}
public static Map<String, String> parseOracleUrl(String url) {
String SID_PATTERN = "jdbc:oracle:thin:@([.\\w]+):(\\d+):([\\w]+)";
String SERVICE_NAME_PATTERN = "jdbc:oracle:thin:@//([.\\w]+):(\\d+)/([\\w]+)";
String TNS_NAME_PATTERN = "jdbc:oracle:thin:@(\\w+)";
List<String> patterns = Arrays.asList(SID_PATTERN, SERVICE_NAME_PATTERN, TNS_NAME_PATTERN);
Map<String, String> map = new HashMap<>();
for (String pattern : patterns) {
Pattern oraclePattern = Pattern.compile(pattern);
Matcher matcher = oraclePattern.matcher(url);
// String host = null;
if (matcher.find()) {
if (pattern.equals(SID_PATTERN)) {
String host = matcher.group(1);
String port = Integer.parseInt(matcher.group(2)) + "";
String db = matcher.group(3);
map.put(HOST, host);
map.put(PORT, port);
map.put(DB, db);
map.put(ORACLE_CONN_TYPE, ORACLE_SID);
} else if (pattern.equals(SERVICE_NAME_PATTERN)) {
String host = matcher.group(1);
String port = Integer.parseInt(matcher.group(2)) + "";
String db = matcher.group(3);
map.put(HOST, host);
map.put(PORT, port);
map.put(DB, db);
map.put(ORACLE_CONN_TYPE, ORACLE_SERVER_NAME);
} else {
String host = matcher.group(1);
map.put(HOST, host);
map.put(ORACLE_CONN_TYPE, ORACLE_TNS_NAME);
}
break;
}
}
return map;
}
}
传统关系数据库的jdbc url解析工具
于 2023-07-27 23:49:04 首次发布