Build a Java application

Maven

To build your Java application using the PostgreSQL JDBC driver, add the following Maven dependency to your application:

  1. <dependency>
  2. <groupId>org.postgresql</groupId>
  3. <artifactId>postgresql</artifactId>
  4. <version>42.2.5</version>
  5. </dependency>

Working example

Prerequisites

This tutorial assumes that you have:

  • YugabyteDB up and running. If you are new to YugabyteDB, you can download, install, and have YugabyteDB up and running within five minutes by following the steps in the Quick Start guide.
  • 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 the Maven build file

Create a maven build file pom.xml and add the following content into it.

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.yugabyte.sample.apps</groupId>
  8. <artifactId>hello-world</artifactId>
  9. <version>1.0</version>
  10. <packaging>jar</packaging>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.postgresql</groupId>
  14. <artifactId>postgresql</artifactId>
  15. <version>42.2.5</version>
  16. </dependency>
  17. </dependencies>
  18. <build>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-compiler-plugin</artifactId>
  23. <version>3.7.0</version>
  24. <configuration>
  25. <source>1.8</source>
  26. <target>1.8</target>
  27. </configuration>
  28. </plugin>
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-dependency-plugin</artifactId>
  32. <version>2.1</version>
  33. <executions>
  34. <execution>
  35. <id>copy-dependencies</id>
  36. <phase>prepare-package</phase>
  37. <goals>
  38. <goal>copy-dependencies</goal>
  39. </goals>
  40. <configuration>
  41. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  42. <overWriteReleases>true</overWriteReleases>
  43. <overWriteSnapshots>true</overWriteSnapshots>
  44. <overWriteIfNewer>true</overWriteIfNewer>
  45. </configuration>
  46. </execution>
  47. </executions>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

Write an application

Create the appropriate directory structure as expected by Maven.

  1. $ mkdir -p src/main/java/com/yugabyte/sample/apps

Copy the following contents into the file src/main/java/com/yugabyte/sample/apps/YBSqlHelloWorld.java.

  1. package com.yugabyte.sample.apps;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. public class YBSqlHelloWorld {
  7. public static void main(String[] args) {
  8. try {
  9. // Create the DB connection
  10. Class.forName("org.postgresql.Driver");
  11. Connection connection = null;
  12. connection = DriverManager.getConnection(
  13. "jdbc:postgresql://127.0.0.1:5433/yugabyte","yugabyte", "yugabyte");
  14. // Create table 'employee'
  15. String createStmt = "CREATE TABLE employee (id int PRIMARY KEY, " +
  16. "name varchar, " +
  17. "age int, " +
  18. "language varchar);";
  19. connection.createStatement().execute(createStmt);
  20. System.out.println("Created table employee");
  21. // Insert a row.
  22. String insertStmt = "INSERT INTO employee (id, name, age, language)" +
  23. " VALUES (1, 'John', 35, 'Java');";
  24. connection.createStatement().executeUpdate(insertStmt);
  25. System.out.println("Inserted data: " + insertStmt);
  26. // Query the row and print out the result.
  27. String selectStmt = "SELECT name, age, language FROM employee WHERE id = 1;";
  28. PreparedStatement pstmt = connection.prepareStatement(selectStmt);
  29. ResultSet rs = pstmt.executeQuery();
  30. while (rs.next()) {
  31. String name = rs.getString(1);
  32. int age = rs.getInt(2);
  33. String language = rs.getString(3);
  34. System.out.println("Query returned: " +
  35. "name=" + name + ", age=" + age + ", language: " + language);
  36. }
  37. // Close the client.
  38. connection.close();
  39. } catch (Exception e) {
  40. System.err.println("Error: " + e.getMessage());
  41. }
  42. }
  43. }

Build and run the application

To build the application, run the following command.

  1. $ mvn package

To run the program, run the following command.

  1. $ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBSqlHelloWorld

You should see the following as the output.

  1. Created table employee
  2. Inserted data: INSERT INTO employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');
  3. Query returned: name=John, age=35, language: Java