postgresql jdbc连接参数

本文介绍了如何使用 JDBC 连接到 PostgreSQL 数据库,详细解析了连接字符串中的参数,如 `jdbc:postgresql://192.168.1.23:12308/test?useUnicode=true&characterEncoding=gbk&allowEncodingChanges=true`。内容涵盖了数据库的主机名、端口、数据库名以及连接参数,包括 SSL 连接、用户、密码、协议版本、字符集等设置。此外,还讨论了服务器端和客户端的编码处理以及连接超时、日志级别和服务器端预编译语句等高级选项。
摘要由CSDN通过智能技术生成

 

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 &#

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值