Book a Demo Start Instantly
Java

Author: Phoebe He (Content Developer at PingCAP)

Editors: Calvin Weng, Fendy Feng

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

  1. If you do not have a TiDB Cloud account, click here to sign up for an account.
  2. Log in to your TiDB Cloud account.
  3. Click Create a Cluster to go to the plan selection page, and then click Get Started for Free under the Developer Tier plan.
  4. On the Create a Cluster page, set up your cluster name and root password.
  5. For the cloud provider field, note that AWS is selected by default. Select the region where you want to create your cluster.
  6. 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

  1. On the TiDB Cloud console, click Connect on the upper right of the pane.  The Connect to TiDB dialog displays.
  2. Click the Web SQL Shell tab and select >_Open SQL Shell. The SQL Shell window opens.

    Screenshot of SQL Shell window

  3. Enter the cluster password you set in the previous step to login.
  4. Create the bank database using the following statements:
    CREATE DATABASE bank;
    CREATE USER '<username>' IDENTIFIED BY '<password>';
    GRANT ALL PRIVILEGES ON bank.* TO '<username>';
  5. 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

  1. Go to https://tidbcloud.com/console/clusters and sign in to your cluster.
  2. On the TiDB Cloud console, click Connect on the upper right of the pane.
    The Connect to TiDB dialog displays.
  3. Create the traffic filter for the cluster.  Click the button Allow Access from Anywhere.  Then click Create Filter.
  4. 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

  1. In your favorite IDE, open the tidb-hibernate-example demo app.
    1. Use IntelliJ for example, click Open,  select <your_download _path>/tidb-hibernate-example/build.gradle file, and click Open.
    2. Select Open as Project.
  2. Expand the /src/main/resources folder, and click hibernate.properties.
  3. Change the connection settings:
    1. Modify the hibernate.connection.url field and change it to:
      jdbc:mysql://<your_tidb_cloud_ip_address>:4000/bank?enabledTLSProtocols=TLSv1.2
    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.
    3. Modify the hibernate.connection.password field and add the password from the same CREATE USER command.
  4. Deploy this application. Use IntelliJ for example: right-click the src/main/java/com/pingcap/hibernate/Example.java file and select run Example.main().
  5. 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!


Book a Demo


Have questions? Let us know how we can help.

Contact Us
TiDB Dedicated

TiDB Dedicated

A fully-managed cloud DBaaS for predictable workloads

TiDB Dedicated

TiDB Serverless

A fully-managed cloud DBaaS for auto-scaling workloads