---
schema_version: "1"
id: 5ba9ab38c9d543b81b7a95e1360063420c41158d428f3d08e3ae71961b6cc261
title: Using the Driver from Java
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/data-cloud-query-guide/guides/get-started-dc-jdbc/jdbc-java.md
canonical_url: https://developer.salesforce.com/docs/data/data-cloud-query-guide/guide/jdbc-java.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3f43902aad0dfa82242c8433d50275a08333661d979e6f8fbf30801095dbfd61
products:
  - Data 360 Query Guide
  - Data 360 Developer Documentation
---
# 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](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.

```xml
<dependency>
    <groupId>com.salesforce.datacloud</groupId>
    <artifactId>jdbc</artifactId>
    <version>${jdbc.version}</version>
</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.

```bash
mvn 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](https://developer.salesforce.com/docs/data/data-cloud-query-guide/guide/jdbc-setup.md#driver-url-and-properties) section. Here’s the general structure for establishing a connection by using the JDBC driver.

```java
import java.sql.Connection;
import java.util.Properties;
import java.sql.DriverManager;

class MyDataCloudJDBCApp {
    public static Connection createMyConnection() {
        Properties properties = new Properties();
        properties.put("propName", YOUR_PROP_VALUE);

        return DriverManager.getConnection(
            "jdbc:salesforce-datacloud://login.salesforce.com",
            properties
        );
    }
}
```

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](https://developer.salesforce.com/docs/data/data-cloud-query-guide/guide/jdbc-setup.md#required-properties-for-jwt-bearer-flow).

```java
import java.sql.Connection;
import java.util.Properties;
import java.sql.DriverManager;

class MyDataCloudJDBCApp {
    public static Connection createMyConnection() {
        Properties properties = new Properties();
        properties.put("userName", YOUR_USERNAME);
        properties.put("clientId", YOUR_CLIENT_ID);
        properties.put("privateKey", YOUR_PRIVATE_KEY);

        return DriverManager.getConnection(
            "jdbc:salesforce-datacloud://login.salesforce.com",
            properties
        );
    }
}
```

> **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.

```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyDataCloudJDBCApp {

    private static final Logger log = LoggerFactory.getLogger(MyDataCloudJDBCApp.class);

    public static void main(String args) throws Exception {
        Connection connection = createConnection();
        runQuery(connection);
        connection.close();
    }

    public static Connection createConnection() throws Exception {
       Properties properties = new Properties();
        properties.put("userName", YOUR_USERNAME);
        properties.put("clientId", YOUR_CLIENT_ID);
        properties.put("privateKey", YOUR_PRIVATE_KEY);

        return DriverManager.getConnection(
            "jdbc:salesforce-datacloud://login.salesforce.com",
            properties
        );
    }

    public static void runQuery(Connection connection) throws Exception {
        // 1. Prepare Statement
        PreparedStatement statement = connection.prepareStatement(
            "SELECT \"FirstName__c\",\n"
                + "     \"BirthDate__c\",\n"
                + "     \"YearlyIncome__c\"\n"
                + "FROM \"Individual__dlm\"\n"
                + "WHERE FirstName__c =?\n"
                + "     AND YearlyIncome__c >?"
        );

        // 2. Set Parameters
        statement.setString(1, "Angella");
        statement.setInt(2, 1000);

        // 3. Execute Query
        ResultSet resultSet = statement.executeQuery();

        // 4. Process Results
        while (resultSet.next()) {
            log.info(
                "FirstName: {}, BirthDate__c: {}, YearlyIncome__c: {}",
                resultSet.getString("FirstName__c"),
                resultSet.getTimestamp("BirthDate__c"),
                resultSet.getInt("YearlyIncome__c")
            );
        }

        // 5. Close Resources
        resultSet.close();
        statement.close();
    }
}
```

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](https://developer.salesforce.com/docs/data/data-cloud-query-guide/get-started-dc-sql/dc-query-section.md) guide.
