MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

4
CONTENTS MySQL JDBC for Windows This tutorial shows how to set up a data source and connect to your MySQL database. This tutorial uses the MySQL Connector/J 5.1.17 driver to connect to the MySQL Version 5.5.16 database. Step 1. Verify the driver installation. If the JDBC driver for MySQL 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. Add the JDBC driver to the MATLAB static Java class path. 1. Run the prefdir command in the Command Window. The output of this command is a file path to a folder on your computer. 2. Close MATLAB if it is running. 3. Navigate to the folder and create a file called javaclasspath.txt in the folder. 4. Open javaclasspath.txt. Add the full path to the database driver JAR file in javaclasspath.txt. The full path includes the path to the folder where you downloaded the JAR file from the database provider and the JAR file name. For example, C:\DB_Drivers\mysql‐connector‐java‐5.1.17‐bin.jar. Save and close javaclasspath.txt. 5. Restart MATLAB. Alternatively, you can use javaaddpath to add your JDBC driver to the dynamic Java class path. For details about static and dynamic class paths, see Bringing Java Classes into MATLAB Workspace. Step 3. Set up the data source using Database Explorer. This step is required only for connecting to Database Explorer. If you want to use the command line to connect to your database, see Connect to MySQL using the JDBC connection command line. 1. Open Database Explorer by clicking the Apps tab on the MATLAB Toolstrip. Then, select Database Explorer from the Database Connectivity and Reporting section in the apps gallery. Alternatively, enter dexplore at the command line. If no data sources are set up, a message box opens. Click OK to close it. Otherwise, the Connect to a Data Source dialog box opens. Click Cancel to close this dialog box. 2. Click the Database Explorer tab, and then select New > JDBC. The Create a New JDBC data source dialog box opens. ® ® ®

description

ng

Transcript of MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

Page 1: MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

5/13/2015 MySQL JDBC for Windows ­ MATLAB & Simulink ­ MathWorks United Kingdom

http://uk.mathworks.com/help/database/ug/mysql­jdbc­windows.html 1/4

CONTENTSMySQL JDBC for Windows

This tutorial shows how to set up a data source and connect to your MySQL  database. This tutorial uses the MySQLConnector/J 5.1.17 driver to connect to the MySQL Version 5.5.16 database.

Step 1. Verify the driver installation.If the JDBC driver for MySQL is not installed on your computer, find the link on the Driver Installation page to install thedriver. Follow the instructions to download and install this driver on your computer.

Step 2. Add the JDBC driver to the MATLAB static Java class path.1.  Run the prefdir command in the Command Window. The output of this command is a file path to a folder on your

computer.

2.  Close MATLAB  if it is running.

3.  Navigate to the folder and create a file called javaclasspath.txt in the folder.

4.  Open javaclasspath.txt. Add the full path to the database driver JAR file in javaclasspath.txt. The full pathincludes the path to the folder where you downloaded the JAR file from the database provider and the JAR filename. For example, C:\DB_Drivers\mysql‐connector‐java‐5.1.17‐bin.jar. Save and closejavaclasspath.txt.

5.  Restart MATLAB.

Alternatively, you can use javaaddpath to add your JDBC driver to the dynamic Java  class path. For details aboutstatic and dynamic class paths, see Bringing Java Classes into MATLAB Workspace.

Step 3. Set up the data source using Database Explorer.This step is required only for connecting to Database Explorer. If you want to use the command line to connect to yourdatabase, see Connect to MySQL using the JDBC connection command line.

1.  Open Database Explorer by clicking the Apps tab on the MATLAB Toolstrip. Then, select Database Explorer fromthe Database Connectivity and Reporting section in the apps gallery. Alternatively, enter dexplore at thecommand line. If no data sources are set up, a message box opens. Click OK to close it. Otherwise, the Connect toa Data Source dialog box opens. Click Cancel to close this dialog box.

2.  Click the Database Explorer tab, and then select New > JDBC.

The Create a New JDBC data source dialog box opens.

®

®

®

Page 2: MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

5/13/2015 MySQL JDBC for Windows ­ MATLAB & Simulink ­ MathWorks United Kingdom

http://uk.mathworks.com/help/database/ug/mysql­jdbc­windows.html 2/4

3.  Select MYSQL from the Vendor list. After selecting the vendor, if you did not add the JDBC driver file path to the Javaclass path, this dialog box displays this message at the bottom. Address this message by following the stepsdescribed in Step 2.

4.  Enter the database server name in the Server Name field, port number in the Port Number field, user name in theUsername field, password in the Password field, and database name in the Database field.

5.  Click Test to test the connection. If your connection succeeded, Database Explorer displays ConnectionSuccessful!

6.  Enter a data source name in the Data Source Name field in the Create a New JDBC data source dialog box. Usea new data source name that does not appear in the existing list of data source names. Click Save. The new JDBCdata source appears in the list of data sources in the Connect to a Data Source dialog box.

7.  If this time is the first time that you are creating a data source using Database Explorer, the New file to store JDBCconnection parameters dialog box opens. Use this dialog box to create a MAT­file that saves your specified datasource information for future Database Explorer sessions. This MAT­file name is stored insetdbprefs('JDBCDataSourceFile') and is valid for all MATLAB sessions.

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

After you complete the data source setup, connect to the MySQL database using Database Explorer or thecommand line with the JDBC connection.

Step 4. Connect using Database Explorer or the command line.

Page 3: MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

5/13/2015 MySQL JDBC for Windows ­ MATLAB & Simulink ­ MathWorks United Kingdom

http://uk.mathworks.com/help/database/ug/mysql­jdbc­windows.html 3/4

Connect to MySQL using Database Explorer.

1.  After setting up the data source, connect to your database by selecting the data source name for the MySQLdatabase from the Data Sources list. Enter a user name and password. Click Connect.

Database Explorer connects to your database and displays its contents in a tab named with the data source name.

2.  Close the connection using Database Explorer by hovering the cursor over the Close button (  ) next to theMySQL JDBC data source name on the database tab. The Close button turns into a red circle (  ). Click it toclose the database connection. If you want to close Database Explorer and all database connections, click theClose button (  ) in the top­right corner.

If Database Explorer is docked, click the Close button (  ) to close all database connections and DatabaseExplorer.

Page 4: MySQL JDBC for Windows - MATLAB & Simulink - MathWorks United Kingdom

5/13/2015 MySQL JDBC for Windows ­ MATLAB & Simulink ­ MathWorks United Kingdom

http://uk.mathworks.com/help/database/ug/mysql­jdbc­windows.html 4/4

Connect to MySQL using the JDBC connection command line.

1.  Use the Vendor name­value pair argument of database to specify a connection to a MySQL database. Forexample, the following code assumes you are connecting to a database named dbname on a database servernamed sname with user name username and password pwd.

conn = database('dbname','username','pwd',...                'Vendor','MySQL',...                'Server','sname');

2.  Close the database connection conn.

close(conn)

See Alsoclose | database | javaaddpath

More AboutWorking with Database Explorer

Bringing Java Classes into MATLAB Workspace