## Module 2: TiDB Basic Operations using Java

Run the following Java code to connect to TiDB Cloud Starter cluster and do some basic operations.

### Step 1. Connect to TiDB Cloud Starter cluster

+ On the left, in the **EXPLORER** section of the VS Code, click `DemoJdbcConnectionServerless.java` file.

+ The following Java code snippet in the `DemoJdbcConnectionServerless.java` connects you to the lab TiDB Cloud Starter cluster.

  ```java
  ...
  String tidbHost = System.getenv().getOrDefault("TIDB_HOST", "localhost");
  int tidbPort = Integer.parseInt(System.getenv().getOrDefault("TIDB_PORT", "4000"));
  String tidbUser = System.getenv().getOrDefault("TIDB_USER", "root");
  String tidbPassword = System.getenv().getOrDefault("TIDB_PASSWORD", "");
  String tidbDatabase = System.getenv().getOrDefault("TIDB_DATABASE", "test");

  String connectionUrl = "jdbc:mysql://" + tidbHost + ":" + tidbPort + "/" + tidbDatabase +
          "?sslMode=VERIFY_IDENTITY&enabledTLSProtocols=TLSv1.2,TLSv1.3";

  connection = DriverManager.getConnection(connectionUrl, tidbUser, tidbPassword);
  ...
  ```

+ You can now execute the code, and see the results.

  ```bash
  rm -f DemoJdbcConnectionServerless.class
  javac -cp mysql-connector-j-9.1.0.jar DemoJdbcConnectionServerless.java
  sleep 1
  java -cp :mysql-connector-j-9.1.0.jar DemoJdbcConnectionServerless
  ```

  ```output
  Connection established.
  Connection closed.
  ```

### Step 2. Basic CRUD operations

+ On the left, in the **EXPLORER** section of the VS Code, click `DemoJdbcExecute.java` file, you can briefly go through the source code.

+ In the following code snippet, we show you one way of creating a table, and then inserting and querying the data.

  ```java
  ...
  // Create table
  connection.createStatement().executeUpdate("CREATE TABLE t1 (id int PRIMARY KEY, name char(4))");

  // Describe table
  printResultSetStringString("DESCRIBE test.t1", connection);

  // Explain SQL
  printResultSetStringString("EXPLAIN SELECT * FROM test.t1", connection);

  // Select
  printResultSetStringString("SELECT * FROM test.t1", connection);

  // Try DML
  printResultSetStringString("INSERT INTO test.t1 VALUES (100, 'WXYZ'); INSERT INTO test.t1 VALUES (200, 'ABCD')", connection);

  // Select again
  printResultSetStringString("SELECT * FROM test.t1", connection);
  ...
  ```

  + Note that, for simplicity, we `SET tidb_multi_statement_mode='ON'` in the code, and executes two SQL statements in the one line. Please understand the security risks (such as SQL injections).

+ You can now execute the code, and see the results.

  ```bash
  rm -f DemoJdbcExecute.class
  javac -cp mysql-connector-j-9.1.0.jar DemoJdbcExecute.java
  sleep 1
  java -cp :mysql-connector-j-9.1.0.jar DemoJdbcExecute
  ```

  ```output
  Connection established.

  /* Executing: SET tidb_multi_statement_mode='ON'; */

  /* Executing: show variables like 'autocommit'; */
          Row#,  Variable_name, Value
          1) autocommit, ON

  /* Executing: describe test.t1; */
          Row#,  Field, Type
          1) id, int(11)
          2) name, char(4)

  /* Executing: explain select * from test.t1; */
          Row#,  id, estRows
          1) TableReader_5, 10000.00
          2) └─TableFullScan_4, 10000.00

  /* Executing: select * from test.t1; */
          Row#,  id, name

  /* Executing: insert into test.t1 values (100, 'WXYZ'); insert into test.t1 values (200, 'ABCD'); */

  /* Executing: select * from test.t1; */
          Row#,  id, name
          1) 100, WXYZ
          2) 200, ABCD
  Connection closed.
  ```
