Build a Java application

    Working example

    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 , AdoptOpenJDK, or .
    • Apache Maven 3.3, or later, is installed.
    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. <source>1.8</source>
    25. <target>1.8</target>
    26. </configuration>
    27. </plugin>
    28. <plugin>
    29. <groupId>org.apache.maven.plugins</groupId>
    30. <artifactId>maven-dependency-plugin</artifactId>
    31. <executions>
    32. <execution>
    33. <id>copy-dependencies</id>
    34. <phase>prepare-package</phase>
    35. <goals>
    36. <goal>copy-dependencies</goal>
    37. </goals>
    38. <configuration>
    39. <outputDirectory>${project.build.directory}/lib</outputDirectory>
    40. <overWriteReleases>true</overWriteReleases>
    41. <overWriteSnapshots>true</overWriteSnapshots>
    42. <overWriteIfNewer>true</overWriteIfNewer>
    43. </configuration>
    44. </execution>
    45. </executions>
    46. </plugin>
    47. </plugins>
    48. </build>
    49. </project>

    Create the appropriate directory structure as expected by Maven.

    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. Connection connection = null;
    11. connection = DriverManager.getConnection(
    12. // Create table 'employee'
    13. String createStmt = "CREATE TABLE employee (id int PRIMARY KEY, " +
    14. "name varchar, " +
    15. "age int, " +
    16. "language varchar);";
    17. connection.createStatement().execute(createStmt);
    18. System.out.println("Created table employee");
    19. // Insert a row.
    20. String insertStmt = "INSERT INTO employee (id, name, age, language)" +
    21. " VALUES (1, 'John', 35, 'Java');";
    22. connection.createStatement().executeUpdate(insertStmt);
    23. System.out.println("Inserted data: " + insertStmt);
    24. // Query the row and print out the result.
    25. String selectStmt = "SELECT name, age, language FROM employee WHERE id = 1;";
    26. PreparedStatement pstmt = connection.prepareStatement(selectStmt);
    27. ResultSet rs = pstmt.executeQuery();
    28. while (rs.next()) {
    29. String name = rs.getString(1);
    30. int age = rs.getInt(2);
    31. String language = rs.getString(3);
    32. System.out.println("Query returned: " +
    33. "name=" + name + ", age=" + age + ", language: " + language);
    34. }
    35. // Close the client.
    36. connection.close();
    37. } catch (Exception e) {
    38. System.err.println("Error: " + e.getMessage());
    39. }
    40. }
    41. }

    To build the application, run the following command.

    You should see the following as the output.