Using the Driver from Java

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

The Salesforce Data Cloud 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 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.

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.

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.

Here are specific examples for each OAuth flow type.

For property descriptions, see JWT Bearer Flow.

For property descriptions, see Username-Password Flow.

For property descriptions, see Refresh Token Flow.

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 utilizes 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 a compatible Java Development Kit (JDK) installed on your system.
  • Add the Salesforce Data Cloud 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.

The example code demonstrates these steps.

  1. Establish a Connection: The createConnection() method establishes a connection to Data Cloud by using the DriverManager.getConnection() method along with your authentication credentials. Make sure that you replace the placeholder credentials with your actual credentials.

  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 held by these objects.

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

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