Response
The Response class wraps the results returned by a SurrealQL query execution. A single query string can contain multiple statements, and the Response holds the result of each statement indexed by its zero-based position.
Source: surrealdb.java
Methods
.take(index)
Extracts the result of a specific statement from the response by its zero-based index. The untyped variant returns a raw Value, while the typed variant deserializes the result into the specified Java class.
Method Syntax
Value take(int num)
<T> List<T> take(Class<T> type, int num)
| Parameter | Type | Description |
|---|
type optional | Class<T> | The class to deserialize the result into. Omit for an untyped Value return. |
num required | int | The zero-based index of the statement result to extract. |
Returns: Value (untyped) or List<T> (typed)
Example
Response response = db.query("SELECT * FROM users; SELECT * FROM posts;");
Value users = response.take(0);
List<Post> posts = response.take(Post.class, 1);
.size()
Returns the number of statement results contained in the response.
Method Syntax
response.size()
Returns: int
Example
Response response = db.query("SELECT * FROM users; SELECT * FROM posts;");
int count = response.size();
Complete Example
Working with multi-statement responses
import com.surrealdb.Surreal;
import com.surrealdb.Response;
import com.surrealdb.signin.RootCredential;
try (Surreal db = new Surreal()) {
db.connect("ws://localhost:8000");
db.useNs("surrealdb").useDb("docs");
db.signin(new RootCredential("root", "root"));
Response response = db.query(
"CREATE person SET name = 'Alice'; SELECT * FROM person;"
);
int statementCount = response.size();
Value created = response.take(0);
List<Person> people = response.take(Person.class, 1);
}
See Also