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驅(qū)動(dòng)語(yǔ)句:
$matlabroot/java/jar/toolbox/mysql-connector-java-5.1.7-bin.jar
Step 3:重新打開MATLAB即可
驅(qū)動(dòng)程序安裝成功后,接來下要是matlab連接mysql數(shù)據(jù)庫(kù)的代碼:
conn=database('databasename','username','password','driver','databaseurl')
連接成功后,返回連接對(duì)象。
參數(shù)如下:*databasename: 數(shù)據(jù)庫(kù)名稱.
*driver: JDBC driver.
*username and password: 用戶名和密碼.
*databaseurl: 類似于jdbc:subprotocol:subname. subprotocol是數(shù)據(jù)庫(kù)類型,
subname類似于//hostname:port/databasename.
如果matlab和數(shù)據(jù)庫(kù)建立了連接,將返回類似于如下信息:
Instance: 'SampleDB' UserName: ''Driver: []URL: []Constructor: [1x1com.mathworks.toolbox.database.databaseConnect] Message: []Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection] TimeOut: 0AutoCommit: 'off' Type: 'Database Object' |
連接mysql的代碼如下:
conn = database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
連接成功后,就可以用exec函數(shù)執(zhí)行sql語(yǔ)句
exec函數(shù)執(zhí)行sql語(yǔ)句并返回一個(gè)開指針
語(yǔ)法如下:curs =exec(conn,'sqlquery')
例如:curs = exec(conn, 'select * fromcustomers')
執(zhí)行完查詢后,還要將查詢結(jié)果從開放cursor對(duì)象導(dǎo)入到對(duì)象curs中,該功能是用
cursor.fetch函數(shù)實(shí)現(xiàn)的。curs =fetch(curs)
使用curs.Data來顯示數(shù)據(jù),curs.Data返回一個(gè)CELL結(jié)構(gòu),可以先把CELL結(jié)構(gòu)轉(zhuǎn)換成
MATRIX結(jié)構(gòu)再取值:
cur=cell2mat(cur)
a=cur(1,1);則查詢結(jié)果就加到了向量a中
注意:在exec函數(shù)執(zhí)行查詢過程中,有的sql語(yǔ)句要輸入變量,這時(shí)可使用strcat函數(shù)完成該
功能。t =strcat(s1, s2, s3, ...) for(t=1:10)sql1 = strcat('select count(did) from rss_genepairs_u wheregocc>=',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('select count(did) from rss_genepairs_x2 where score<=',num2str(x),' and did in(select did fromrss_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);endend |
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com