Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

connection of MATLAB 7.0 and MYSQL

I want to connect MATLAB with MYSQL.I dont know the procedure.In MATLAB help it says about some drivers which confuses me alot.Can someone please guide me!please tell me the complete process.I shall be very very thankful!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use JDBC to connect from MATLAB to mySQL database. Works seamlessly.

  • First download JDBC driver for mySQL from here: http://www.mysql.com/downloads/connector/j/
  • Unpack mysql-connector-java-x.x.xx-bin.jar (the latest version) file from the archive into a folder
  • In the beginning of your script add the path to this jar file, then you can connect to a database and so on.

Here is an example of connecting to and querying public human genome database:

%# add path to the JAR file you just installed to Java dynamic classpath
javaaddpath('h:DocumentsMATLABmyJavaClassesmysql-connector-java-5.1.12-bin.jar')
%# connection parameteres
host = 'genome-mysql.cse.ucsc.edu';
user = 'genome';
password = '';
dbName = 'hg18'; 
%# JDBC parameters
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';

%# Create the database connection object
conn = database(dbName, user , password, jdbcDriver, jdbcString);

gene = 'NF1';
if isconnection(conn) % check to make sure that we successfully connected
    qry = sprintf('SELECT geneName, chrom, txStart, txEnd FROM refFlat WHERE geneName=''%s''',gene);
    rs = fetch(exec(conn, qry));
    rsdata = get(rs, 'Data');
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...