Quick Start for NuoDB JDBC

Overview

This guide describes the steps to create a Java application to connect to a NuoDB database using the JDBC driver.

Requirements

  • Ensure a running NuoDB database instance. For instructions on starting and running a NuoDB database, see NuoDB Quick Start Guide.

  • Download the NuoDB JDBC JAR file and add it to Java classpath, or declare it as a dependency in your build tool.

    For dependency declarations for Java build tools (for example, Maven or Gradle), see NuoDB JDBC Driver on Maven.

    Use the latest version of the JDBC driver.

Steps to Create a Java Application

  1. Using a preferred editor, open a new Java source file in the current directory.

  2. Import the packages required to access the classes used in the Java program.

    For example:

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    import com.nuodb.jdbc.DataSource;
  3. Establish a connection to the database.

    Use the dataSource.getConnection() method to establish a connection to the NuoDB database. This method requires the database URL, username, and password to authenticate with the database.

    For example:

    String url = String.format("jdbc:com.nuodb://%s/%s", host, db);
    
    Properties properties = new Properties();
    properties.put(DataSource.PROP_URL, url);
    properties.put(DataSource.PROP_USER, username);
    properties.put(DataSource.PROP_PASSWORD, password);
    DataSource dataSource = new DataSource(properties);
    Connection connection = dataSource.getConnection();

    The format of the database URL is:

    jdbc:com.nuodb://<host>:[<port>]/<db_name>

    For example:

    jdbc:com.nuodb://localhost/hockey
  4. Execute SQL queries and process results.

    Use JDBC Statement methods such as executeUpdate() or executeQuery() to execute SQL queries.

    For example:

    statement.executeUpdate("DROP TABLE IF EXISTS teams");

Example Java Application File

In the following example, the Java application (JDBCexample.java) connects to the demo database, creates a table called teams, and counts the number of rows from the table.

The following environment variables provide the database connection information:

  • NUODB_AP_HOST: Provide the database host

  • NUODB_DB_NAME: Provides the database name

  • NUODB_USERNAME: Provides the username

  • NUODB_PASSWORD: Provides the password

JDBCexample.java file
/*Import required packages*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.nuodb.jdbc.DataSource;

public class JDBCexample {
    public static void main(String[] args) throws SQLException {
        /* Change the following according to your database or set them using environment variables. */

        String host     = System.getenv().getOrDefault("NUODB_AP_HOST", "localhost");
        String db       = System.getenv().getOrDefault("NUODB_DB_NAME", "demo");
        String username = System.getenv().getOrDefault("NUODB_USERNAME", "dba");
        String password = System.getenv("NUODB_PASSWORD");

        String url = String.format("jdbc:com.nuodb://%s/%s", host, db);

        Properties properties = new Properties();
        properties.put(DataSource.PROP_URL, url);
        properties.put(DataSource.PROP_USER, username);
        properties.put(DataSource.PROP_PASSWORD, password);
        DataSource dataSource = new DataSource(properties);

        /*Connect to the database*/
        try (
             Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()
        ) {
            /*Execute SQL statements*/
            statement.executeUpdate("DROP TABLE IF EXISTS teams");
            statement.executeUpdate("CREATE TABLE teams (id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name STRING)");
            statement.executeUpdate("INSERT INTO teams (name) VALUES ('Mexico'), ('Brazil'), ('Germany'), ('Japan'), ('Egypt')");
            ResultSet result = statement.executeQuery("SELECT COUNT(*) AS total FROM teams");
            /*Print out the results*/
            if (result.next()) {
                System.out.println("Team count: " + result.getInt("total"));
            }
        }
    }
}

Output after successfully running the compiled Java program:

Team count: 5