Quickstart
The Java SDK for SurrealDB makes it straightforward to connect to your instance and start querying data. This guide walks you through connecting, authenticating, and performing basic operations.
1. Install the SDK
Follow the installation guide to install the SDK as a dependency in your project. Once installed, import the SDK to start using it.
2. Connect to SurrealDB
Use a try-with-resources block to create a connection, then call .useNs() and .useDb() to select a namespace and database, and .signin() to authenticate.
Supported connection protocols include:
WebSocket (
ws://,wss://) for long-lived stateful connectionsHTTP (
http://,https://) for short-lived stateless connectionsEmbedded (
memory,surrealkv://) for in-process databases
The Surreal class implements AutoCloseable, so the connection is automatically closed at the end of the try-with-resources block.
3. Inserting data
To represent database records in your application, define POJO classes that match your table structure. A public no-argument constructor is required. Use a RecordId field named id to hold the record identifier.
Use .create() to insert records into a table. When passing a table name, the method returns a list of created records with generated IDs.
To create a record with a specific ID, pass a RecordId instead. This returns the created record directly.
4. Retrieving data
Selecting records
The .select() method retrieves all records from a table, or a single record by its RecordId.
Running SurrealQL queries
For more advanced use cases, use the .queryBind() method to execute SurrealQL statements with bound parameters.
5. Closing the connection
If you use a try-with-resources block as shown above, the connection is closed automatically. Otherwise, call .close() manually to release resources.
What's next?
You have learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There is a lot more you can do with the SDK, including updating and deleting records, handling authentication, live queries, and transactions.
Connection management
Learn how to manage your database connections, including protocols and embedded mode.
Authentication
Read more about authentication and how to integrate it into your application.
Data manipulation
Learn how to create, read, update, and delete records using the SDK.
API Reference
Complete reference for all classes, methods, types, and errors.