Skip to Main Content Skip to Search
Product Documentation

Configuring Your Environment

About Data Sources and Database Drivers

Before you can use the Database Toolbox software to connect to a database, you must set up a data source. A data source consists of:

Data sources interact with ODBC drivers or JDBC drivers. An ODBC driver is a standard Microsoft® Windows® interface that enables communication between database management systems and SQL-based applications. A JDBC driver is a standard interface that enables communication between applications based on Sun™ Java™ and database management systems.

The Database Toolbox software is based on Java. It uses a JDBC/ODBC bridge to connect to the ODBC driver of a database, which is automatically installed as part of the MATLAB JVM™.

The following figure illustrates how drivers interact with the Database Toolbox software.

Before You Begin

Before you can use the Database Toolbox software with the examples provided in this documentation, do the following:

  1. Set up the data sources that are provided with the Database Toolbox product. For instructions, see Preparing Examples.

  2. Configure the data sources for use with your database driver.

Preparing Examples

Setting Up the dbtoolboxdemo Data Source

The dbtoolboxdemo data source uses the tutorial database located in matlabroot/toolbox/database/dbdemos/tutorial.mdb.

To set up this data source:

  1. Copy tutorial.mdb into a folder to which you have write access.

  2. Confirm you have write access to tutorial.mdb.

  3. Open tutorial.mdb from within the MATLAB Current Folder browser by right-clicking the file and selecting Open Outside MATLAB. The file opens in Microsoft® Access™.

      Note   You might need to convert the database to the version of Microsoft Access you are currently running. For example, beginning in Microsoft Access 2007, you will see the option to save as *.accdb. For more information, consult your database administrator.

Setting Up the SampleDB Data Source

The SampleDB data source uses a Microsoft Access sample database called Nwind. If you do not have the Nwind database on your system, download it from the Microsoft downloads page at http://www.microsoft.com/downloads/. The version referred to in this documentation is the Northwind Traders sample database, Nwind.exe.

To set up this data source:

  1. Run Nwind.exe to create the Nwind.mdb database.

  2. Confirm you have write access to Nwind.mdb.

  3. Open Nwind.mdb from within the MATLAB Current Folder browser by right-clicking the file and selecting Open Outside MATLAB.

      Note   When using Microsoft Access 2007 or later, the file might convert to Nwind.accdb.

  4. Create a table in the Nwind database into which to export MATLAB data:

    1. Open the Nwind database in the Microsoft Access application.

    2. Create a new table and save it as Avg_Freight_Cost.

        Note   The Microsoft Access database warns you that there is no primary key, but you do not need one. If you choose to designate a primary key, you can run the example as written only once. The Microsoft Access application prevents you from inserting the same record into the database twice.

    3. Add two fields, Calc_Date and Avg_Cost.

    4. Use the default type, which is Text, for the Calc_Date Data Type field.

    5. Set the Data Type of the Avg_Cost field to Number.

    6. Save the table and close it.

Setting Up ODBC Data Sources

This section describes how to set up a data source whose database is located on a PC running the Windows operating system, or on another system to which the PC is networked, for use with an ODBC driver. These instructions use the Microsoft ODBC Data Source Administrator Version 6.1 for the U.S. English version of Microsoft Access 2010 for Windows systems. If you have a different configuration, you might need to modify these instructions. For more information, consult your database administrator.

  1. Close open databases, including tutorial.mdb and Nwind.mdb (or Nwind.accdb) in the database program.

  2. Open the Windows Data Source Administrator dialog box to define the ODBC data source:

    1. Start Visual Query Builder by entering the command querybuilder at the MATLAB command prompt.

        Requirement   When using a 32-bit version of Microsoft Office, you must also use a 32-bit version of MATLAB to complete the remaining steps.

    2. In Visual Query Builder, select Query > Define ODBC Data Source.

    The ODBC Data Source Administrator dialog box appears, listing existing data sources.

  3. Click the User DSN tab.

  4. Select MS Access Database and click Add.

    A list of installed ODBC drivers appears in the ODBC Data Source Administrator dialog box.

  5. Select Microsoft Access Driver (*.mdb, *.accdb) and click Finish.

    The ODBC Microsoft Access Setup dialog box for your driver appears. The dialog box for your driver might look different from the following.

  6. Enter dbtoolboxdemo as the Data Source Name.

  7. Enter tutorial database as the Description.

  8. Select the database for this data source to use. For some drivers, you can skip this step. If you are not sure whether you can skip this step, consult your database administrator.

    1. In the ODBC Microsoft Access Setup dialog box, click Select.

      The Select Database dialog box appears.

    2. Specify the database you want to use. For the dbtoolboxdemo data source, select tutorial.mdb.

    3. If your database is on a system to which your PC is connected:

      1. Click Network. The Map Network Drive dialog box appears.

      2. Specify the folder containing the database you want to use.

      3. Click Finish.

    4. Click OK to close the Select Database dialog box.

  9. In the ODBC Microsoft Access Setup dialog box, click OK.

  10. Repeat steps 6 through 9 with the following changes to define the data source for the other sample database, Nwind:

    1. In step 6, enter SampleDB as the Data Source Name.

    2. In step 7, enter Northwind database as the Description.

    3. In step 8, select Nwind.mdb or Nwind.accdb and then click OK.

  11. The ODBC Data Source Administrator dialog box now displays the dbtoolboxdemo and SampleDB data sources in the User DSN tab.

  12. Click OK to close the dialog box.

