The application connects to your YugabyteDB instance via
topology-aware JDBC driver and performs basic SQL
operations. The instructions below are provided for YugabyteDB Managed deployments.
If you use a different type of deployment, then update the /src/main/resources/app.properties
file with proper connection parameters.
- Java Development Kit, version 8 or later
- Maven 3.0 or later
- Command line tool or your favourite IDE, such as IntelliJ IDEA, or Eclipse.
- Start YugabyteDB Managed instance. Free tier exists.
- Add an IP address of your machine/laptop to the IP allow list
Clone the application to your machine:
git clone https://github.com/yugabyte/yugabyte-simple-java-app.git && cd yugabyte-simple-java-app
The application needs to establish a secured connection to your YugabyteDB Managed instance.
Open the app.properties
file and specify the following configuration parameters:
host
- the hostname of your cluster instance.port
- the port number that will be used by the JDBC driver (the default is5433
)dbUser
- the database username you used for your instance.dbPassword
- the database password.sslMode
- the SSL mode. Set toverify-full
for YugabyteDB Managed deployments.sslRootCert
- a full path to your CA root cert (for example,/Users/dmagda/certificates/root.crt
)
Note, you can easily find all the settings on the YugabyteDB Managed dashboard:
- Build the app with Maven:
mvn clean package
- Run the app:
java -cp target/yugabyte-simple-java-app-1.0-SNAPSHOT.jar SampleApp
Upon successful execution, you will see output similar to the following:
>>>> Successfully connected to YugabyteDB!
>>>> Successfully created DemoAccount table.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 10000
name = John, age = 28, country = Canada, balance = 9000
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 9200
name = John, age = 28, country = Canada, balance = 9800
Congrats! You successfully executed a simple Java app that works with YugabyteDB.
Now, explore the source code of the SampleApp.java
file:
main
method - establishes a connection with your cloud instance via JDBC driver.createDatabase
method - creates a table and populates it with sample data.selectAccounts
method - queries the data with SQLSELECT
statements.transferMoneyBetweenAccounts
method - updates records consistently with distributed transactions.
Having issues running this application or want to learn more from Yugabyte experts?
Join our Slack channel,
or raise a question on StackOverflow and tag the question with yugabytedb
!