In this article, We will show you how to set up a JDBC database that connects to 1. Microsoft SQL server, 2. Oracle, 3. My SQL
1. Microsoft SQL servers
1-1) Download the latest JDBC driver that is appropriate for your database.
1-2) See the documentation of the driver for setting up the driver and creating database connection objects.
2. ORACLE
2-1) Download the latest JDBC driver for your database.
https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html
2-2) Run the following command to open $MATLAB\toolbox\local\classpath.txt in the MATLAB editor.
2-3) Add the full path of the database driver file to the last line.
Example:
C:\Drivers\Oracle\ojdbc6.jar
2-4) Save the file and restart MATLAB.
2-5) Run the following command to connect to the database.
conn = database('databasename','scott','tiger','oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@111.222.333.444:1521:');
3-1) Download the latest JDBC driver (Conenector/J) below.
3-2) Run the following command to open the $MATLAB\toolbox\local\classpath.txt in the MATLAB editor.
* If you want to add a dynamic Sun Java class path to the driver, you can use the JAVAADDPATH function.
3-3) Add the full path of the database driver file to the last line.
Example:
C:\Drivers\MySQL\mysql-connector-java-5.1.12-bin.jar
3-4) Save the file and restart MATLAB.
3-5) Run the following command to connect to the database.
url = 'jdbc:mysql://localhost/test';
conn = database('databasename', 'username', 'password', 'com.mysql.jdbc.Driver', url);
After setting the connection, you can perform connection test by the following script.
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
Setting Up Data Sources for Use with JDBC Drivers
https://www.mathworks.com/help/releases/R2017a/database/ug/configuring-driver-and-data-source.html
database
http://www.mathworks.com/access/helpdesk/help/toolbox/database/ug/database.html