O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

Here is the code fragment that creates connection to our local PostgreSQL database:

String URL = "jdbc:postgresql://localhost/cookbook";Properties prop = new Properties( );//prop.put( "user", "cook" );//prop.put( "password", "secretPass123" );Connection conn = DriverManager.getConnection(URL, prop);

The commented lines show how you can set a user and password for your connection. Since, for this demo, we keep the database open and accessible to anyone, we could use an overloaded DriverManager.getConnection(String url) method. However, we will show the most general implementation that would allow anyone to read from a property file and pass other useful values (ssl as true/false, autoReconnect as true/false, connectTimeout ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required