Connect Java to Postgres on ObjectRocket

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re a Java developer planning to work with a Postgres database in your applications, the first thing you’ll need to do is create the necessary connections. Fortunately, the task of connecting Java to Postgres is an easy one. In this article, we’ll show you how to connect Java to Postgres on ObjectRocket with detailed instructions and code examples to get you started.

Prerequisites

A few important prerequisites need to be in place in order to follow along with the instructions in this tutorial. You’ll need to install and configure the following items:

  • PostgreSQL 11

  • ECLIPSE

  • JAVA JDK 1.8 must be installed on your system.

  • You’ll need to create an instance of Postgres for your ObjectRocket account using the Mission Control panel.

This tutorial also assumes you have some knowledge of PostgreSQL.

Download JDBC Driver for Postgres

In order to make the connection between our Postgres database on ObjectRocket and our Java applications, we’ll need to download a JDBC driver that bridges these two technologies.

The driver can be downloaded here PostgreSQL.org.

Creating a User for ObjectRocket

Our next task will be to create a user for the Postgres instance on ObjectRocket Mission Control. This will enable us to connect to our database.

Creating Username and Password in PostgreSQL Instance on ObjectRocket

Once your PostgreSQL instance is created on ObjectRocket, click VIEW MORE DETAILS as shown below:

Locate the “USERS” tab, then click the icon as shown in the window to create a new user. You’ll be asked to provide a username and password. Be sure to click the Admin button to grant the user PostgreSQL superuser access rights.

Connect to Postgres Instance on ObjectRocket

Now that we created our user and set a password, let’s discuss the details we’ll need for our Java and PostgreSQL connection.

Connecting to the Postgres Server Instance

To connect our Java application to the Postgres instance on ObjectRocket, we’ll use JDBC. In JDBC, our database will be represented using a URL. The basic form of the URL is shown below:

1
jdbc:PostgreSQL://host:port/database

Let’s take a closer look at the URL syntax:

  • host: This represents the host name of the server. The default value is localhost.
  • port: This represents the port where the server is listening for any client connection requests.
  • database: This represents the name of the database to which we want to connect.

The URL can be found in the “CONNECT” tab of your ObjectRocket Mission Control interface:

For the purposes of this tutorial, we’ll be using the following host URL: ingress.w98sujpz.launchpad.objectrocket.cloud

Creating JAVA Application

At this point, we’ve completed all the necessary setup, and we’re ready to dive into our code. We’ll start our Eclipse IDE, then we’ll create a new Java project named “javaPostgreSQL”.

Next, we need to import the JAR file that we downloaded.

Importing the JAR file allows us to reference that library within our application.

Now we can create a Java class named JavaPostgres. This class will be responsible for connecting to our database on ObjectRocket.

We’ll add the following Java code to this class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package javaPostgreSQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JavaPostgres {

   
    private final String dburl = "jdbc:PostgreSQL://ingress.w98sujpz.launchpad.objectrocket.cloud:4149/PostgreSQL";
    private final String dbuser = "pguser";
    private final String dbpassword = "1234";
 
    /**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */

    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
            System.out.println("You have successfully connected your Java application on ObjectRocket Postgres database");
 
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
 
        return conn;
    }
   
    public static void main(String[] args) {
        JavaPostgres app = new JavaPostgres();
        app.connect();
        }

}

If we run the application, we should see something like this:

Conclusion

If you need your Java applications to interact with a PostgreSQL database on ObjectRocket, it’s important to set up the correct connection between the two technologies. In this article, we explained how to connect Java to Postgres on ObjectRocket, providing step-by-step instructions for each step of the process. With this tutorial to guide you, you’ll have no trouble setting up a connection between Java and Postgres for your own application development.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.