-
Notifications
You must be signed in to change notification settings - Fork 13
/
QueryExecutionExamples.java
165 lines (146 loc) · 6.02 KB
/
QueryExecutionExamples.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 sample.applications;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* This class provides some examples on how to send queries to Amazon Neptune with the JDBC driver.
*/
public class QueryExecutionExamples {
private final Connection connection;
/**
* Constructor initializes the {@link java.sql.Connection}.
*
* @throws SQLException If creating the {@link Connection} fails.
*/
QueryExecutionExamples() throws SQLException {
connection = AuthenticationExamples.createIAMAuthConnection();
}
/**
* Helper function to execute queries.
*
* @param query Query to execute.
* @return {@link java.sql.ResultSet} after executing query.
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
ResultSet executeQuery(final String query) throws SQLException {
final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query);
statement.close();
return resultSet;
}
/**
* This function sends a query with a Boolean literal and returns the value as a Boolean.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryLiteralBooleanExample() throws SQLException {
final String query = "RETURN true AS n";
final ResultSet resultSet = executeQuery(query);
// Value will be true.
final Boolean value = resultSet.getBoolean(0);
resultSet.close();
}
/**
* This function sends a query with a Integer literal and returns the value as a Integer.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryLiteralIntegerExample() throws SQLException {
final String query = "RETURN 1 AS n";
final ResultSet resultSet = executeQuery(query);
// Value will be 1.
final Integer value = resultSet.getInt(0);
resultSet.close();
}
/**
* This function sends a query with a Float literal and returns the value as a Float.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryLiteralFloatExample() throws SQLException {
final String query = "RETURN 1.0 AS n";
final ResultSet resultSet = executeQuery(query);
// Value will be 1.0.
final Float value = resultSet.getFloat(0);
resultSet.close();
}
/**
* This function sends a query with a String literal and returns the value as a String.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryLiteralStringExample() throws SQLException {
final String query = "RETURN 'hello' AS n";
final ResultSet resultSet = executeQuery(query);
// Value will be "hello".
final String value = resultSet.getString(0);
resultSet.close();
}
/**
* This function sends a query with a Date literal and returns the value as a Date.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryLiteralDateExample() throws SQLException {
final String query = "RETURN date(\"1993-03-30\") AS n";
final ResultSet resultSet = executeQuery(query);
// Value will be the date 1993-03-03 (yyyy-mm-dd).
final java.sql.Date value = resultSet.getDate(0);
resultSet.close();
}
/**
* This function sends a query with a node and returns the value as a String.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryNodeExample() throws SQLException {
final String query = "CREATE (node:Foo {hello:'world'}) RETURN node";
final ResultSet resultSet = executeQuery(query);
// Value will be the String "([Foo] : {hello=world})".
final String value = resultSet.getString(0);
resultSet.close();
}
/**
* This function sends a query with a relationship and returns the value as a String.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryRelationshipExample() throws SQLException {
final String query = "CREATE (node1:Foo)-[rel:Rel {hello:'world'}]->(node2:Bar) RETURN rel";
final ResultSet resultSet = executeQuery(query);
// Value will be the String "[Rel : {hello=world}]".
final String value = resultSet.getString(0);
resultSet.close();
}
/**
* This function sends a query with a path and returns the value as a String.
*
* @throws SQLException If {@link java.sql.Statement} creation or query execution fails.
*/
void queryPathExample() throws SQLException {
final String query = "CREATE p=(l:Person { name:'Foo'})-[:WORKS {position:'developer'}]->(bqt:Company " +
"{product:'software'})<-[:WORKS {position:'developer'}]-(v:Person { name:'Bar'}) RETURN p";
final ResultSet resultSet = executeQuery(query);
// Value will be the String
// "([Person] : {name=Foo})-[WORKS : {position=developer0}]->([Company] : {product=software})
// <-[WORKS : {position=developer}]-([Person] : {name=Bar})".
final String value = resultSet.getString(0);
resultSet.close();
}
}