Executing queries
The Java SDK provides methods for executing raw SurrealQL queries against SurrealDB. You can run single or multi-statement queries, bind parameters for safe variable injection, and call server-side functions.
API References
| Method | Description |
|---|---|
db.query(sql) | Executes a SurrealQL query |
db.queryBind(sql, params) | Executes a parameterized query |
db.run(name, args) | Runs a SurrealDB function |
Running a query
The .query() method executes one or more SurrealQL statements and returns a Response. Use .take(int) to extract the result of a specific statement by its zero-based index, or .take(Class, int) to deserialize the result into a typed Java object.
Using query parameters
The .queryBind() method accepts a Map<String, ?> of parameters that are safely injected into the query. Parameterized queries prevent SurrealQL injection and ensure values are properly escaped.
Always prefer .queryBind() over string concatenation when incorporating user-provided values into queries.
Working with query responses
The Response object contains the results of all statements in the query. Use .take(int) to get a raw Value, or .take(Class, int) to deserialize into a POJO. The .size() method returns the number of statement results.
See Value types for details on working with the Value class and type conversions.
Running SurrealDB functions
The .run() method calls a server-side function defined with DEFINE FUNCTION. Pass the function name and any arguments.
Learn more
Surreal API reference for method signatures
Response API reference for response handling
Value types for working with query results
SurrealQL for query language reference
SurrealQL parameters for parameter syntax in queries
DEFINE FUNCTION for defining server-side functions