Creating a connection is an expensive operation. In a web application, you should never create a connection per request. You must use a connection pool.
The (pgJDBC) is a Type 4 open-source driver written in pure Java. It allows Java applications to communicate with PostgreSQL databases using standard, database-independent Java code.
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT version()"); if (rs.next()) System.out.println(rs.getString(1));
String url = "jdbc:postgresql://localhost:5432/mydb?ssl=true&sslmode=require";
The core of JDBC is the Connection object. You obtain this via the DriverManager .
String url = "jdbc:postgresql://host1:5432,host2:5432/mydb?targetServerType=primary";
Note the use of try-with-resources blocks. This automatically closes the connection and statement when the block ends, preventing memory leaks.
Add the following to your pom.xml dependencies block. Check for the latest version on Maven Central .