MicrosoftSQL Server JDBC for macOS

This tutorial shows how to set up a data source and connect to a Microsoft® SQL Server® database using the Database Explorer app or the command line. This tutorial uses the Microsoft JDBC Driver 4.0 for Microsoft SQL Server to connect to a Microsoft SQL Server 2016 Express database.

Using the Driver. The following subsections show the formatting of JDBC connection strings for MariaDB and MySQL database servers. Additionally, sample code. Install the driver on a Mac system Download the latest MySQL JDBC Driver from the MySQL website. Choose the Platform Independent option and download the driver as a ZIP archive. Locate the JDBC driver mysql-connector-java.jar inside the downloaded ZIP archive.

Step 1. Verify the driver installation.

If the JDBC driver for SQL Server is not installed on your computer, find the link on the Driver Installation page to install the driver. Follow the instructions to download and install this driver on your computer.

Step 2. Set up the data source.

You set up a data source using the Database Explorer app or the command line.

Set Up Data Source Using Database Explorer App

  1. Open the Database Explorer app by clicking the Apps tab on the MATLAB® Toolstrip. Then, on the right of the Apps section, click the Show more arrow to open the apps gallery. Under Database Connectivity and Reporting, click Database Explorer. Alternatively, enter databaseExplorer at the command line.

  2. In the Data Source section, select Configure Data Source > Configure JDBC data source.

    The JDBC Data Source Configuration dialog box opens.

  3. In the Name box, enter a name for your data source. You use this name to establish a connection to your database.

  4. From the Vendor list, select Microsoft SQL Server.

  5. In the Driver Location box, enter the full path to the JDBC driver file.

  6. In the Database box, enter the name of your database. In the Server box, enter the name of your database server. Consult your database administrator for the name of your database server. In the Port Number box, enter the port number.

  7. Under Connection Options, in the Name column, enter the name of an additional driver-specific option. Then, in the Value column, enter the value of the driver-specific option. Click to specify additional driver-specific options.

  8. Click . The Test Connection dialog box opens. Enter the user name and password for your database, or leave these boxes blank if your database does not require them. Click .

    If your connection succeeds, the Database Explorer dialog box displays a message indicating the connection is successful. Otherwise, it displays an error message.

  9. Click . The JDBC Data Source Configuration dialog box displays a message indicating the data source is saved successfully. Close this dialog box.

Set Up Data Source Using Command Line

  1. Create a JDBC data source for an SQL Server database.

  2. Set the JDBC connection options. For example, this code assumes that you are connecting to a JDBC data source named MSSQLServer, database server dbtb04, port number 54317, and full path of the JDBC driver file /home/user/DB_Drivers/sqljdbc4.jar.

    To add JDBC driver-specific connection options, use the addConnectionOptions function.

  3. Test the database connection by specifying the user name username and password pwd, or leave these arguments blank if your database does not require them.

  4. Save the JDBC data source.

After you complete the data source setup, connect to the SQL Server database using the Database Explorer app or the JDBC driver and command line.

Step 3. Connect using the Database Explorer app or the command line.

Connect to SQL Server Using Database Explorer App

  1. On the Database Explorer tab, in the Connections section, click and select the data source for the connection.

  2. In the connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. Click .

    The Catalog and Schema dialog box opens.

  3. Select the catalog and schema from the Catalog and Schema lists. Click .

    The app connects to the database and displays its tables in the Data Browser pane. A data source tab appears to the right of the pane. The title of the data source tab is the data source name that you defined during the setup. The data source tab contains empty SQL Query and Data Preview panes.

  4. Select tables in the Data Browser pane to query the database.

  5. Close the data source tab to close the SQL query. In the Connections section, close the database connection by clicking .

    Note

    If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from the list.

Connect to SQL Server Using JDBC Driver and Command Line

  1. Connect to an SQL Server database using the configured JDBC data source, user name username, and password pwd. For example, this code assumes that you are connecting to a JDBC data source named MSSQLServer.

  2. Close the database connection.

See Also

Apps

Functions

  • closeconfigureJDBCDataSourcedatabasesaveAsJDBCDataSourcesetConnectionOptionstestConnection

Related Topics

Coherence provides native, entity-based implementations of the CacheStore and CacheLoader interfaces that use the Java Persistence API (JPA) to load and store objects to a database. Before using JPA with Coherence, you should be familiar with the CacheStore and CacheLoader interfaces. These interfaces are used to cache data sources. See Caching Data Sources.

Note:

Only resource-local and bootstrapped entity managers can be used with Coherence and JPA. Container-managed entity managers and those that use Java Transaction Architecture (JTA) transactions are not currently supported.

This chapter includes the following sections:

  • Overview of the JPA CacheStore and CacheLoader Implementations
    Oracle Coherence provides two implementations of the CacheStore and CacheLoader interfaces which can be used with JPA: a generic JPA implementation and an EclipseLink-specific implementation.
  • Obtain a JPA Provider Implementation
    A JPA provider allows you to work directly with Java objects, rather then with SQL statements. You map, store, update and retrieve data, and the provider performs the translation between database entities and Java objects.
  • Configure a Coherence JPA Cache Store
    Using JPA with Coherence requires configuring persistence properties and defining a cache that uses the JpaCacheStore implementation.

Overview of the JPA CacheStore and CacheLoader Implementations

Oracle Coherence provides two implementations of the CacheStore and CacheLoader interfaces which can be used with JPA: a generic JPA implementation and an EclipseLink-specific implementation. For both implementations, the entities must be mapped to the data store and a JPA persistence unit configuration must exist. A JPA persistence unit is defined as a logical grouping of user-defined entity classes that can be persisted and their settings. The JPA run-time configuration file, persistence.xml, and the default JPA Object-Relational mapping file, orm.xml, are typically provided as part of a JPA solution.

