matlab 连接 mysql 数据库
2016-03-11 09:45
495 查看
首先要安装mysql驱动程序包,详细步骤如下:
Step 1: 将mysql-connector-java-5.1.7-bin.jar文件拷贝到......\MATLAB\R2009a\java\jar\toolbox
Step 2: 到......\MATLAB\R2009a\toolbox\local目录下,找到classpath.txt文件,打开,并添加用来加载mysql的jdbc驱动语句:
$matlabroot/java/jar/toolbox/mysql-connector-java-5.1.7-bin.jar
Step 3:重新打开MATLAB即可
驱动程序安装成功后,接来下要是matlab连接mysql数据库的代码:
conn =database('databasename','username','password','driver','databaseurl')
连接成功后,返回连接对象。
参数如下:
*databasename: 数据库名称.
*driver: JDBC driver.
*username and password: 用户名和密码.
*databaseurl: 类似于jdbc:subprotocol:subname.subprotocol 是数据库类型,
subname 类似于//hostname:port/databasename.
如果matlab和数据库建立了连接,将返回类似于如下信息:
Instance: 'SampleDB'
UserName: ''
Driver: []
URL: []
Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect]
Message: []
Handle: [1x1sun.jdbc.odbc.JdbcOdbcConnection]
TimeOut: 0
AutoCommit: 'off'
Type: 'Database Object'
连接mysql的代码如下:
conn = database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
连接成功后,就可以用exec函数执行sql语句
exec函数执行sql语句并返回一个开指针
语法如下:
curs = exec(conn,'sqlquery')
例如:curs =exec(conn, 'select * from customers')
执行完查询后,还要将查询结果从开放cursor对象导入到对象curs中,该功能是用
cursor.fetch函数实现的。
语法如下:
curs = fetch(curs)
使用curs.Data来显示数据,curs.Data返回一个CELL结构,可以先把CELL结构转换成
MATRIX结构再取值:disp
cur =cell2mat(cur)
a=cur(1,1);
则查询结果就加到了向量a中
注意:
在exec函数执行查询过程中,有的sql语句要输入变量,这时可使用strcat函数完成该
功能。
t = strcat(s1, s2, s3, ...)
for(t=1:10)
sql1 = strcat('selectcount(did) from rss_genepairs_u where gocc>=',num2str(t),'|| gomf >= ',num2str(t),' || gobp >= ',num2str(t));
end
完整代码如下:
conn =database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
for t=0.5:0.01:0.91
for x=0.5:0.1:11
sql = strcat('selectcount(did) from rss_genepairs_x2 where score <=',num2str(x),' and didin(select did from rss_genepairs_u where gocc >=',num2str(t),' || gomf >=',num2str(t),' || gobp >= ',num2str(t),')');
aTemp = exec(conn,sql);
aTemp = fetch(aTemp);
a = aTemp.Data;
a = cell2mat(a);
a= a(1,1);
end
end
Step 1: 将mysql-connector-java-5.1.7-bin.jar文件拷贝到......\MATLAB\R2009a\java\jar\toolbox
Step 2: 到......\MATLAB\R2009a\toolbox\local目录下,找到classpath.txt文件,打开,并添加用来加载mysql的jdbc驱动语句:
$matlabroot/java/jar/toolbox/mysql-connector-java-5.1.7-bin.jar
Step 3:重新打开MATLAB即可
驱动程序安装成功后,接来下要是matlab连接mysql数据库的代码:
conn =database('databasename','username','password','driver','databaseurl')
连接成功后,返回连接对象。
参数如下:
*databasename: 数据库名称.
*driver: JDBC driver.
*username and password: 用户名和密码.
*databaseurl: 类似于jdbc:subprotocol:subname.subprotocol 是数据库类型,
subname 类似于//hostname:port/databasename.
如果matlab和数据库建立了连接,将返回类似于如下信息:
Instance: 'SampleDB'
UserName: ''
Driver: []
URL: []
Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect]
Message: []
Handle: [1x1sun.jdbc.odbc.JdbcOdbcConnection]
TimeOut: 0
AutoCommit: 'off'
Type: 'Database Object'
连接mysql的代码如下:
conn = database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
连接成功后,就可以用exec函数执行sql语句
exec函数执行sql语句并返回一个开指针
语法如下:
curs = exec(conn,'sqlquery')
例如:curs =exec(conn, 'select * from customers')
执行完查询后,还要将查询结果从开放cursor对象导入到对象curs中,该功能是用
cursor.fetch函数实现的。
语法如下:
curs = fetch(curs)
使用curs.Data来显示数据,curs.Data返回一个CELL结构,可以先把CELL结构转换成
MATRIX结构再取值:disp
cur =cell2mat(cur)
a=cur(1,1);
则查询结果就加到了向量a中
注意:
在exec函数执行查询过程中,有的sql语句要输入变量,这时可使用strcat函数完成该
功能。
t = strcat(s1, s2, s3, ...)
for(t=1:10)
sql1 = strcat('selectcount(did) from rss_genepairs_u where gocc>=',num2str(t),'|| gomf >= ',num2str(t),' || gobp >= ',num2str(t));
end
完整代码如下:
conn =database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
for t=0.5:0.01:0.91
for x=0.5:0.1:11
sql = strcat('selectcount(did) from rss_genepairs_x2 where score <=',num2str(x),' and didin(select did from rss_genepairs_u where gocc >=',num2str(t),' || gomf >=',num2str(t),' || gobp >= ',num2str(t),')');
aTemp = exec(conn,sql);
aTemp = fetch(aTemp);
a = aTemp.Data;
a = cell2mat(a);
a= a(1,1);
end
end
相关文章推荐
- Matlab2014下载和破解方法,以及Matlab很好的学习网站
- matlab
- 图像处理笔记 —— 图像的读取及RGB分离等
- [matlab]并行编程
- Matlab Robotic Toolbox学习
- matlab所需插件
- matlab 编译c文件
- [置顶] MATLAB和c#混合编程实现心电图显示软件
- 为什么matlab生成的exe文件只能在matlab中打开,在电脑的其他地方打不开?
- 使用matlab编译器生成EXE文件
- (笔记) MATLAB 文本的高级控制,极坐标图
- matlab中统计工具箱函数大全
- 双边滤波Matlab实现<The Bilateral Filter>
- Matlab和java混合编程开发
- matlab s变换
- 文章标题Matlab GUI学习笔记(一)function “funcname” 工作区和基本工作区
- (笔记) MATLAB 图像增强
- (笔记) MATLAB 关系运算 逻辑运算
- Matlab 中一些符号的含义
- matlab可以使用detrend函数去除时间序列x中的均值或线性趋势,这在FFT处理中尤其常用。