MySQL JDBC for Linux
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.Run the prefdir command in the
Command Window. The output is a file path to a folder on your computer.
Close MATLAB® if it is running.
Navigate to the folder and create a file called javaclasspath.txt in
the folder.
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, /home/user/DB_Drivers/mysql-connector-java-5.1.17-bin.jar.
Save and close javaclasspath.txt.
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 Java Class Path (MATLAB).
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 JDBC Driver and Command LineOpen 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.
Click the Database Explorer tab,
and then select New > JDBC.
The Create a New JDBC data source dialog box opens.
Select MYSQL from the Vendor list.
After selecting the vendor, if you did not add the JDBC driver file
path to the Java class path, this dialog box displays this message
at the bottom. Address this message by following the steps described
in Step 2.
Enter the database server name in the Server
Name field, port number in the Port Number field,
user name in the Username field, password in
the Password field, and database name in the Database field.
Click Test to test the connection.
If your connection succeeded, Database Explorer displays Connection
Successful!
Enter a data source name in the Data Source
Name field in the Create a New JDBC data source dialog
box. Use a new data source name that does not appear in the existing
list of data source names. Click Save. The
new JDBC data source appears in the list of data sources in the Connect
to a Data Source dialog box.
If this time is the first time that you are creating
a data source using Database Explorer, the New file to store JDBC
connection parameters dialog box opens. Use this dialog box to create
a MAT-file that saves your specified data source information for future
Database Explorer sessions. This MAT-file name is stored in setdbprefs('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, and
click Save.
After you complete the data source setup, connect to the MySQL database
using Database Explorer or the command line with the JDBC connection.
Step 4. Connect using Database Explorer or the command line.
Connect to MySQL using Database Explorer.After setting up the data source, connect to your
database by selecting the data source name for the MySQL database
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.
Close the connection using Database Explorer by hovering
the cursor over the Close button () next to the MySQL data
source name on the database tab. The Close button
turns into a red circle (). Click it to close the
database connection. If you want to close Database Explorer and all
database connections, click the Close button
() in the top-right corner.
If Database Explorer is docked, click the Close button
() to close all database connections
and Database Explorer.
Connect to MySQL Using JDBC Driver and Command Line
When using the command line, you do not have to set up a data
source with Database Explorer. You can use the command line to pass
all the required parameters for connection.Use the Vendor name-value pair
argument of database to specify a connection to
a MySQL database. For example, the following code assumes you
are connecting to a database named dbname on a
database server named sname with user name username and
password pwd.
conn = database('dbname','username','pwd', ...
'Vendor','MySQL', ...
'Server','sname');
Close the database connection.
close(conn)
See Also
Related Topics