本教程介绍如何在Matlab中使用MySQL。注意,您需要下载MySQL Connector/jjarhere(类似于mysql-connector-java-5.1.25-bin.jar)的文件。在% Database Server
host = 'mysql-user.stanford.edu';
% Database Username/Password
user = 'gfarmsharetest';
password = 'putyourpasswordhere';
% Database Name
dbName = 'g_farmshare_testing';
% JDBC Parameters
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';
% Set this to the path to your MySQL Connector/J JAR
javaaddpath('/usr/share/java/mysql-connector-java.jar')
% On Windows it looks like: javaaddpath('C:\server\mysql-connector-java-5.1.25\mysql-connector-java-5.1.25-bin.jar')
% Create the database connection object
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);
% Check to make sure that we successfully connected
if isconnection(dbConn)
% Fetch the symbol, market cap, and last close for the 10 largest
% market cap ETFs
result = get(fetch(exec(dbConn, 'SELECT foo,bar from matlabfoo')), 'Data');
disp(result);
else
% If the connection failed, print the error message
disp(sprintf('Connection failed: %s', dbConn.Message));
end
要运行查询:
^{pr2}$