Matlab连接MySQL数据库入门

1、加载MySQL的jdbc驱动

(1)将mysql-connector-java-5.1.28-bin.jar拷贝到D:\Program Files\MATLAB\R2014a\java\jar\toolbox目录;

(2)打开D:\Program Files\MATLAB\R2014a\toolbox\local目录classpath.txt文件,添加用来加载MySQL的jdbc驱动语句,语句如下:

$matlabroot/java/jar/toolbox/mysql-connector-java-5.1.28-bin.jar

2、重启Matlab

3、获取连接对象

>> conn = database('hwd', 'root', '123456', 'com.mysql.jdbc.Driver', 'jdbc:mysql://localhost:3306/hwd')
 
conn =
 
       Instance: 'hwd'
       UserName: 'root'
         Driver: 'com.mysql.jdbc.Driver'
            URL: 'jdbc:mysql://localhost:3306/hwd'
    Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect]
        Message: []
         Handle: [1x1 com.mysql.jdbc.JDBC4Connection]
        TimeOut: 0
     AutoCommit: 'on'
           Type: 'Database Object'

>> 

4、执行SQL语言

(1)查询user表

>> curs = exec(conn, 'select * from user')
 
curs =
 
        Attributes: []
              Data: 0
    DatabaseObject: [1x1 database]
          RowLimit: 0
          SQLQuery: 'select * from user'
           Message: []
              Type: 'Database Cursor Object'
         ResultSet: [1x1 com.mysql.jdbc.JDBC4ResultSet]
            Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
         Statement: [1x1 com.mysql.jdbc.StatementImpl]
             Fetch: 0

>> 
>> curs = fetch(curs)
 
curs =
 
        Attributes: []
              Data: {2x3 cell}
    DatabaseObject: [1x1 database]
          RowLimit: 0
          SQLQuery: 'select * from user'
           Message: []
              Type: 'Database Cursor Object'
         ResultSet: [1x1 com.mysql.jdbc.JDBC4ResultSet]
            Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
         Statement: [1x1 com.mysql.jdbc.StatementImpl]
             Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]

>> 

(2)返回CELL

>> cur = curs.Data

cur = 

    [1]    'tom'      '123'
    [2]    'jerry'    '456'

>> 

(3)取值

>> id = cur(1,1)

id = 

    [1]

>> 

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值