To connect to the database using the DriverManager class, first load the JDBC driver, then specify the connection string as a URI in the API of the DriverManager class.
Specify org.postgresql.Driver.
URI connection is performed as follows:
jdbc:postgresql://host:port/database?user=user&password=password1&loginTimeout=loginTimeout&socketTimeout=socketTimeout
Argument | Description |
---|---|
host | Specify the host name for the connection destination. |
port | Specify the port number for the database server. The default is "26500". |
database | Specify the database name. |
user | Specify the username that will connect with the database. If this is omitted, the username logged into the operating system that is executing the application will be used. |
password | Specify a password when authentication is required. |
loginTimeout | Specify the timeout for connections (in units of seconds). Specify a value between 0 and 9223372036854775. There is no limit set if you set 0 or an invalid value. An error occurs when a connection cannot be established within the specified time. |
socketTimeout | Specify the timeout for communication with the server (in units of seconds). Specify a value between 0 and 2147483647. There is no limit set if you set 0 or an invalid value. An error occurs when data is not received from the server within the specified time. |
maxStatements | Specify the number of statements to be cached. Specify an integer value between 0 and 2147483647. The default is 0. The statement caching feature will be disabled if 0 is specified. An error will not occur if an invalid value is specified, however, the behavior will be as if 0 has been specified. Refer to "2.4.2 Statement Caching Feature" for information on this feature. |
Example
Code examples for applications
import java.sql.*; ... Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://sv1:26500/mydb?user=myuser&password=myuser01&loginTimeout=20&socketTimeout=20&maxStatements=20"; Connection con = DriverManager.getConnection(url);