-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JdbcExample.java
135 lines (113 loc) · 4.84 KB
/
JdbcExample.java
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
125
126
127
128
129
130
131
132
133
134
135
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.examples.datagrid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.ignite.examples.ExampleNodeStartup;
/**
* This example demonstrates usage of Ignite JDBC driver.
* <p>
* Ignite nodes must be started in separate process using {@link ExampleNodeStartup} before running this example.
*/
public class JdbcExample {
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
print("JDBC example started.");
// Open JDBC connection
try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/")) {
print("Connected to server.");
// Create database objects.
try (Statement stmt = conn.createStatement()) {
// Create reference City table based on REPLICATED template.
stmt.executeUpdate("CREATE TABLE city (id LONG PRIMARY KEY, name VARCHAR) " +
"WITH \"template=replicated\"");
// Create table based on PARTITIONED template with one backup.
stmt.executeUpdate("CREATE TABLE person (id LONG, name VARCHAR, city_id LONG, " +
"PRIMARY KEY (id, city_id)) WITH \"backups=1, affinityKey=city_id\"");
// Create an index.
stmt.executeUpdate("CREATE INDEX on Person (city_id)");
}
print("Created database objects.");
// Populate City table with PreparedStatement.
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO city (id, name) VALUES (?, ?)")) {
stmt.setLong(1, 1L);
stmt.setString(2, "Forest Hill");
stmt.executeUpdate();
stmt.setLong(1, 2L);
stmt.setString(2, "Denver");
stmt.executeUpdate();
stmt.setLong(1, 3L);
stmt.setString(2, "St. Petersburg");
stmt.executeUpdate();
}
// Populate Person table with PreparedStatement.
try (PreparedStatement stmt =
conn.prepareStatement("INSERT INTO person (id, name, city_id) values (?, ?, ?)")) {
stmt.setLong(1, 1L);
stmt.setString(2, "John Doe");
stmt.setLong(3, 3L);
stmt.executeUpdate();
stmt.setLong(1, 2L);
stmt.setString(2, "Jane Roe");
stmt.setLong(3, 2L);
stmt.executeUpdate();
stmt.setLong(1, 3L);
stmt.setString(2, "Mary Major");
stmt.setLong(3, 1L);
stmt.executeUpdate();
stmt.setLong(1, 4L);
stmt.setString(2, "Richard Miles");
stmt.setLong(3, 2L);
stmt.executeUpdate();
}
print("Populated data.");
// Get data.
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs =
stmt.executeQuery("SELECT p.name, c.name FROM Person p INNER JOIN City c on c.id = p.city_id")) {
print("Query results:");
while (rs.next())
System.out.println(">>> " + rs.getString(1) + ", " + rs.getString(2));
}
}
// Drop database objects.
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("DROP TABLE Person");
stmt.executeUpdate("DROP TABLE City");
}
print("Dropped database objects.");
}
print("JDBC example finished.");
}
/**
* Prints message.
*
* @param msg Message to print before all objects are printed.
*/
private static void print(String msg) {
System.out.println();
System.out.println(">>> " + msg);
}
}