The following describes how to setup JDBC database connections to Microsoft SQL Server, Oracle and MySQL. MATLAB code for a simple test and other related information can be found further below.
________________________________________
Microsoft SQL Server
________________________________________
1. Download the latest JDBC driver for your database:
2. Refer to the documentation for information on how to set up the driver and create a database connection object.
________________________________________
ORACLE
________________________________________
1. Download the latest JDBC driver for your database:
2. Open the file "$matlabroot\toolbox\local\classpath.txt" in the MATLAB Editor by executing the following command:
3. Scroll down to the end of the file and add the full path to the database driver file, for example as follows:
C:\Drivers\Oracle\ojdbc6.jar
4. Save the file and restart MATLAB.
5. Open the database connection as follows:
conn = database('databasename','scott','tiger','oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@111.222.333.444:1521:');
________________________________________
MySQL
________________________________________
1. Download the latest JDBC driver (Connector/J):
2. Open the file "$matlabroot\toolbox\local\classpath.txt" in the MATLAB Editor by executing the following command:
Note: Alternatively, you can also use the function JAVAADDPATH to add the driver to the dynamic Sun Java class path.
3. Scroll down to the end of the file and add the full path to the database driver file, for example as follows:
C:\Drivers\MySQL\mysql-connector-java-5.1.12-bin.jar
4. Save the file and restart MATLAB.
5. Open the database connection as follows:
url = 'jdbc:mysql://localhost/test';
conn = database('databasename', 'username', 'password', 'com.mysql.jdbc.Driver', url);
________________________________________
Simple test
________________________________________
After opening the connection as explained above, you can run a simple test to retrieve some data as follows:
ping(conn)
if isconnection(conn)
curs = exec(conn,'SELECT * FROM Table1');
curs = fetch(curs);
data = curs.data;
update(conn,'Table1',{'Field1'},{1; 2; 3},{'WHERE Field2=1'; 'WHERE Field2=2'; 'WHERE Field2=3'});
update(conn,'Table1',{'Field1'},{1; 2; 3},{'WHERE Field3=''aaa'''; 'WHERE Field3=''bbb'''; 'WHERE Field3=''ccc'''});
exec(conn,'INSERT INTO Table1(Field1, Field2, Field3) VALUES(4, 4, ''ddd'')');
exec(conn,'DELETE FROM Table1 WHERE Field3=''ddd''');
fastinsert(conn,'Table1',{'Field1', 'Field2', 'Field3'}, {1, 1, 'aaa'; 2, 2, 'bbb'; 3, 3, 'ccc'});
close(conn);
end
________________________________________
Related information
________________________________________
About configuring Driver and Data Sources, please refer to the below documentation:
- Run this command in MATLAB 2017a for documentation:
>> web(fullfile(docroot, 'database/ug/configuring-driver-and-data-source.html'))
- Or for latest release documentation on configure driver and data source please refer to:
For information on working with Visual Query Builder, please run the following command in MATLAB 2016b for documentation:
>> web(fullfile(docroot, 'database/ug/getting-started-with-visual-query-builder.html'))
(Please note Visual Query Builder have been removed since R2017a)
Please refer below documentation for more info on database.