Skip to content

Statements

Santiago Escobares edited this page Apr 5, 2025 · 4 revisions

Once we already set up a database instance and we already created some tables, we are ready to start creating Statements for different SQL operations. Structure is similar to all previous objects and methods are strictly related to SQL syntaxes. In this wiki, only a few examples will be provided, but there may be more methods to use for your statements.

Creating a user

Statement.create()
        .insertInto(Tables.USERS)
        .values(4) // Insert '?' four times.
        .replacements(
                1,
                "santiago",
                Instant.now(),
                "12345"
        )
        .executeUpdate();

Deleting a user

Statement.create()
        .deleteFrom(Tables.USERS)
        .where()
        .equals(UserColumns.ID)
        .replacements(1)
        .executeUpdate();

Updating a user

Statement.create()
        .update(Tables.USERS)
        .set(UserColumns.PASSWORD)
        .where()
        .equals(UserColumns.ID)
        .replacements("new-password", 1) // Password column, id column
        .executeUpdate();

Fetching a user asynchronously

Statement.create()
        .selectAllFrom(Tables.USERS)
        .where()
        .equals(UserColumns.ID)
        .replacements(1)
        .executeQueryAsync(result -> {
            String name = result.getString(UserColumns.ID);
            Instant creationDate = result.getTimestamp(UserColumns.CREATION_DATE).toInstant();
            String password = result.getString(UserColumns.PASSWORD);

            // Do something with data...
        })
        .thenAccept(i -> {
            System.out.println("User loaded!");
        })
Clone this wiki locally