`

postgresql jdbc连接参数

 
阅读更多

 

jdbc:postgresql://192.168.1.23:12308/test?useUnicode=true&characterEncoding=gbk&allowEncodingChanges=true"

见官网:https://jdbc.postgresql.org/documentation/documentation.html

Connecting to the Database

With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms:

  • jdbc:postgresql:database
  • jdbc:postgresql://host/database
  • jdbc:postgresql://host:port/database

The parameters have the following meanings:

  • host

    The host name of the server. Defaults to localhost. To specify an IPv6 address your must enclose the host parameter with square brackets, for example:

    jdbc:postgresql://[::1]:5740/accounting

  • port

    The port number the server is listening on. Defaults to the PostgreSQL™ standard port number (5432).

  • database

    The database name.

To connect, you need to get a Connection instance from JDBC. To do this, you use the DriverManager.getConnection() method:

Connection db = DriverManager.getConnection(url, username, password);

Connection Parameters

In addition to the standard connection parameters the driver supports a number of additional properties which can be used to specify additional driver behaviour specific to PostgreSQL™. These properties may be specified in either the connection URL or an additional Properties object parameter to DriverManager.getConnection. The following examples illustrate the use of both methods to establish a SSL connection.

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);

String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);

 

  • user = String

    The database user on whose behalf the connection is being made.

  • password = String

    The database user's password.

  • ssl

    Connect using SSL. The driver must have been compiled with SSL support. This property does not need a value associated with it. The mere presence of it specifies a SSL connection. However, for compatibility with future versions, the value "true" is preferred. For more information see Chapter 4, Using SSL.

  • sslfactory = String

    The provided value is a class name to use as the SSLSocketFactory when establishing a SSL connection. For more information see the section called “Custom SSLSocketFactory”.

  • sslfactoryarg = String

    This value is an optional argument to the constructor of the sslfactory class provided above. For more information see the section called “Custom SSLSocketFactory”.

  • compatible = String

    Act like an older version of the driver to retain compatibility with older applications. At the moment this controls two driver behaviours: the handling of binary data fields, and the handling of parameters set via setString().

    Older versions of the driver used this property to also control the protocol used to connect to the backend. This is now controlled by the protocolVersion property.

    Information on binary data handling is detailed in Chapter 7, Storing Binary Data. To force the use of Large Objects set the compatible property to 7.1.

    When compatible is set to 7.4 or below, the default for the stringtype parameter is changed to unspecified.

  • sendBufferSize = int

    Sets SO_SNDBUF on the connection stream

  • recvBufferSize = int

    Sets SO_RCVBUF on the connection stream

  • protocolVersion = String

    The driver supports both the V2 and V3 frontend/backend protocols. The V3 protocol was introduced in 7.4 and the driver will by default try to connect using the V3 protocol, if that fails it will fall back to the V2 protocol. If the protocolVersion property is specified, the driver will try only the specified protocol (which should be either "2" or "3"). Setting protocolVersion to "2" may be used to avoid the failed attempt to use the V3 protocol when connecting to a version 7.3 or earlier server, or to force the driver to use the V2 protocol despite connecting to a 7.4 or greater server.

  • loglevel = int

    Set the amount of logging information printed to the DriverManager's current value for LogStream or LogWriter. It currently supports values of org.postgresql.Driver.DEBUG (2) and org.postgresql.Driver.INFO (1). INFO will log very little information while DEBUG will produce significant detail. This property is only really useful if you are a developer or are having problems with the driver.

  • charSet = String

    The character set to use for data sent to the database or received from the database. This property is only relevant for server versions less than or equal to 7.2. The 7.3 release was the first with multibyte support compiled by default and the driver uses its character set translation facilities instead of trying to do it itself.

  • allowEncodingChanges = boolean

    When using the V3 protocol the driver monitors changes in certain server configuration parameters that should not be touched by end users. The client_encoding setting is set by the driver and should not be altered. If the driver detects a change it will abort the connection. There is one legitimate exception to this behaviour though, using the COPY command on a file residing on the server's filesystem. The only means of specifying the encoding of this file is by altering the client_encoding setting. The JDBC team considers this a failing of the COPY command and hopes to provide an alternate means of specifying the encoding in the future, but for now there is this URL parameter. Enable this only if you need to override the client encoding when doing a copy.

  • logUnclosedConnections = boolean

    Clients may leak Connection objects by failing to call its close() method. Eventually these objects will be garbage collected and the finalize() method will be called which will close the Connection if caller has neglected to do this himself. The usage of a finalizer is just a stopgap solution. To help developers detect and correct the source of these leaks the logUnclosedConnections URL parameter has been added. It captures a stacktrace at each Connection opening and if the finalize() method is reached without having been closed the stacktrace is printed to the log.

  • binaryTransferEnable = String

    A comma separated list of types to enable binary transfer. Either OID numbers or names.

  • binaryTransferDisable = String

    A comma separated list of types to disable binary transfer. Either OID numbers or names. Overrides values in the driver default set and values set with binaryTransferEnable.

  • prepareThreshold = int

    Determine the number of PreparedStatement executions required before switching over to use server side prepared statements. The default is five, meaning start using server side prepared statements on the fifth execution of the same PreparedStatement object. More information on server side prepared statements is available in the section called “Server Prepared Statements”.

  • loginTimeout = int

    Specify how long to wait for establishment of a database connection. The timeout is specified in seconds.

  • connectTimeout = int

    The timeout value used for socket connect operations. If connecting to the server takes longer than this value, the connection is broken. The timeout is specified in seconds and a value of zero means that it is disabled.

  • socketTimeout = int

    The timeout value used for socket read operations. If reading from the server takes longer than this value, the connection is closed. This can be used as both a brute force global query timeout and a method of detecting network problems. The timeout is specified in seconds and a value of zero means that it is disabled.

  • tcpKeepAlive = boolean

    Enable or disable TCP keep-alive probe. The default is false.

  • unknownLength = int

    Certain postgresql types such as TEXT do not have a well defined length. When returning meta-data about these types through functions like ResultSetMetaData.getColumnDisplaySize and ResultSetMetaData.getPrecision we must provide a value and various client tools have different ideas about what they would like to see. This parameter specifies the length to return for types of unknown length.

  • stringtype = String

    Specify the type to use when binding PreparedStatement parameters set via setString(). If stringtype is set to VARCHAR (the default), such parameters will be sent to the server as varchar parameters. If stringtype is set to unspecified, parameters will be sent to the server as untyped values, and the server will attempt to infer an appropriate type. This is useful if you have an existing application that uses setString() to set parameters that are actually some other type, such as integers, and you are unable to change the application to use an appropriate method such as setInt().

  • kerberosServerName = String

    The Kerberos service name to use when authenticating with GSSAPI. This is equivalent to libpq's PGKRBSRVNAME environment variable and defaults to "postgres".

  • jaasApplicationName = String

    Specifies the name of the JAAS system or application login configuration.

  • gsslib = String

    Force either SSPI (Windows transparent single-sign-on) or GSSAPI (Kerberos, via JSSE) to be used when the server requests Kerberos or SSPI authentication. Permissible values are auto (default, see below), sspi (force SSPI) or gssapi (force GSSAPI-JSSE).

    If this parameter is auto, SSPI is attempted if the server requests SSPI authentication, the JDBC client is running on Windows, and the Waffle libraries required for SSPI are on the CLASSPATH. Otherwise Kerberos/GSSAPI via JSSE is used. Note that this behaviour does not exactly match that of libpq, which uses Windows' SSPI libraries for Kerberos (GSSAPI) requests by default when on Windows.

    gssapi mode forces JSSE's GSSAPI to be used even if SSPI is available, matching the pre-9.4 behaviour.

    On non-Windows platforms or where SSPI is unavailable, forcing sspi mode will fail with a PSQLException.

    Since: 9.4

  • sspiServiceClass = String

    Specifies the name of the Windows SSPI service class that forms the service class part of the SPN. The default, POSTGRES, is almost always correct.

    See: SSPI authentication (Pg docs) Service Principal Names (MSDN), DsMakeSpn (MSDN) Configuring SSPI (Pg wiki).

    This parameter is ignored on non-Windows platforms.

  • useSpnego = boolean

    Use SPNEGO in SSPI authentication requests

  • ApplicationName = String

    Specifies the name of the application that is using the connection. This allows a database administrator to see what applications are connected to the server and what resources they are using through views like pgstatactivity.

  • sendBufferSize = int

    Sets SO_SNDBUF on the connection stream

  • receiveBufferSize = int

    Sets SO_RCVBUF on the connection stream

  • readOnly = boolean

    Put the connection in read-only mode

  • disableColumnSanitiser = boolean

    Enable optimization that disables column name sanitiser.

  • assumeMinServerVersion = String

    Assume that the server is at least the given version, thus enabling to some optimization at connection time instead of trying to be version blind.

  • currentSchema = String

    Specify the schema to be set in the search-path. This schema will be used to resolve unqualified object names used in statements over this connection.

  • targetServerType

    Allows opening connections to only servers with required state, the allowed values are any, master, slave and preferSlave. The master/slave distinction is currently done by observing if the server allows writes. The value preferSlave tries to connect to slaves if any are available, otherwise allows falls back to connecting also to master.

  • hostRecheckSeconds = int

    Controls how long in seconds the knowledge about a host state is cached in JVM wide global cache. The default value is 10 seconds.

  • loadBalanceHosts = boolean

    In default mode (disabled) hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates.

    Connection Fail-over

    To support simple connection fail-over it is possible to define multiple endpoints (host and port pairs) in the connection url separated by commas. The driver will try to once connect to each of them in order until the connection succeeds. If none succeed, a normal connection exception is thrown.

    The syntax for the connection url is:

    jdbc:postgresql://host1:port1,host2:port2/database

    The simple connection fail-over is useful when running against a high availability postgres installation that has identical data on each node. For example streaming replication postgres or postgres-xc cluster.

    For example an application can create two connection pools. One data source is for writes, another for reads. The write pool limits connections only to master node:

    jdbc:postgresql://node1,node2,node3/accounting?targetServerType=master . And read pool balances connections between slaves nodes, but allows connections also to master if no slaves are available:

    jdbc:postgresql://node1,node2,node3/accounting?targetServerType=preferSlave&loadBalanceHosts=true

分享到:
评论

相关推荐

    JDBC 连接到 PostgreSQL 数据库获取数据简单例子

    JDBC 连接到 PostgreSQL 数据库获取数据简单例子,最小的代码实现数据库读取

    JDBC连接数据库各种数据库参数大全

    JDBC连接数据库各种数据库参数大全 1、Oracle8/8i/9i数据库(thin模式) 2、DB2数据库 3、Sql Server7.0/2000数据库 4、Sybase数据库 5、Informix数据库 6、MySQL数据库 7、PostgreSQL...

    Post gre SQL 的JDBC的API说明文档

    从 http://jdbc.postgresql.org/ 官网下的jdbc api,然后制成了CHM,供大家学习使用。

    The PostgreSQL JDBC Interface.CHM(中文版)

    第一章 绪 论 第二章 安装JDBC驱动程序 2.1 获得驱动 ...3.4 连接参数 第四章 使用SSL 配置服务端 4.2 配置客户端 4.2.1 使用不带证书的SSL 4.3 定制 SSLSocketFactory 第五章 传递一个查询并处理结果

    OrderMS:进销存管理(因工作原因,搁置)

    进销存管理 运行环境 最低单核2G,推荐双核4G JDK(8,9,10),PostgreSQL9.6 JDK环境:,,, ...spring.datasource.url:JDBC连接参数其中127.0.0.1:5432修改为自己的数据库地址与端口 spring.datasource.username

    flink-cdc-connectors:更改Apache Flink的数据捕获(CDC)连接器

    Flink CDC连接器 Flink CDC连接器是Apache Flink的一组源连接器,使用更改数据捕获(CDC)从不同的数据库中提取更改。... PostgreSQL 数据库:9.6,10,11,12 JDBC驱动程序:42.2.12 产品特点 支持读取数据库快

    MySQL大量数据插入各种方法性能分析与比较

    不管是日常业务数据处理中,还是数据库的导入导出,都可能遇到需要处理大量数据的...  如果我们每插入一条都执行一个SQL语句,那么我们需要执行除了连接和关闭之外的所有步骤N次,这样是非常耗时的,优化的方式有一下

    JabberServer:用于查询与名为Jabber的伪社交媒体相关的PostgreSQL数据库的Java服务器

    该程序使用JDBC API与数据库建立连接。 程序的用户可以从main调用方法以执行具有不同参数的各种查询。 查询被发送到数据库,结果关系存储在单维或多维ArrayList中。 其他方法可用于直接处理表中的数据,例如添加和...

    Linux环境数据库管理员指南

    9.32 ODBC、JDBC和UDBC驱动程序 286 9.33 Perl 和 DBI 接口 287 9.34 PostgreSQL的教材 289 9.35 PostgreSQL URL 参考 290 9.36 小结 290 9.37 常见问答 291 第10章 开发基于Web的应用程序 295 10.1 引言 295 10.2 ...

    初级java笔试题-ff-flight-diary:个人飞行日记可存储您的飞行历史并比较全球数据

    用户可以根据各种参数查看他/她的个人统计数据。 服务的公共部分提供基于整个系统的统计信息。 动机 这个应用程序连接了我的两大爱好——航空和编码。 你可能会说那里有商业解决方案,你是对的。 那些家伙干了一件...

    MYSQL

    9.4 MySQL数据库表类型 10 从 MySQL 得到最大的性能 10.1 优化概述 10.2 系统/编译时和启动参数的调节 10.2.1 编译和链接如何影响 MySQL 的速度 10.2.2 磁盘问题 10.2.2.1 为数据库和...

    MySQL中文参考手册

    + 4.12.5 用 SSH 从 Win32 连接一个远程MySQL + 4.12.6 MySQL-Win32与Unix MySQL 比较 o 4.13 OS/2 注意事项 o 4.14 TcX 二进制代码 o 4.15 安装后期(post-installation)的设置与测试 + 4.15.1 运行mysql_...

    MySQL中文参考手册.chm

    9.4 MySQL数据库表类型 10 从 MySQL 得到最大的性能 10.1 优化概述 10.2 系统/编译时和启动参数的调节 10.2.1 编译和链接如何影响 MySQL 的速度 10.2.2 磁盘问题 10.2.2.1 为...

    Python Cookbook

    10.15 连接一个正在运行的Internet Explorer实例 392 10.16 读取Microsoft Outlook Contacts 393 10.17 在Mac OS X中收集详细的系统信息 396 第11章 用户界面 400 引言 400 11.1 在文本控制台中显示进度条 402...

Global site tag (gtag.js) - Google Analytics