Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 30da159

Browse files
authoredOct 7, 2022
refactor: integrating sample code as subprojects (aws#214)
Integrate sample code snippets into subprojects that is compilable and easy to run.
1 parent ac1123f commit 30da159

14 files changed

+158
-134
lines changed
 

‎docs/using-the-jdbc-driver/DataSource.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ To use the AWS JDBC Driver with a connection pool, you must:
8181
ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);
8282
```
8383

84-
See [here](./sample-code/HikariSample.java) for a complete sample.
84+
See [here](../../examples/AWSDriverExample/src/main/java/software/amazon/DatasourceExample.java) for a simple AWS Driver Datasource example.
85+
86+
See [here](../../examples/HikariExample/src/main/java/software/amazon/HikariExample.java) for a complete Hikari example.
8587

8688
> **:warning:Note:** HikariCP supports either DataSource-based configuration or DriverManager-based configuration by specifying the `dataSourceClassName` or the `jdbcUrl`. When using the `AwsWrapperDataSource` you must specify the `dataSourceClassName`, therefore `HikariDataSource.setJdbcUrl` is not supported. For more information see HikariCP's [documentation](https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby).

‎docs/using-the-jdbc-driver/using-plugins/UsingTheAwsSecretsManagerPlugin.md

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,5 @@ The following properties are required for the AWS Secrets Manager Connection Plu
1818
| `secretsManagerRegion` | String | Yes | Set this value to be the region your secret is in. | `us-east-2` | `us-east-1` |
1919

2020
### Example
21-
The following example demonstrates using the AWS Advanced JDBC Driver to make a connection to a PostgreSQL database using credentials fetched from the AWS Secrets Manager:
22-
23-
```java
24-
import java.sql.Connection;
25-
import java.sql.DriverManager;
26-
import java.sql.ResultSet;
27-
import java.sql.SQLException;
28-
import java.sql.Statement;
29-
import java.util.Properties;
30-
31-
public class AwsSecretsManagerConnectionPluginPostgresqlSample {
32-
33-
private static final String CONNECTION_STRING = "jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/employees";
34-
35-
public static void main(String[] args) throws SQLException, ClassNotFoundException {
36-
// Set the AWS Secrets Manager Connection Plugin parameters and the JDBC Wrapper parameters.
37-
final Properties properties = new Properties();
38-
properties.setProperty("secretsManagerRegion", "us-east-2");
39-
properties.setProperty("secretsManagerSecretId", "secretId");
40-
41-
// Enable the AWS Secrets Manager Connection Plugin.
42-
properties.setProperty(
43-
"wrapperPlugins",
44-
"awsSecretsManager");
45-
46-
// Try and make a connection:
47-
try (final Connection conn = DriverManager.getConnection(CONNECTION_STRING, properties);
48-
final Statement statement = conn.createStatement();
49-
final ResultSet rs = statement.executeQuery("SELECT * FROM employees")) {
50-
while (rs.next()) {
51-
System.out.println(rs.getString("first_name"));
52-
}
53-
}
54-
}
55-
}
56-
```
21+
[AwsSecretsManagerConnectionPluginPostgresqlExample.java](../../../examples/AWSDriverExample/src/main/java/software/amazon/AwsSecretsManagerConnectionPluginPostgresqlExample.java)
22+
demonstrates using the AWS Advanced JDBC Driver to make a connection to a PostgreSQL database using credentials fetched from the AWS Secrets Manager:

‎docs/using-the-jdbc-driver/using-plugins/UsingTheIamAuthenticationPlugin.md

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,4 @@ IAM database authentication use is limited to certain database engines. For more
3434

3535

3636
## Sample code
37-
```java
38-
import software.amazon.jdbc.PropertyDefinition;
39-
import java.sql.Connection;
40-
import java.sql.DriverManager;
41-
import java.sql.ResultSet;
42-
import java.sql.SQLException;
43-
import java.sql.Statement;
44-
import java.util.Properties;
45-
46-
public class AwsIamAuthenticationPostgresqlSample {
47-
public static final String POSTGRESQL_CONNECTION_STRING =
48-
"jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/employees";
49-
private static final String USERNAME = "john_smith";
50-
51-
public static void main(String[] args) throws SQLException {
52-
53-
final Properties properties = new Properties();
54-
55-
// Enable AWS IAM database authentication and configure driver property values
56-
properties.setProperty(PropertyDefinition.PLUGINS.name, "iam");
57-
properties.setProperty(PropertyDefinition.USER.name, USERNAME);
58-
properties.setProperty(PropertyDefinition.TARGET_DRIVER_USER_PROPERTY_NAME.name, "user");
59-
properties.setProperty(PropertyDefinition.TARGET_DRIVER_PASSWORD_PROPERTY_NAME.name, "password");
60-
61-
// Attempt a connection
62-
try (Connection conn = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, properties);
63-
Statement statement = conn.createStatement();
64-
ResultSet result = statement.executeQuery("select aurora_db_instance_identifier()")) {
65-
66-
System.out.println(getResult(result));
67-
}
68-
}
69-
70-
static String getResult(final ResultSet result) throws SQLException {
71-
int cols = result.getMetaData().getColumnCount();
72-
StringBuilder builder = new StringBuilder();
73-
while (result.next()) {
74-
for (int i = 1; i <= cols; i++) {
75-
builder.append(result.getString(i)).append(" ");
76-
}
77-
builder.append("\n");
78-
}
79-
return builder.toString();
80-
}
81-
}
82-
```
37+
[AwsIamAuthenticationPostgresqlExample.java](../../../examples/AWSDriverExample/src/main/java/software/amazon/AwsIamAuthenticationPostgresqlExample.java)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
dependencies {
18+
implementation("org.postgresql:postgresql:42.5.0")
19+
implementation("software.amazon.jdbc:aws-advanced-jdbc-wrapper:1.0.0")
20+
implementation("software.amazon.awssdk:rds:2.17.289")
21+
implementation("software.amazon.awssdk:secretsmanager:2.17.285")
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Do not publish the Jar file for this subproject
16+
nexus.publish=false

‎docs/using-the-jdbc-driver/sample-code/AwsIamAuthenticationPostgresqlSample.java renamed to ‎examples/AWSDriverExample/src/main/java/software/amazon/AwsIamAuthenticationPostgresqlExample.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*
1717
*/
1818

19+
package software.amazon;
20+
1921
import software.amazon.jdbc.PropertyDefinition;
2022
import java.sql.Connection;
2123
import java.sql.DriverManager;
@@ -24,7 +26,7 @@
2426
import java.sql.Statement;
2527
import java.util.Properties;
2628

27-
public class AwsIamAuthenticationPostgresqlSample {
29+
public class AwsIamAuthenticationPostgresqlExample {
2830
public static final String POSTGRESQL_CONNECTION_STRING =
2931
"jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/employees";
3032
private static final String USERNAME = "john_smith";
@@ -36,27 +38,13 @@ public static void main(String[] args) throws SQLException {
3638
// Enable AWS IAM database authentication and configure driver property values
3739
properties.setProperty(PropertyDefinition.PLUGINS.name, "iam");
3840
properties.setProperty(PropertyDefinition.USER.name, USERNAME);
39-
properties.setProperty(PropertyDefinition.TARGET_DRIVER_USER_PROPERTY_NAME.name, "user");
40-
properties.setProperty(PropertyDefinition.TARGET_DRIVER_PASSWORD_PROPERTY_NAME.name, "password");
4141

4242
// Attempt a connection
4343
try (Connection conn = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, properties);
4444
Statement statement = conn.createStatement();
4545
ResultSet result = statement.executeQuery("select aurora_db_instance_identifier()")) {
4646

47-
System.out.println(getResult(result));
48-
}
49-
}
50-
51-
static String getResult(final ResultSet result) throws SQLException {
52-
int cols = result.getMetaData().getColumnCount();
53-
StringBuilder builder = new StringBuilder();
54-
while (result.next()) {
55-
for (int i = 1; i <= cols; i++) {
56-
builder.append(result.getString(i)).append(" ");
57-
}
58-
builder.append("\n");
47+
System.out.println(Util.getResult(result));
5948
}
60-
return builder.toString();
6149
}
62-
}
50+
}

‎docs/using-the-jdbc-driver/sample-code/AwsSecretsManagerConnectionPluginPostgresqlSample.java renamed to ‎examples/AWSDriverExample/src/main/java/software/amazon/AwsSecretsManagerConnectionPluginPostgresqlExample.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,34 @@
1414
* limitations under the License.
1515
*/
1616

17+
package software.amazon;
18+
1719
import java.sql.Connection;
1820
import java.sql.DriverManager;
1921
import java.sql.ResultSet;
2022
import java.sql.SQLException;
2123
import java.sql.Statement;
2224
import java.util.Properties;
25+
import software.amazon.jdbc.PropertyDefinition;
2326

24-
public class AwsSecretsManagerConnectionPluginPostgresqlSample {
27+
public class AwsSecretsManagerConnectionPluginPostgresqlExample {
2528

2629
private static final String CONNECTION_STRING = "jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/employees";
2730

28-
public static void main(String[] args) throws SQLException, ClassNotFoundException {
31+
public static void main(String[] args) throws SQLException {
2932
// Set the AWS Secrets Manager Connection Plugin parameters and the JDBC Wrapper parameters.
3033
final Properties properties = new Properties();
3134
properties.setProperty("secretsManagerRegion", "us-east-2");
3235
properties.setProperty("secretsManagerSecretId", "secretId");
3336

3437
// Enable the AWS Secrets Manager Connection Plugin.
35-
properties.setProperty(
36-
"wrapperPlugins",
37-
"awsSecretsManager");
38+
properties.setProperty(PropertyDefinition.PLUGINS.name, "awsSecretsManager");
3839

3940
// Try and make a connection:
4041
try (final Connection conn = DriverManager.getConnection(CONNECTION_STRING, properties);
4142
final Statement statement = conn.createStatement();
4243
final ResultSet rs = statement.executeQuery("SELECT * FROM employees")) {
43-
while (rs.next()) {
44-
System.out.println(rs.getString("first_name"));
45-
}
44+
System.out.println(Util.getResult(rs));
4645
}
4746
}
4847
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
23
*
3-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
47
*
5-
* Licensed under the Apache License, Version 2.0 (the "License").
6-
* You may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
8+
* http://www.apache.org/licenses/LICENSE-2.0
169
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
1715
*/
1816

19-
import software.amazon.jdbc.ds.AwsWrapperDataSource;
17+
package software.amazon;
18+
2019
import java.sql.Connection;
2120
import java.sql.ResultSet;
2221
import java.sql.SQLException;
2322
import java.sql.Statement;
2423
import java.util.Properties;
24+
import software.amazon.jdbc.ds.AwsWrapperDataSource;
2525

26-
public class DataSourceSample {
26+
public class DatasourceExample {
2727
private static final String USER = "username";
2828
private static final String PASSWORD = "password";
2929

@@ -48,11 +48,9 @@ public static void main(String[] args) throws SQLException {
4848

4949
// Try and make a connection:
5050
try (final Connection conn = ds.getConnection(USER, PASSWORD);
51-
final Statement statement = conn.createStatement();
52-
final ResultSet rs = statement.executeQuery("SELECT * FROM employees")) {
53-
while (rs.next()) {
54-
System.out.println(rs.getString("first_name"));
55-
}
51+
final Statement statement = conn.createStatement();
52+
final ResultSet rs = statement.executeQuery("SELECT * FROM employees")) {
53+
System.out.println(Util.getResult(rs));
5654
}
5755
}
5856
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon;
18+
19+
import java.sql.ResultSet;
20+
import java.sql.SQLException;
21+
22+
public class Util {
23+
static String getResult(final ResultSet result) throws SQLException {
24+
int cols = result.getMetaData().getColumnCount();
25+
StringBuilder builder = new StringBuilder();
26+
while (result.next()) {
27+
for (int i = 1; i <= cols; i++) {
28+
builder.append(result.getString(i)).append(" ");
29+
}
30+
builder.append("\n");
31+
}
32+
return builder.toString();
33+
}
34+
}

‎examples/HibernateExample/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Do not publish the Jar file for the benchmarks subproject
15+
# Do not publish the Jar file for this subproject
1616
nexus.publish=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
dependencies {
18+
implementation("org.postgresql:postgresql:42.5.0")
19+
implementation("software.amazon.jdbc:aws-advanced-jdbc-wrapper:1.0.0")
20+
implementation("com.zaxxer:HikariCP:4.0.3")
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Do not publish the Jar file for this subproject
16+
nexus.publish=false

0 commit comments

Comments
 (0)
Please sign in to comment.