TiDB Cloud is a fully-managed Database-as-a-Service (DBaaS) that brings everything great about TiDB to your cloud and lets you focus on your applications, not the complexities of your database.
In this 15-minute tutorial, you will learn how to use TiDB Cloud as the backend database for your Java application, and how easy it is to use TiDB Cloud with Hibernate ORM.
Before you begin
Get the sample Java application used in this tutorial. Download the zip file directly, or if you have the git tools installed you can clone the file:
git clone https://github.com/bb7133/tidb-hibernate-example
The sample application is a simple CRM tool where you can add, query, and update customer and order information. For more information, check the detailed code explanations here.
Create a TiDB dev tier cluster
- If you do not have a TiDB Cloud account, click here to sign up for an account.
- Log in to your TiDB Cloud account.
- Click Create a Cluster to go to the plan selection page, and then click Get Started for Free under the Developer Tier plan.
- On the Create a Cluster page, set up your cluster name and root password.
- For the cloud provider field, note that AWS is selected by default. Select the region where you want to create your cluster.
- Select the S1.dev cluster tier, and then click Submit.
Your TiDB Cloud cluster will be created in approximately 5 to 10 minutes.
Create a Database in TiDB Cloud
- On the TiDB Cloud console, click Connect on the upper right of the pane. The Connect to TiDB dialog displays.
- Click the Web SQL Shell tab and select >_Open SQL Shell. The SQL Shell window opens.
Screenshot of SQL Shell window
- Enter the cluster password you set in the previous step to login.
- Create the bank database using the following statements:
CREATE DATABASE bank;
CREATE USER '<username>' IDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON bank.* TO '<username>';
- Use the bank database and check the current tables:
USE bank;
SHOW tables;
Screenshot of
SHOW tables
There are currently no tables and rows in this database.
Get TiDB Cluster IP address
- Go to https://tidbcloud.com/console/clusters and sign in to your cluster.
- On the TiDB Cloud console, click Connect on the upper right of the pane.
The Connect to TiDB dialog displays. - Create the traffic filter for the cluster. Click the button Allow Access from Anywhere. Then click Create Filter.
- Note the TiDB Cloud IP address listed between the
-h
parameter and-P
parameter. You will use it at a later step. For example:mysql -u root -h tidb.xxx.xxxxx.us-west-2.prod.aws.tidbcloud.com -P 4000 -p
.
Use TiDB Cloud as the database for your Java application
- In your favorite IDE, open the tidb-hibernate-example demo app.
- Use IntelliJ for example, click Open, select <
your_download _path>/tidb-hibernate-example/build.gradle
file, and click Open. - Select Open as Project.
- Use IntelliJ for example, click Open, select <
- Expand
the /src/main/resources
folder, and click hibernate.properties. - Change the connection settings:
- Modify the hibernate.connection.url field and change it to:
jdbc:mysql://<your_tidb_cloud_ip_address>:4000/bank?enabledTLSProtocols=TLSv1.2
- Modify the hibernate.connection.username field and change it to the username you created in the Create a TiDB Dev Tier Cluster section, when you ran the
CREATE USER
command. - Modify the hibernate.connection.password field and add the password from the same
CREATE USER
command.
- Modify the hibernate.connection.url field and change it to:
- Deploy this application. Use IntelliJ for example: right-click the
src/main/java/com/pingcap/hibernate/Example.java
file and select run Example.main(). - Once the deployment is complete, go to TiDB Cloud SQL Shell and verify the change:
USE bank;
SHOW tables;
Screenshot of
SHOW tables
You have created two tables using your Java application.
Want to learn more?
Ready to give TiDB Cloud a try? TiDB Cloud Developer Tier is now available! It lets you run a TiDB cluster for free for one year on Amazon Web Services. Make sure to follow us on Twitter to stay updated on TiDB Cloud news!
TiDB Cloud Dedicated
TiDB Cloudのエンタープライズ版。
専用VPC上に構築された専有DBaaSでAWSとGoogle Cloudで利用可能。
TiDB Cloud Serverless
TiDB Cloudのライト版。
TiDBの機能をフルマネージド環境で使用でき無料かつお客様の裁量で利用開始。