Posted by
admin on Jul 9, 2012 in
matlab |
1 comment
Connecting to MySQL database from Matlab is not that hard (as I thought before). Matlab apparently use JDBC to connect to database server.
So, here’s simple steps to connect to MySQL server:
- Open the zip/tar.gz file and extract only the .jar file on your Matlab project directory.

Only extract that .jar file
- Now you are ready to use your MySQL driver inside Matlab.
- Open Matlab and set your “Current Folder” to where you extract that MySQL Connector/J driver.

matlab current folder
- For this tutorial, I’ll use cdcols database from XAMPP sample MySQL database. (database name=cdcols, mysql username=root and mysql password is empty).
- Load MySQL driver (as I’m writing this, the current driver version is: 5.1.21)
javaaddpath 'mysql-connector-java-5.1.21-bin.jar'; |
conn=database('cdcol', 'root', '', 'com.mysql.jdbc.Driver', 'jdbc:mysql://localhost/'); |
- We will access cds table, here’s the table structure (taken from open sourced HeidiSQL)

cds table structure
- SELECT Query on cds table
e = exec(conn,'SELECT * FROM cds'); |
- fetch 2 rows from the resultset/recordset. The return value would be a cell of matrices
- accessing the record value
% second row, first column ('titel' field)
dat.Data{2,1} |
- INSERT query on cds table (you can use the same technique on UPDATE and DELETE query)
r=exec(conn, 'insert into cds(`titel`, `interpret`, `jahr`) values(''Kisah 2002 Malam'', ''Peterpan'', 2002)'); |
- table cds after above INSERT query (screenshot also from HeidiSQL):

data after INSERT query
- Closing database connection
That’s it. It’s not that hard isn’t it?
Related Posts via Taxonomies
Thank you very much. It was very helpful