Connect to MSSQL from Matlab on Mac OS X
In both cases I assume you do have Database Toolbox installed.
But since it is only wrapper for Java classes it should not be very hard to manage without it (google for “JDBC example” or “jTDS example”).
a) Using Microsoft (free) JDBC driver
1. Download Microsoft SQL Server JDBC Driver
2. Unpack archives
3. Add sqljdbc4.jar to classpath
javaaddpath('PATH_TO_FILE/sqljdbc4.jar');
4. Start Matlab and run
conn = database('database', 'username', 'password',
'com.microsoft.sqlserver.jdbc.SQLServerDriver',
'jdbc:sqlserver://server:1433;database=database');
curs = exec(conn, 'SELECT TOP 10 * FROM database.dbo.TestTable');
curs = fetch(curs);
curs.Data
This method for some mysterious reasons worked for me only once. See this thread.
a) Using jTDS driver (also free)
1. Download jTDS driver from their website
2. Unpack archives
3. Add jtds-1.2.5.jar to classpath
javaaddpath('PATH_TO_FILE/jtds-1.2.5.jar');
4. Start Matlab and run
conn = database('database', 'username', 'password',
'net.sourceforge.jtds.jdbc.Driver',
'jdbc:jtds:sqlserver://server:1433/database');
curs = exec(conn, 'SELECT TOP 10 * FROM database.dbo.TestTable');
curs = fetch(curs);
curs.Data
This one worked for me.
Note
Instead of adding .jar to the classpath every time you start Matlab you can add a row into /Applications/Matlab_R2011a/toolbox/local/classpath.txt file
/Applications/MATLAB_R2011a.app/sqljdbc3/enu/sqljdbc4.jar
/Applications/MATLAB_R2011a.app/jtds/jtds-1.2.5.jar
No comments yet.