Using the Driver from Java

This topic provides instructions on how to use the Data 360 JDBC driver in your Java projects.

Note: External Client Apps support the JWT Bearer Flow for JDBC connections. Username-Password Flow and Refresh Token Flow are not supported with External Client Apps.

Download and Include in Your Project 

The Data 360 JDBC driver JAR file is available in the global Maven package registry at https://central.sonatype.com/artifact/com.salesforce.datacloud/jdbc.

Maven is a popular build automation tool that’s used primarily for Java projects. It helps manage dependencies, builds, and reporting. To include the JDBC driver in your Maven project, add this dependency to your pom.xml file.

1<dependency>
2    <groupId>com.salesforce.datacloud</groupId>
3    <artifactId>jdbc</artifactId>
4    <version>${jdbc.version}</version>
5</dependency>

The fully qualified class name for the driver is com.salesforce.datacloud.jdbc.DataCloudJDBCDriver.

If you cloned the JDBC driver repository, you can build and test the driver locally by using this Maven command.

1mvn clean install

Creating a Connection from URL and Auth Properties 

To establish a connection, the driver requires a connection URL and authentication properties, as detailed in the Driver URL and Properties section. Here’s the general structure for establishing a connection by using the JDBC driver.

1import java.sql.Connection;
2import java.util.Properties;
3import java.sql.DriverManager;
4
5class MyDataCloudJDBCApp {
6    public static Connection createMyConnection() {
7        Properties properties = new Properties();
8        properties.put("propName", YOUR_PROP_VALUE);
9
10        return DriverManager.getConnection(
11            "jdbc:salesforce-datacloud://login.salesforce.com",
12            properties
13        );
14    }
15}

Here is an example for JWT Bearer Flow authentication with External Client Apps.

JWT Bearer Flow 

For property descriptions, see Required Properties for JWT Bearer Flow.

1import java.sql.Connection;
2import java.util.Properties;
3import java.sql.DriverManager;
4
5class MyDataCloudJDBCApp {
6    public static Connection createMyConnection() {
7        Properties properties = new Properties();
8        properties.put("userName", YOUR_USERNAME);
9        properties.put("clientId", YOUR_CLIENT_ID);
10        properties.put("privateKey", YOUR_PRIVATE_KEY);
11
12        return DriverManager.getConnection(
13            "jdbc:salesforce-datacloud://login.salesforce.com",
14            properties
15        );
16    }
17}

Note: The clientSecret (Consumer Secret) is not required for JWT Bearer Flow authentication with External Client Apps.

Complete Working Example 

This section provides a concise Java example that demonstrates how to establish a connection, execute a query, process the results, and release resources. The example uses a PreparedStatement to execute a parameterized query, which helps prevent SQL injection vulnerabilities and can improve performance.

Before running this example:

  • Ensure that you have Java 8 or later installed on your system.
  • Add the Data 360 JDBC driver and the SLF4j library (for logging) to your project’s classpath. If you’re using Maven, include the appropriate dependencies in your pom.xml file. If you’re not using a build tool, manually download the JAR files for the JDBC driver and SLF4j, and add them to your project’s classpath.
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.PreparedStatement;
4import java.sql.ResultSet;
5import java.util.Properties;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8
9public class MyDataCloudJDBCApp {
10
11    private static final Logger log = LoggerFactory.getLogger(MyDataCloudJDBCApp.class);
12
13    public static void main(String args) throws Exception {
14        Connection connection = createConnection();
15        runQuery(connection);
16        connection.close();
17    }
18
19    public static Connection createConnection() throws Exception {
20       Properties properties = new Properties();
21        properties.put("userName", YOUR_USERNAME);
22        properties.put("clientId", YOUR_CLIENT_ID);
23        properties.put("privateKey", YOUR_PRIVATE_KEY);
24
25        return DriverManager.getConnection(
26            "jdbc:salesforce-datacloud://login.salesforce.com",
27            properties
28        );
29    }
30
31    public static void runQuery(Connection connection) throws Exception {
32        // 1. Prepare Statement
33        PreparedStatement statement = connection.prepareStatement(
34            "SELECT \"FirstName__c\",\n"
35                + "     \"BirthDate__c\",\n"
36                + "     \"YearlyIncome__c\"\n"
37                + "FROM \"Individual__dlm\"\n"
38                + "WHERE FirstName__c =?\n"
39                + "     AND YearlyIncome__c >?"
40        );
41
42        // 2. Set Parameters
43        statement.setString(1, "Angella");
44        statement.setInt(2, 1000);
45
46        // 3. Execute Query
47        ResultSet resultSet = statement.executeQuery();
48
49        // 4. Process Results
50        while (resultSet.next()) {
51            log.info(
52                "FirstName: {}, BirthDate__c: {}, YearlyIncome__c: {}",
53                resultSet.getString("FirstName__c"),
54                resultSet.getTimestamp("BirthDate__c"),
55                resultSet.getInt("YearlyIncome__c")
56            );
57        }
58
59        // 5. Close Resources
60        resultSet.close();
61        statement.close();
62    }
63}

The example code demonstrates these steps:

  1. Establish a Connection: The createConnection() method establishes a connection to Data 360 by using the DriverManager.getConnection() method along with JWT Bearer Flow authentication credentials. Make sure you replace the placeholder values with your actual username, client ID, and private key.

  2. Prepare the SQL Statement: The connection.prepareStatement() method creates a PreparedStatement object. The SQL query in this example uses placeholders (?) to represent values that will be dynamically inserted later. This technique, known as parameterized querying, is crucial for preventing SQL injection vulnerabilities and can improve query performance.

  3. Set Parameter Values: The code sets the values for the placeholders in the prepared statement.

    • statement.setString(1, "Angella") sets the first placeholder (index 1) to the string value “Angella.”
    • statement.setInt(2, 1000) sets the second placeholder (index 2) to the integer value 1000. The JDBC driver provides various methods, such as setString, setInt, and setDate, to set parameter values based on their data types.
  4. Execute the Query: The statement.executeQuery() method executes the parameterized query. Because the parameter values have already been set, there’s no need to pass any additional values here.

  5. Process the Result Set: The code uses a while loop to iterate through the ResultSet obtained from the query execution. The resultSet.next() method moves the cursor to the next row in the result set, and the code within the loop retrieves and logs the data for each row.

  6. Close Resources: It’s important to close the ResultSet and the PreparedStatement to release any database resources that are held by these objects.

  7. Execute the Java Application: After you set up your project and complete the code, compile and run your Java application to execute the example.

Next Steps 

You can now query and interact with your Data 360 data through your Java application. See the Get Started with Data 360 SQL guide.