Skip to main content

開發 - JDBC Driver

info

為了企業數據安全,我們在 CannerFlow 虛擬數據中台只能讓使用者 read-only 也就是目前僅支援 SELECT statement,不支援 Update, Delete 等操作。

Installation

你可以從下方連結下載 Jar

連線設定

CannerFlow JDBC 的 URL 格式如下

jdbc:cannerflow://[hosts]/cannerflow/[workspace sql name][?properties]
  • hosts: hosts 包含 port,舉例來說,若使用 IP 連線,hosts 會像是 192.168.100.1:80,若使用網域連線並搭配 SSL,hosts 會是 data-portal.apps.cannerflow.com:443
  • workspace sql name: workspace sql name 會是 workspace 的 SQL Name,這個參數可以在 Workspace > ConfigSettings 區塊的 Name in SQL 看到
  • ?properties: 連線的額外參數

實際連線的 URL 會像是

  • jdbc:cannerflow://192.168.100.1:80/cannerflow/test_workspace?token=Y2xpZW50XzM0MTE4NjJlLTI1Y
  • jdbc:cannerflow://data-portal.apps.cannerflow.com:443/cannerflow/test_workspace?token=Y2xpZW50XzM0MTE4NjJlLTI1Y

Example code

try {
// Database credentials
final String token = "<your personal access token here>";
final String jdbcUri = String.format("jdbc:cannerflow://<host>:<port>/cannerflow/<your workspace sql name here>?token=%s", token);

// Execute a query
Connection conn = DriverManager.getConnection(jdbcUri);
Statement stmt = conn.createStatement();
String sql = "SELECT * from lineitem";
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();

// Extract data from result set
// column count
int columnCount = metadata.getColumnCount();

for (int i = 1; i <= columnCount; i++ ) {
// print column name and column type
System.out.println(metadata.getColumnName(i));
System.out.println(metadata.getColumnType(i));
}

while(rs.next()) {
// iterate ResultSet
System.out.println(rs.getString("orderkey"));
}

// Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}