Establishing a Connection
Creating a JDBC Data Source
You can create a JDBC data source to connect from your Java application. Creating a JDBC data source based on the CData JDBC Driver for SQL Server consists of three basic steps:
- Add the driver JAR file to the classpath. The JAR file is located in the lib subfolder of the installation directory. Note that the .lic file must be located in the same folder as the JAR file.
- Provide the driver class. For example:
cdata.jdbc.sql.SQLDriver
- Provide the JDBC URL. For example:
jdbc:sql:user=myuser;password=mypassword;Server=localhost;Database=Northwind; or jdbc:cdata:sql:user=myuser;password=mypassword;Server=localhost;Database=Northwind;
You can use the second format above whenever there is a conflict in your application between drivers using the same URL format to ensure you are using the CData driver. The URL must start with either "jdbc:sql:" or "jdbc:cdata:sql:" and can include any of the connection properties in name-value pairs separated with semicolons.
Connecting to SQL Server
You can use the CData JDBC Driver for SQL Server to connect to any instance of Microsoft SQL Server, Azure SQL Server, or Azure Data Warehouse.
Microsoft SQL Server
Set the following connection properties to connect to SQL Server:
Azure SQL Server or AZure Data Warehouse
Set the following connection properties to connect to Azure SQL Server or Azure Data Warehouse:
- Server: The server running Azure. You can find this by logging into the Azure portal and navigating to SQL databases (or SQL data warehouses) > Select your database > Overview > Server name.
- Database: The name of the database, as seen in the Azure portal on the SQL databases (or SQL warehouses) page.
Authenticating to SQL Server
The driver supports authenticating to SQL Server using SQL Server authentication or Kerberos authentication.Microsoft SQL Server
To authenticate to Microsoft SQL Server using your SQL Server user login credentials, set the following:
- AuthScheme: Set this to Password.
- User: The username provided for authentication with SQL Server.
- Password: The password associated with the authenticating user.
Kerberos
To authenticate to SQL Server with Kerberos, set AuthScheme to KERBEROS.
Authenticating to SQL Server via Kerberos requires you to define authentication properties and to choose how Kerberos should retrieve authentication tickets.
Retrieve Kerberos Tickets
Kerberos tickets are used to authenticate the requester's identity. The use of tickets instead of formal logins/passwords eliminates the need to store passwords locally or send them over a network. Users are reauthenticated (tickets are refreshed) whenever they log in at their local computer or enter kinit USER at the command prompt.The driver provides three ways to retrieve the required Kerberos ticket, depending on whether or not the KRB5CCNAME and/or KerberosKeytabFile variables exist in your environment.
MIT Kerberos Credential Cache File
This option enables you to use the MIT Kerberos Ticket Manager or kinit command to get tickets. With this option there is no need to set the User or Password connection properties.
This option requires that KRB5CCNAME has been created in your system.
To enable ticket retrieval via MIT Kerberos Credential Cache Files:
- Ensure that the KRB5CCNAME variable is present in your environment.
- Set KRB5CCNAME to a path that points to your credential cache file. (For example, C:\krb_cache\krb5cc_0 or /tmp/krb5cc_0.) The credential cache file is created when you use the MIT Kerberos Ticket Manager to generate your ticket.
- To obtain a ticket:
- Open the MIT Kerberos Ticket Manager application.
- Click Get Ticket.
- Enter your principal name and password.
- Click OK.
If the ticket is successfully obtained, the ticket information appears in Kerberos Ticket Manager and is stored in the credential cache file.
The driver uses the cache file to obtain the Kerberos ticket to connect to SQL Server.
Note: If you would prefer not to edit KRB5CCNAME, you can use the KerberosTicketCache property to set the file path manually. After this is set, the driver uses the specified cache file to obtain the Kerberos ticket to connect to SQL Server.
Keytab File
If your environment lacks the KRB5CCNAME environment variable, you can retrieve a Kerberos ticket using a Keytab File.
To use this method, set the User property to the desired username, and set the KerberosKeytabFile property to a file path pointing to the keytab file associated with the user.
User and Password
If your environment lacks the KRB5CCNAME environment variable and the KerberosKeytabFile property has not been set, you can retrieve a ticket using a user and password combination.
To use this method, set the User and Password properties to the user/password combination that you use to authenticate with SQL Server.
Enabling Cross-Realm Authentication
More complex Kerberos environments can require cross-realm authentication where multiple realms and KDC servers are used. For example, they might use one realm/KDC for user authentication, and another realm/KDC for obtaining the service ticket.To enable this kind of cross-realm authentication, set the KerberosRealm and KerberosKDC properties to the values required for user authentication. Also, set the KerberosServiceRealm and KerberosServiceKDC properties to the values required to obtain the service ticket.
NTLM
You can authenticate with a username and password using the NTLM authentication mechanism.To authenticate, set the following:
- AuthScheme: NTLM
- User: The name of the authenticating user.
- Password: The password associated with the authenticating user.
Alternatively, you can use OAuth by setting AuthScheme to AzureAd, AzurePassword, or AzureMSI. All OAuth connections require setting the AzureTenant connection property to the Id of the tenant the SQL Server database is hosted on.
Entra ID (Azure AD)
Note: Microsoft has rebranded Azure AD as Entra ID. In topics that require the user to interact with the Entra ID Admin site, we use the same names Microsoft does. However, there are still CData connection properties whose names or values reference "Azure AD".
Microsoft Entra ID is a multi-tenant, cloud-based identity and access management platform. It supports OAuth-based authentication flows that enable the driver to access SQL Server endpoints securely.
Authentication to Entra ID via a web application always requires that you first create and register a custom OAuth application. This enables your application to define its own redirect URI, manage credential scope, and comply with organization-specific security policies.
For full instructions on how to create and register a custom OAuth application, see Creating a Custom Azure AD Application.
After setting AuthScheme to AzureAD, the steps to authenticate vary, depending on the environment. For details on how to connect from desktop applications, web-based workflows, or headless systems, see the following sections.
Desktop Applications
You can authenticate from a desktop application using either the driver's embedded OAuth application or a custom OAuth application registered in Microsoft Entra ID.
Option 1: Use the Embedded OAuth Application
This is a pre-registered application, included with the driver. It simplifies setup and eliminates the need to register your own credentials and is ideal for development environments, single-user tools, or any setup where quick and easy authentication is preferred.
Set the following connection properties:
- AuthScheme: AzureAD
- InitiateOAuth:
- GETANDREFRESH – Use for the initial login. Launches the login page and saves tokens.
- REFRESH – Use this setting when you have already obtained valid access and refresh tokens. Reuses stored tokens without prompting the user again.
When you connect, the driver opens the Microsoft Entra sign-in page in your default browser. After signing in and granting access, the driver retrieves the access and refresh tokens and saves them to the path specified by OAuthSettingsLocation.
Option 2: Use a Custom OAuth Application
If your organization requires more control, such as managing security policies, redirect URIs, or application branding, you can instead register a custom OAuth application in Microsoft Entra ID and provide its values during connection.
During registration, record the following values:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
For full instructions on how to register a custom OAuth application and configure redirect URIs, see Creating a Custom Azure AD Application.
Set the following connection properties:
- AuthScheme: AzureAD
- InitiateOAuth:
- GETANDREFRESH – Use for the initial login. Launches the login page and saves tokens.
- REFRESH – Use this setting when you have already obtained valid access and refresh tokens. Reuses stored tokens without prompting the user again.
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
After authentication, tokens are saved to OAuthSettingsLocation. These values persist across sessions and are used to automatically refresh the access token when it expires, so you don't need to log in again on future connections.
Web Applications
To authenticate from a web application, you must register a custom OAuth application in Microsoft Entra ID (formerly Azure Active Directory). Embedded OAuth apps are not supported in this context because web-based flows require a registered redirect URI and centralized credential management.
This approach is designed for hosted, multi-user environments where access must be delegated through a secure, standards-compliant OAuth workflow. It gives your organization control over the OAuth client, redirect URI, branding, and permissions scope.
Before you begin: Register a custom OAuth application in the Azure portal. During registration, collect the following values:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
For full instructions on how to register a custom OAuth application and configure redirect URIs, see Creating a Custom Azure AD Application.
To authenticate using AzureAD in a web application, configure the following connection properties:
- AuthScheme: AzureAD
- InitiateOAuth: OFF – Disables automatic login prompts.
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
Because web applications typically manage OAuth flows manually on the server-side, InitiateOAuth must be set to OFF. This allows you to explicitly control when and how tokens are retrieved and exchanged using stored procedures.
After configuring these properties, follow the steps below to obtain and exchange OAuth tokens:
- Call the GetOAuthAuthorizationUrl stored procedure:
- CallbackURL: Set to your registered redirect URI
- Open the returned URL in a browser. Sign in with a Microsoft Entra ID account and grant access.
- After signing in, you are redirected to your CallbackURL with a code parameter in the query string.
- Extract the code and pass it to the GetOAuthAccessToken stored procedure:
- AuthMode: WEB
- Verifier: The authorization code from the CallbackURL
- The procedure returns:
- OAuthAccessToken: Used for authentication.
- OAuthRefreshToken: Used to refresh the access token.
- ExpiresIn: The lifetime of the access token in seconds.
To enable automatic token refresh, configure the following connection properties:
- InitiateOAuth: REFRESH
- OAuthRefreshToken: Your previously obtained refresh token
When InitiateOAuth is set to REFRESH, the driver uses the provided refresh token to request a new access token automatically.
After a successful connection, the driver saves the updated access and refresh tokens to the file specified by OAuthSettingsLocation.
You only need to repeat the full OAuth authorization flow if the refresh token expires, is revoked, or becomes invalid.
For more background on OAuth flows in Microsoft Entra ID, see Microsoft Entra Authentication Overview.
Headless Machines
Headless environments like CI/CD pipelines, background services, or server-based integrations do not have an interactive browser. To authenticate using AzureAD, you must complete the OAuth flow on a separate device with a browser and transfer the authentication result to the headless system.
Setup options:
- Obtain and exchange a verifier code
- Use another device to sign in and retrieve a verifier code, which the headless system uses to request tokens.
- Transfer an OAuth settings file
- Authenticate on another device, then copy the stored token file to the headless environment.
Using a Verifier Code
- On a device with a browser:
- If using a custom OAuth app, set the following properties:
- InitiateOAuth: OFF
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- Call the GetOAuthAuthorizationUrl stored procedure to generate a sign-in URL.
- Open the returned URL in a browser. Sign in and grant grant permissions to the driver. You are redirected to the callback URL, which contains the verifier code.
- After signing in, save the value of the code parameter from the redirect URL. You will use this later to set the OAuthVerifier connection property.
- If using a custom OAuth app, set the following properties:
- On the headless machine:
- Set the following properties:
- AuthScheme: AzureAD
- InitiateOAuth: REFRESH
- OAuthVerifier: The verifier code you saved.
- OAuthSettingsLocation: The path of the file that holds the OAuth token values.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- After tokens are saved, reuse them by setting:
- InitiateOAuth: REFRESH
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- Set the following properties:
Transferring OAuth Settings
- On a device with a browser:
- Connect using the instructions in the Desktop Applications section.
- After connecting, tokens are saved to the file path in OAuthSettingsLocation. The default filename is OAuthSettings.txt.
- On the headless machine:
- Copy the OAuth settings file to the machine.
- Set the following properties:
- AuthScheme: AzureAD
- InitiateOAuth: REFRESH
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
After setup, the driver uses the stored tokens to refresh the access token automatically, no browser or manual login is required.
Azure Service Principal
Note: Microsoft has rebranded Azure AD as Entra ID. In topics that require the user to interact with the Entra ID Admin site, we use the same names Microsoft does. However, there are still CData connection properties whose names or values reference "Azure AD".
Service principals are security objects within a Microsoft Entra ID (Azure AD) application that define what that application can do within a specific tenant.
Service principals are created in the Entra admin center, also accessible through the Azure portal.
As part of the creation process we also specify whether the service principal will access Entra resources via a client secret or a certificate.
Depending on the service you are connecting to, a tenant administrator may need to enable Service Principal authentication or assign the Service Principal to the appropriate roles or security groups.
Instead of being tied to a particular user, service principal permissions are based on the roles assigned to them. These roles determine which resources the application can access and which operations it can perform.
When authenticating using a service principal, you must register an application with an Entra tenant, as described in Creating a Service Principal App in Entra ID (Azure AD).
This subsection describes properties you must set before you can connect. These vary, depending on whether you will authenticate via a client secret or a certificate.
Authentication with Client Secret
- AuthScheme: AzureServicePrincipal.
- AzureTenant: The Azure AD tenant to which you will connect.
- OAuthClientId: The client ID in your application settings.
- OAuthClientSecret: The client secret in your application settings.
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
Authentication with Certificate
- AuthScheme: AzureServicePrincipalCert.
- AzureTenant: The Azure AD tenant to which you will connect.
- OAuthClientId: The client Id in your application settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The JWT Certificate store type.
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
Azure Password
To authenticate directly to Azure Active Directory using your Azure credentials, specify the following connection properties:
- AuthScheme: AzurePassword.
- User: The account for the authenticating Azure user.
- Password: The authenticating Azure user's password.
- AzureTenant: The Directory (tenant) ID of the custom Azure AD application used to authenticate to SQL Server on Azure. To find this, go to the custom application's Overview page.
Managed Service Identity (MSI)
If you are running SQL Server on an Azure VM and want to automatically obtain Managed Service Identity (MSI) credentials to connect, set AuthScheme to AzureMSI.
User-Managed Identities
To obtain a token for a managed identity, use the OAuthClientId property to specify the managed identity's client_id.If your VM has multiple user-assigned managed identities, you must also specify OAuthClientId.