Table 1-1 describes the JPA implementations provided by Coherence.

Table 1-1 JPA-Related CacheStore and CacheLoader API Included with Coherence Download from box.net in chrome for mac.

Class NameLocationDescription

JpaCacheStore

COHERENCE_HOMElibcoherence-jpa.jar

A JPA implementation of the Coherence CacheStore interface. Use this class as a full load and store implementation. It can use any JPA implementation to load and store entities to and from a data store.

Note: The persistence unit is assumed to be set to use RESOURCE_LOCAL transactions.

JpaCacheLoader

A JPA implementation of the Coherence CacheLoader interface. Use this class as a load-only implementation. It can use any JPA implementation to load entities from a data store.

Use the JpaCacheStore class for a full load and store implementation.

EclipseLinkJPACacheStoreORACLE_HOMEoracle_commonmodulesoracle.toplink_versiontoplink-grid.jar

An EclipseLink specific JPA implementation of the Coherence CacheStore interface. This implementation is intended to be used where the application uses Coherence directly and the cache store and loader is used behind the scene to persist and load data.

Note: To use this implementation, make sure no cache interceptors or query redirectors from the EclipseLink-Coherence integration are set within the persistence unit for the specific class.

EclipseLinkJPACacheLoader

An EclipseLink specific JPA implementation of the Coherence CacheLoader interface.

Note: To use this implementation, make sure no cache interceptors or query redirectors from the EclipseLink-Coherence integration are set within the persistence unit for the specific class.

Obtain a JPA Provider Implementation

A JPA provider allows you to work directly with Java objects, rather then with SQL statements. You map, store, update and retrieve data, and the provider performs the translation between database entities and Java objects.

Oracle recommends using EclipseLink JPA– the reference implementation for the JPA 2.0 specification and also the JPA provider used in Oracle TopLink. EclipseLink provides a high-performance JPA implementation with many advanced features for caching, threading, and overall performance.

The EclipseLink JAR files (eclipselink.jar) is included in the Coherence installation and can be found in the ORACLE_HOMEoracle_commonmodulesoracle.toplink_version folder.

Configure a Coherence JPA Cache Store

Using JPA with Coherence requires configuring persistence properties and defining a cache that uses the JpaCacheStore implementation.

This section includes the following topics:

  • Map the Persistent Classes
  • Configure JPA
  • Configure a Coherence Cache for JPA
  • Configure the Persistence Unit

Map the Persistent Classes

Map the entity classes to the database. This will allow you to load and store objects through the JPA cache store. JPA mappings are standard, and can be specified in the same way for all JPA providers.

You can map entities either by annotating the entity classes or by adding an orm.xml or other XML mapping file. See the JPA provider documentation for more information about how to map JPA entities.

Parent topic:Configure a Coherence JPA Cache Store

Configure JPA

Edit the persistence.xml file to create the JPA configuration. This file contains the properties that dictate run-time operation.

Set the transaction type to RESOURCE_LOCAL and provide the required JDBC properties for your JPA provider (such as driver, url, user, and password) with the appropriate values for connecting and logging into your database. List the classes that are mapped using JPA annotations in <class> elements. Example 1-1 illustrates a sample persistence.xml file with the typical properties that you can set.

Parent topic:Configure a Coherence JPA Cache Store

Configure a Coherence Cache for JPA

Create a coherence-cache-config.xml file to override the default Coherence settings and define a caching scheme. The caching scheme includes a <cachestore-scheme> element that lists the JPA implementation class and includes the following parameters.

  • The entity name of the entity being stored. Unless it is explicitly overridden in JPA, this is the unqualified name of the entity class. Example 1-2 uses the built-in Coherence macro {cache-name} that translates to the name of the cache that is constructing and using the cache store. This works because a separate cache must be used for each type of persistent entity and Coherence ensures that the name of each cache is set to the name of the entity that is being stored in it.

  • The fully qualified name of the entity class. If the classes are all in the same package and use the default JPA entity names, then you can again use the {cache-name} macro for the part that is variable across the different entity types. In this way, the same caching scheme can be used for all of the entities that are cached within the same persistence unit.

  • The persistence unit name. This should be the same as the name specified in the persistence.xml file.

The various named caches are then directed to use the JPA caching scheme. Example 1-2 is a sample coherence-cache-config.xml file that defines a cache named Employee that caches instances of the Employee class. The cache is configured to use the JpaCacheStore implementation. To define additional entity caches for more classes, add more <cache-mapping> elements to the file.

Example 1-2 Assigning Named Caches to a JPA Caching Scheme

Parent topic:Configure a Coherence JPA Cache Store

Configure the Persistence Unit

When using a JPA cache store or loader implementation, configure the persistence unit to ensure that no changes are made to entities when they are inserted or updated. Any changes made to entities by the JPA provider are not reflected in the Coherence cache. This means that the entity in the cache will not match the database contents. In particular, entities should not use ID generation, for example, @GeneratedValue, to obtain an ID. IDs should be assigned in application code before an object is put into Coherence. The ID is typically the key under which the entity is stored in Coherence.

Optimistic locking (for example, @Version) should not be used because it might lead to the failure of a database transaction commit transaction.

Drivers

When using a JPA cache store or loader implementation, L2 (shared) caching should be disabled in your persistence unit. See the documentation for your provider. In EclipseLink, this can be specified on an individual entity with @Cache(shared=false) or as the default in the persistence.xml file with the following property:

Parent topic:Configure a Coherence JPA Cache Store