Setting Up JDBC Data Sources

  1. Find the name of the file that contains the JDBC drivers. This file is provided by your database vendor. The name and location of this file differs for each system. If you do not know the name or location of this file, consult your database administrator.

  2. Specify the location of the drivers file in the MATLAB Sun Java classpath by adding this file's path to the matlabroot/toolbox/local/classpath.txt file.

    For example, use the following syntax to add a reference to a MySQL® JDBC drivers file, D:/mysql/mysql-connector-java-3.0-bin.jar, to classpath.txt:

    D:/mysql/mysql-connector-java-3.0-bin.jar

    Restart the MATLAB session before you access the database.

    If the drivers file is not located where classpath.txt indicates, errors do not appear, but MATLAB does not establish a database connection. Make sure that you update classpath.txt with the correct location and file name for your drivers file if the path and/or name of this file changes. If a MATLAB session is running while you modify classpath.txt, be sure to restart it before you connect to the database.

  3. To set up JDBC drivers for use with Visual Query Builder:

    1. Run the querybuilder command in the MATLAB Command Window to start Visual Query Builder.

    2. Select Query > Define JDBC Data Source.

      The Define JDBC data sources dialog box appears.

      The following table describes the fields that you use to define your JDBC data source. For examples of syntax used in these fields, see ??? on the database function reference page.

      Field NameDescription
      Name

      The name you assign to the data source. For some databases, the Name must match the name of the database as recognized by the machine it runs on.

      Driver

      The JDBC driver name (sometimes referred to as the class that implements the Java SQL driver for your database).

      URL

      The JDBC URL object, of the form jdbc:subprotocol:subname.

      subprotocol is a database type; for example, the Oracle® Corporation Oracle database.

      subname may contain other information used by Driver, such as the location of the database and/or a port number. It may take the form //hostname:port/databasename.

        Note   Your driver manufacturer's documentation specifies the Driver and URL formats. You may need to consult your database system administrator for this information.

    3. In the Define JDBC data sources dialog box, click Create new file.

    4. The Specify new JDBC data source MAT-file dialog box appears. Use this dialog box to create a MAT-file that saves your specified data source information for future Visual Query Builder sessions.

      Navigate to the folder where you want to put the MAT-file, specify a name for it that includes a .mat extension, and click Save.

    5. In the Define JDBC data sources dialog box, enter information for your JDBC data source into the Name, Driver, and URL fields as described in the table in step 3b.

    6. Test the connection by clicking Test.

      If your database requires a user name and password, a dialog box prompting you to supply them appears. Enter values into these fields and click OK.

      A confirmation dialog box states that the database connection was successful.

    7. In the Define JDBC data sources dialog box, click Add/Update. The data source now appears in the Data source list.

    8. To add more data sources, repeat steps c through g for each new data source. You can add more data sources later by editing the MAT-file that contains the data source information.

        Note   You can create another MAT-file to add new data sources, but Visual Query Builder can only access data sources from one MAT-file at a time. Include multiple data sources in a single MAT-file for easy access.

    9. Click OK to close the Define JDBC data sources dialog box.

      The newly added data source now appears in the VQB Data source list.

      The defined JDBC data source is only valid for the current MATLAB session. To access the data source you defined in a future MATLAB session, follow the instructions in Accessing Existing JDBC Data Sources in the Database Toolbox User's Guide documentation.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS