@@ -38,15 +38,15 @@ database.
3838 $ touch ConnectDatabase.java
3939 ` ` `
4040
41- b. Copy the following code for your DataStax Driver into the ` ConnectDatabase.java ` file.
42- The following example implements a ` ConnectDatabase` class to connect to your Apollo database,
43- runs a CQL query, and prints the output to the console.
41+ b. ** Programmatic configuration ** . Copy the following code for your DataStax Driver into the
42+ ` ConnectDatabase.java ` file. The following example implements a ` ConnectDatabase` class to
43+ connect to your Apollo database, runs a CQL query, and prints the output to the console.
4444
4545 ** Note:** With the `CqlSession.builder ()` object, make sure to set the path to the secure
46- connect bundle for your Apollo database (** " /path/to/secure-connect-database_name.zip " **) in
47- the `withCloudSecureConnectBundle () ` method as shown in the following example.
48- If converting from using the open source Cassandra Java Driver to the DSE Java Driver, ensure
49- that you change ` CqlSession ` to ` DseSession ` .
46+ connect bundle for your Apollo database in the `withCloudSecureConnectBundle () ` method as
47+ shown in the following example. If converting from using the open source Cassandra Java Driver
48+ to the DSE Java Driver, ensure that you change ` CqlSession ` to ` DseSession ` .
49+
5050 * DataStax Java Driver for Apache Cassandra 4.x (recommended)
5151
5252 ` ` ` java
@@ -78,7 +78,7 @@ database.
7878 }
7979 }
8080 ` ` `
81- * DSE Java 2.x
81+ * DataStax Java Driver for DataStax Enterprise (DSE) 2.x
8282
8383 ` ` ` java
8484 import com.datastax.dse.driver.api.core.DseSession;
@@ -110,7 +110,63 @@ database.
110110 }
111111 ` ` `
112112
113- c. Save and close the ConnectDatabase.java file.
113+ c. ** File-based configuration** . An alternative to the programmatic configuration method
114+ detailed above is to include the information required to connect in the driver' s configuration
115+ file (`application.conf`). Merge the following options with any other options that you might
116+ want to include in the configuration file:
117+
118+ ```hocon
119+ basic {
120+ # change this to match the target keyspace
121+ session-keyspace = keyspace_name
122+ cloud {
123+ # change this to match bundle' s location; can be either a path on the local filesystem
124+ # or a valid URL, e.g. http://acme.com/path/to/secure-connect-database_name.zip
125+ secure-connect-bundle = /path/to/secure-connect-database_name.zip
126+ }
127+ }
128+ advanced {
129+ auth-provider {
130+ class = PlainTextAuthProvider
131+ # change below to match the appropriate credentials
132+ username = user_name
133+ password = password
134+ }
135+ }
136+ ` ` `
137+
138+ For more information about the driver configuration mechanism, refer to the
139+ [driver documentation].
140+
141+ With the above configuration, your ConnectDatabase.java file should be simplified as shown
142+ below:
143+
144+ ` ` ` java
145+ import com.datastax.oss.driver.api.core.CqlSession;
146+ import com.datastax.oss.driver.api.core.cql.ResultSet;
147+ import com.datastax.oss.driver.api.core.cql.Row;
148+
149+ public class ConnectDatabase {
150+
151+ public static void main(String[] args) {
152+ // Create the CqlSession object; it will read the configuration file and pick the right
153+ // values to connect to the Apollo database.
154+ try (CqlSession session = CqlSession.builder().build ()) {
155+ // Select the release_version from the system.local table:
156+ ResultSet rs = session.execute(" select release_version from system.local" );
157+ Row row = rs.one ();
158+ //Print the results of the CQL query to the console:
159+ if (row ! = null) {
160+ System.out.println(row.getString(" release_version" ));
161+ } else {
162+ System.out.println(" An error occurred." );
163+ }
164+ }
165+ }
166+ }
167+ ` ` `
168+
169+ d. Save and close the ConnectDatabase.java file.
114170
115171[Download Maven]: https://maven.apache.org/download.cgi
116172[Install Maven]: https://maven.apache.org/install.html
@@ -121,3 +177,4 @@ database.
121177[Download the secure connect bundle - GCP]: https://helpdocs.datastax.com/gcp/dscloud/apollo/dscloudObtainingCredentials.html
122178[Download the secure connect bundle - AWS]: https://helpdocs.datastax.com/aws/dscloud/apollo/dscloudObtainingCredentials.html
123179[Example pom.xml file]: ../core/integration/# minimal-project-structure
180+ [driver documentation]: ../core/configuration/
0 commit comments