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 thehost
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 thestringtype
parameter is changed tounspecified
. -
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) andorg.postgresql.Driver.INFO
(1).INFO
will log very little information whileDEBUG
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 theCOPY
command on a file residing on the server's filesystem. The only means of specifying the encoding of this file is by altering theclient_encoding
setting. The JDBC team considers this a failing of theCOPY
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 itsclose()
method. Eventually these objects will be garbage collected and thefinalize()
method will be called which will close theConnection
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 thelogUnclosedConnections
URL parameter has been added. It captures a stacktrace at eachConnection
opening and if thefinalize()
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 samePreparedStatement
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 likeResultSetMetaData.getColumnDisplaySize
andResultSetMetaData.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 viasetString()
. Ifstringtype
is set toVARCHAR
(the default), such parameters will be sent to the server as varchar parameters. Ifstringtype
is set tounspecified
, 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 usessetString()
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 assetInt()
. -
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数据库并执行简单的数据获取操作。PostgreSQL是一种开源的对象关系型数据库管理系统,因其强大的功能和稳定性而广受欢迎。 首先,确保已安装PostgreSQL数据库并在系统...
总结来说,"Jdbc连接postgresql数据库(源码+jar包)"资源提供了使用Java JDBC连接到PostgreSQL数据库的实例,涵盖了从加载驱动到执行SQL的基本流程,对于学习和实践非常有帮助。通过学习和理解这些代码,开发者能够...
### JDBC连接数据库各种数据库参数详解 #### 一、Oracle8/8i/9i数据库(thin模式) 在Java中通过JDBC访问Oracle数据库时,通常采用thin驱动方式。thin驱动是一种纯Java驱动,无需依赖任何非Java代码即可完成与...
在Java编程中,PostgreSQL JDBC驱动是连接Java应用程序与PostgreSQL数据库的关键组件。这个源代码包包含了版本0.9.2的PostgreSQL JDBC驱动,它允许开发者通过JDBC API与PostgreSQL数据库进行交互。下面我们将详细...
在本示例中,我们将详细讲解如何使用JDBC连接到PostgreSQL数据库,这是一个非常基础且实用的技能,对于任何需要在Java应用程序中处理数据库的开发者来说都是必不可少的。 首先,你需要确保已经在你的系统上安装了...
在本文中,我们将探讨不同数据库的JDBC连接方式,包括Microsoft SQL Server、MySQL、Oracle、Sybase和PostgreSQL。 1. **Microsoft SQL Server & Sybase**: - 使用jTDS驱动进行连接。jTDS是一款开源的、实现了...
"连接postgresql数据库需要的jar包"指的是用于建立Java应用程序与PostgreSQL数据库之间通信的Java数据库连接(JDBC)驱动。在Java中,JDBC驱动是实现Java标准API(java.sql)的类库,它允许Java应用程序执行SQL语句...
在本项目中,我们主要探讨如何使用Java的...总的来说,通过JDBC连接OpenGauss PostgreSQL数据库,并结合Java编程,我们可以构建一个功能完善的图书管理系统,实现对图书信息的增删改查,满足日常管理和用户查询的需求。
这份文档可能详细列举了上述不同数据库的JDBC连接示例代码,包括具体的URL格式、驱动类名以及连接参数设置,供开发者参考。 4. **数据库连接方法.mht** MHT文件是一种单个文件存储网页的格式,可能包含了如何使用...
从 http://jdbc.postgresql.org/ 官网下的jdbc api,然后制成了CHM,供大家学习使用。
标题与描述均提及了“JDBC连接各种数据库的方法”,这明确指出文章将聚焦于Java数据库连接(JDBC)技术在不同数据库系统中的应用。JDBC是Java开发工具包的一部分,它提供了一组标准的API,使得Java应用程序能够访问...
通过上述示例可以看出,使用JDBC连接不同类型的数据库时,主要的区别在于驱动类的加载、URL的格式以及可能的参数设置上。开发者需要根据所使用的数据库类型,选择合适的驱动类,并正确配置连接字符串和登录凭据。...
在IT行业中,Java Database Connectivity(JDBC)是一个重要的标准接口,它允许Java应用程序与各种数据库进行交互...理解和熟练掌握JDBC连接参数及使用方法,对于任何Java数据库开发人员来说都是基础且至关重要的技能。
在本篇文章中,我们将深入探讨不同类型的数据库以及如何使用JDBC连接它们。 1. **DB2**: - 驱动类:`Com.ibm.db2.jdbc.net.DB2Driver` - URL格式:`jdbc:db2://dburl:port/DBname` - 示例连接代码: ```java ...
### JDBC连接驱动大全 #### 一、MySQL连接配置 **MySQL** 是一款广泛使用的开源关系型数据库管理系统。要通过 Java 应用程序连接 MySQL 数据库,可以使用 **MySQL JDBC 驱动**。 - **驱动文件**: `mm.mysql-2.0.2...
本文详细介绍了JDBC中常用的数据库连接字符串及其配置方法,包括MySQL、PostgreSQL、Oracle、Sybase、Microsoft SQL Server、ODBC以及DB2等数据库的连接方式。了解这些连接字符串的格式和参数设置对于进行数据库操作...
每种数据库的连接参数都有所不同,但基本流程相似:加载驱动、设置连接URL、提供必要的认证信息。掌握这些连接技巧,可以大大提高跨数据库应用的开发效率。在实际应用中,开发者还需要注意处理异常、关闭资源等细节...
标题中提到的"JDBC连接驱动(全)"很可能是一个包含多种数据库JDBC驱动的压缩包,如MySQL、Oracle、SQL Server、PostgreSQL等。 以下是几种常见的JDBC驱动类型及其用法: 1. **JDBC-ODBC桥接驱动**:这是最早的...
1. **配置连接**: 开发者需要在Java代码中提供数据库连接参数,如URL、用户名和密码,以建立与PostgreSQL服务器的连接。 2. **创建DataSource**: 使用`java.sql.DriverManager`或`javax.sql.DataSource`接口来管理...