From 00314e643ee6570ed6025630616ad0df70789447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 27 Nov 2020 01:34:23 +0100 Subject: [PATCH] docs: add connection example to readme (#281) --- .readme-partials.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .readme-partials.yaml diff --git a/.readme-partials.yaml b/.readme-partials.yaml new file mode 100644 index 00000000..47e65e14 --- /dev/null +++ b/.readme-partials.yaml @@ -0,0 +1,25 @@ +custom_content: | + ### Creating a JDBC Connection + + The following example shows how to create a JDBC connection to Cloud Spanner and execute a simple query. + + ```java + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", + projectId, instanceId, databaseId))) { + try (Statement statement = connection.createStatement()) { + try (ResultSet rs = statement.executeQuery("SELECT CURRENT_TIMESTAMP()")) { + while (rs.next()) { + System.out.printf( + "Connected to Cloud Spanner at [%s]%n", rs.getTimestamp(1).toString()); + } + } + } + } + ```