Updated on 2025-08-25 GMT+08:00

Example: Common Operations

The following demonstrates the main steps of JDBC-based development.

They involve tasks such as creating a database, creating a table, and inserting data.

Sample Code

This sample will demonstrate how to develop an application based on the JDBC interface provided by DataArts Fabric SQL.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//DBtest.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class DBTest {

  // Create a database connection.
  public static Connection GetConnection() {
    String driver = "org.postgresql.Driver";
    String sourceURL = "jdbc:fabricsql://localhost:443/fabricsql_default";
    Connection conn = null;
    try {
      // Load the database driver.
      Class.forName(driver).newInstance();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

    try {
      Properties properties = new Properties();
      properties.setProperty("workspaceId", "");
      properties.setProperty("endpointId", "");
      properties.setProperty("lakeformation_instance_id", "");
      properties.setProperty("AccessKeyID", "");
      properties.setProperty("SecretAccessKey", "");
      // Create a database connection.
      conn = DriverManager.getConnection(sourceURL, properties);
      System.out.println("Connection succeed!");
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

    return conn;
  };

  // Execute a regular SQL statement to create the newproducts table.
  public static void CreateTable(Connection conn) {
    Statement stmt = null;
    try {
      stmt = conn.createStatement();

      // Execute a regular SQL statement.
      int rc = stmt
          .executeUpdate("CREATE TABLE newproducts(product_id INTEGER,product_name VARCHAR2(60),category VARCHAR2(60),quantity  INTEGER)STORE AS orc;");

      stmt.close();
    } catch (SQLException e) {
      if (stmt != null) {
        try {
          stmt.close();
        } catch (SQLException e1) {
          e1.printStackTrace();
        }
      }
      e.printStackTrace();
    }
  }

  // Execute a prepared statement for batch insertion of data.
  public static void BatchInsertData(Connection conn) {
    PreparedStatement pst = null;

    try {
      // Generate a prepared statement.
      pst = conn.prepareStatement("INSERT INTO newproducts VALUES (?, ?, ?, ?);");
      for (int i = 0; i < 3; i++) {
        // Add parameters.
        pst.setInt(1, i + 1);
        pst.setString(2, "name" + i);
        pst.setString(3, "cata" + i);
        pst.setInt(4, i + 1);
        pst.addBatch();
      }
      // Perform batch processing.
      pst.executeBatch();
      pst.close();
    } catch (SQLException e) {
      if (pst != null) {
        try {
          pst.close();
        } catch (SQLException e1) {
        e1.printStackTrace();
        }
      }
      e.printStackTrace();
    }
  }
  

  /**
   * Main program, calling each static method step by step.
   * @param args
  */
  public static void main(String[] args) {
    // Create a database connection.
    Connection conn = GetConnection();

    // Create a table.
    CreateTable(conn);

    // Insert data in batches.
    BatchInsertData(conn);

    // Execute a precompiled statement to update data.
    ExecPreparedSQL(conn);

    // Close the database connection.
    try {
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }

}