postgresqlHow do I use the PostgreSQL JDBC driver with Maven?
Using the PostgreSQL JDBC driver with Maven is very simple. First, you need to add the driver to your pom.xml
file as a Maven dependency:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
Then, you can use the driver in your Java code. For example, the following code connects to a PostgreSQL database and prints out the version of the database:
// Load the JDBC driver
Class.forName("org.postgresql.Driver");
// Establish a connection
Connection conn = DriverManager.getConnection("jdbc:postgresql://hostname:port/dbname", "username", "password");
// Print out the version of the database
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT version()")) {
if (rs.next()) {
System.out.println(rs.getString(1));
}
}
The output of the above code is the version of the PostgreSQL database, for example:
PostgreSQL 11.2
For more information on using the PostgreSQL JDBC driver with Maven, see the official documentation.
More of Postgresql
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can Zalando use PostgreSQL to improve its software development?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I integrate PostgreSQL with Yii2?
- How can I retrieve data from PostgreSQL for yesterday's date?
- How can I use PostgreSQL XML functions to manipulate XML data?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I set a PostgreSQL interval to zero?
- How can I use PostgreSQL's "zero if null" feature?
See more codes...