| Contents | Index |
| On this page… |
|---|
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 that the toolbox accesses
Information required to find the data, such as driver, folder, server, or network names
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.

Tip Some Windows systems support both ODBC and JDBC drivers. On such systems, JDBC drivers generally provide better performance than ODBC drivers because the ODBC/JDBC bridge is not used to access databases. |
Before you can use the Database Toolbox software with the examples provided in this documentation, do the following:
Set up the data sources that are provided with the Database Toolbox product. For instructions, see Preparing Examples.
Configure the data sources for use with your database driver.
If you are using an ODBC driver, see Setting Up ODBC Data Sources.
If you are using a JDBC driver, see Setting Up JDBC Data Sources.
The dbtoolboxdemo data source uses the tutorial database located in matlabroot/toolbox/database/dbdemos/tutorial.mdb.
To set up this data source:
Copy tutorial.mdb into a folder to which you have write access.
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.
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:
Run Nwind.exe to create the Nwind.mdb database.
Open Nwind.mdb from within the MATLAB Current Folder browser by right-clicking the file and selecting Open Outside MATLAB.
Create a table in the Nwind database into which to export MATLAB data:
Open the Nwind database in the Access application.
Create a new table and save it as Avg_Freight_Cost.
Add two fields, Calc_Date and Avg_Cost.
Use the default type, which is Text, for the Calc_Date Data Type field.

Set the Data Type of the Avg_Cost field to Number.
Save the table and close it.
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.
Close open databases, including tutorial.mdb and Nwind.mdb (or Nwind.accdb) in the database program.
Open the Windows Data Source Administrator dialog box to define the ODBC data source:
Start Visual Query Builder by entering the command querybuilder at the MATLAB command prompt.
In Visual Query Builder, select Query > Define ODBC Data Source.

The ODBC Data Source Administrator dialog box appears, listing existing data sources.
Select MS Access Database and click Add.

A list of installed ODBC drivers appears in the ODBC Data Source Administrator dialog box.
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.

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.
In the ODBC Microsoft Access Setup dialog box, click Select.
The Select Database dialog box appears.

Specify the database you want to use. For the dbtoolboxdemo data source, select tutorial.mdb.
If your database is on a system to which your PC is connected:
Click Network. The Map Network Drive dialog box appears.
Specify the folder containing the database you want to use.
Click Finish.
Click OK to close the Select Database dialog box.
Repeat steps 6 through 9 with the following changes to define the data source for the other sample database, Nwind:
The ODBC Data Source Administrator dialog box now displays the dbtoolboxdemo and SampleDB data sources in the User DSN tab.

Click OK to close the dialog box.
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.
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.
To set up JDBC drivers for use with Visual Query Builder:
Run the querybuilder command in the MATLAB Command Window to start Visual Query Builder.
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 JDBC Driver Name and Database URL on the database function reference page.
| Field Name | Description |
|---|---|
| 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. |
In the Define JDBC data sources dialog box, click Create new file.
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.
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.
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.
In the Define JDBC data sources dialog box, click Add/Update. The data source now appears in the Data source list.
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.
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.
![]() | Product Prerequisites | Starting the Database Toolbox and Visual Query Builder Software | ![]() |

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 |