MATLAB连接SQLServer和MySql数据库

时间:2022-04-06 03:35:53

MATLAB版本:MATLABR2012 a

需要的驱动:SQLServer2008的驱动sqljdbc4.jar(我电脑上安装的JDK的版本对应的驱动,有的可能是sqljdbc.jar);
MySql的驱动:mysql-connector-java-3.1.14-bin.jar(其他的也可以);
将这两个驱动放在安装路径的MATLAB\R2012a\java\jar\toolbox文件夹下,然后在command window窗口中输入edit classpath.txt在打开的文件最后加入:
$matlabroot/java/jar/toolbox/sqljdbc4.jar
$matlabroot/java/jar/toolbox/mysql-connector-java-3.1.14-bin.jar
当然,直接打开MATLAB\R2012a\toolbox\local\classpath.txt加入也可以。
加入路径后重启MATLAB即可。
 

测试程序:

%% MATLAB连接SQLServer2008数据库
databaseurl = 'jdbc:sqlserver://127.0.0.1:1433;databaseName=bookmis';      % SQLServer2008数据库url
driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver';                   % SQLServer2008jdbc驱动
username = 'sa';                                                           % 数据库使用名
password = '******';                                                       % 数据库密码
databasename = 'bookmis';                                                  % 数据库名
conn = database(databasename, username, password, driver, databaseurl);    % 连接数据库
ping(conn)                                                                %测试数据库连接状态
tic                                                                        % 计时
sqlquery = 'select * from books';                                          % 定义SQL语句
curs = exec(conn,sqlquery);                                                % 执行SQL语句
curs = fetch(curs);                                                        % 将SQL执行结果返回到MATLAB工作空间
data = curs.Data;                                                          % 得到SQL执行结果中的数据
toc                                                                        % 计时结束
close(curs);                                                               % 关闭游标对象
close(conn);                                                               % 关闭数据库连接对象

%% MATLAB连接mysql数据库
databaseurl = 'jdbc:mysql://127.0.0.1:3306/bookmis';                       % mysql数据库url
driver = 'com.mysql.jdbc.Driver';                                          % mysql数据库驱动
username = 'root';                                                         % mysql数据库使用名
password = '******';                                                       % mysql数据库密码
databasename = 'bookmis';                                                  % 数据库名
conn = database(databasename, username, password, driver, databaseurl);    % 连接数据库 
ping(conn)                                                                %测试数据库连接状态
tic                                                                        % 计时开始
sqlquery = 'select * from borrow';                                         % 定义SQL语句
curs = exec(conn,sqlquery);                                                % 执行SQL语句
curs = fetch(curs);                                                        % 将SQL执行结果返回到MATLAB工作空间
data = curs.Data;                                                          % 得到SQL执行结果中的数据
toc                                                                        % 计时结束
close(curs);                                                               % 关闭游标对象
close(conn);                                                               % 关闭数据库连接对象

本文出自 “风雪夜之星” 博客,请务必保留此出处http://592669550.blog.51cto.com/5487419/1069666