Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解析Postgresql JDBC url以获取主机名、端口和db_名称_Java_Postgresql_Jdbc - Fatal编程技术网

Java 如何解析Postgresql JDBC url以获取主机名、端口和db_名称

Java 如何解析Postgresql JDBC url以获取主机名、端口和db_名称,java,postgresql,jdbc,Java,Postgresql,Jdbc,解析Postgresql JDBC URL以获取主机名、端口和数据库名称的最佳方法是什么 这是这个问题的一个具体例子:一种可能是使用解析URL。 当然,这只有在它是Postgresql URL时才有效 如果提供的URL不包含故障转移主机,则可以执行以下操作: Properties props = org.postgresql.Driver.parseURL("jdbc:postgresql:myDatabase", null); String host = props.

解析Postgresql JDBC URL以获取主机名、端口和数据库名称的最佳方法是什么


这是这个问题的一个具体例子:

一种可能是使用解析URL。 当然,这只有在它是Postgresql URL时才有效

如果提供的URL不包含故障转移主机,则可以执行以下操作:

Properties props = org.postgresql.Driver.parseURL("jdbc:postgresql:myDatabase", null);

String host   = props.getProperty(PGProperty.PG_HOST.getName());
int    port   = Integer.parseInt(props.getProperty(PGProperty.PG_PORT.getName()));
String dbName = props.getProperty(PGProperty.PG_DBNAME.getName());
上述调用返回以下属性:
{PGDBNAME=myDatabase,PGPORT=5432,PGHOST=localhost}

如果URL包含故障转移主机,则主机属性和端口属性包含多个逗号分隔的值。比如电话

Properties props = org.postgresql.Driver.parseURL("jdbc:postgresql://host1:5434,host2:5433/database", null);

将返回以下属性:
{PGDBNAME=database,PGPORT=54345433,PGHOST=host1,host2}

我很好奇,这对多主机URL有什么作用(请参见结尾处的连接故障转移)?感谢您的评论,我不知道故障转移主机。我相应地强化了答案。