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 can I set a PostgreSQL interval to zero?
- How can I use PostgreSQL XOR to compare two values?
- How can Zalando use PostgreSQL to improve its software development?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I show tables in PostgreSQL?
- How do I rename a column in PostgreSQL?
- How do I store an array in a PostgreSQL database?
See more codes...