This post was originally published on April 13th, 2008.  It was imported from a previous version of the site.

I normally don’t delve too much into the programmatic details of my work, but I’ve seen enough interest in this topic that I figured I’d lend a helping hand to those fellow frustrated souls.

There are a few awkward, non-native MEX implementations of various database interfaces.  However, Matlab has its own database toolbox built around ODBC/JDBC, and when developing distributable software, one always hopes to minimize third-party library usage.  As a result, I’ve put a good deal of effort into integrating Matlab with both MySQL and SQLite.  In fact, the data for every post on this site is stored in a 2GB MySQL database server running on my laptop.

As you can see, this requires only that you distribute the platform-independent JAR.  No DLLs, no MEX compilation.

% Database Server
host = 'localhost';

% Database Username/Password
user = 'user'; password = 'password';
% Database Name
dbName = 'assets'; 
% JDBC Parameters jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName); jdbcDriver = 'com.mysql.jdbc.Driver';
% Set this to the path to your MySQL Connector/J JAR
javaaddpath('mysql-connector-java-5.1.6-bin.jar')
% Create the database connection object dbConn = database(dbName, user , password, jdbcDriver, jdbcString);
% Check to make sure that we successfully connected if isconnection(dbConn)     % Fetch the symbol, market cap, and last close for the 10 largest % market cap ETFs     result = get(fetch(exec(dbConn, 'SELECT info.symbol,info.marketcap,series.close FROM info, series WHERE info.type=''ETF'' AND info.id = series.symbolid AND series.date = ''2008-04-11'' ORDER BY marketcap DESC LIMIT 10')), 'Data');     disp(result);
% If the connection failed, print the error message else     disp(sprintf('Connection failed: %s', dbConn.Message)); end % Close the connection so we don't run out of MySQL threads close(dbConn);
Output:
    ‘SPY’     [8.2300e+010]    [133.3800]
    ‘EFA’     [4.5420e+010]    [ 72.6400]
    ‘EEM’     [2.3850e+010]    [139.3400]
    ‘GLD’     [1.9260e+010]    [ 91.3000]
    ‘QQQQ’    [1.7040e+010]    [ 44.2800]
    ‘IVV’     [1.6410e+010]    [133.5200]
    ‘IWF’     [1.2860e+010]    [ 55.2500]
    ‘DIA’     [1.0830e+010]    [123.3400]
    ‘IWM’     [1.0470e+010]    [ 68.7200]
    ‘VTI’     [9.7800e+009]    [132.2900]

5 Responses to “Using Matlab’s Database Toolbox with MySQL Connector/J”

  1. Akangbe Saidat says:

    Hi, I am a final year student of computer engineering doing my project on biometrics-fingerprint. I read somewhere that MySQL would need an ODBC driver. Is that possible and please can you send me any powerpoint /visual tutorials on using MATLAB with database? Thanks so much. Would like a reply ASAP

    • Hi Akangbe,
      To my knowledge, there are no other tutorials on this topic but this is certainly possible. I put together this post only based on experience and the Matlab documentation. Please review the code in the post and refer to the MySQL and Matlab documents for information on how to install and use JDBC.

  2. sait says:

    How can we create text files (as database) in matlab
    - cerate fields of table
    - insert data to textfiles
    - get data from textfiles
    - select data from text files

  3. MH says:

    Hi,
    awesome post so far, helps a lot to get close to this stuff. Have you ever had the problem that Matlab couldn’t find the JCDB driver with this address? (com.mysql.jdbc.Driver)

    I’m still wondering why I can’t access this driver. Do you know an other address?

    Thanks
    MH

  4. sani says:

    hi,
    i am working on a project and it is on transactions of the database .plz tell me how to use matlab for this purpose……..plz it would be very kind of u if u could mail me some info regarding it at aknem.ani8@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>