This tutorial assumes that:

  • YugabyteDB is up and running. Download and install YugabyteDB by following the steps in Quick start.
  • Java Development Kit (JDK) 1.8, or later, is installed. JDK installers for Linux and macOS can be downloaded from OpenJDK, AdoptOpenJDK, or Azul Systems.
  • Apache Maven 3.3 or later, is installed.

Create and configure the Java project

  1. Create a project called "MySample".

    $ mvn archetype:generate \
        -DgroupId=com.yugabyte \
        -DartifactId=MySample \
        -DarchetypeArtifactId=maven-archetype-quickstart \
    $ cd MySample
  2. Open the pom.xml file in a text editor.

  3. Add the following below the <url> element.

  4. Add the following in the <dependencies> element.


    Your pom.xml file should now be similar to the following:

    <project xmlns="" xmlns:xsi=""
  5. Save and close pom.xml.

  6. Install the added dependency.

    $ mvn install

Create the sample Java application

  1. Copy the following Java code to a new file named src/main/java/com/yugabyte/

    package com.yugabyte;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class HelloSqlApp {
      public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5433/yugabyte",
                                                      "yugabyte", "yugabyte");
        Statement stmt = conn.createStatement();
        try {
            System.out.println("Connected to the PostgreSQL server successfully.");
            stmt.execute("DROP TABLE IF EXISTS employee");
            stmt.execute("CREATE TABLE IF NOT EXISTS employee" +
                        "  (id int primary key, name varchar, age int, language text)");
            System.out.println("Created table employee");
            String insertStr = "INSERT INTO employee VALUES (1, 'John', 35, 'Java')";
            System.out.println("EXEC: " + insertStr);
            ResultSet rs = stmt.executeQuery("select * from employee");
            while ( {
              System.out.println(String.format("Query returned: name = %s, age = %s, language = %s",
                                              rs.getString(2), rs.getString(3), rs.getString(4)));
        } catch (SQLException e) {
  2. Run your new program.

    $ mvn -q package exec:java -DskipTests -Dexec.mainClass=com.yugabyte.HelloSqlApp

    You should see the following as the output:

    Connected to the PostgreSQL server successfully.
    Created table employee
    Inserted data: INSERT INTO employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');
    Query returned: name=John, age=35, language: Java