Professional Documents
Culture Documents
MySQL JDBC For Windows - MATLAB & Simulink - MathWorks United Kingdom
MySQL JDBC For Windows - MATLAB & Simulink - MathWorks United Kingdom
MySQLJDBCforWindowsMATLAB&SimulinkMathWorksUnitedKingdom
CONTENTS
MySQLJDBCforWindows
ThistutorialshowshowtosetupadatasourceandconnecttoyourMySQLdatabase.ThistutorialusestheMySQL
Connector/J5.1.17drivertoconnecttotheMySQLVersion5.5.16database.
Step1.Verifythedriverinstallation.
IftheJDBCdriverforMySQLisnotinstalledonyourcomputer,findthelinkontheDriverInstallationpagetoinstallthe
driver.Followtheinstructionstodownloadandinstallthisdriveronyourcomputer.
Step2.AddtheJDBCdrivertotheMATLABstaticJavaclasspath.
1. RuntheprefdircommandintheCommandWindow.Theoutputofthiscommandisafilepathtoafolderonyour
computer.
2. CloseMATLAB ifitisrunning.
3. Navigatetothefolderandcreateafilecalledjavaclasspath.txtinthefolder.
4. Openjavaclasspath.txt.AddthefullpathtothedatabasedriverJARfileinjavaclasspath.txt.Thefullpath
includesthepathtothefolderwhereyoudownloadedtheJARfilefromthedatabaseproviderandtheJARfile
name.Forexample,C:\DB_Drivers\mysqlconnectorjava5.1.17bin.jar.Saveandclose
javaclasspath.txt.
5. RestartMATLAB.
Alternatively,youcanusejavaaddpathtoaddyourJDBCdrivertothedynamicJavaclasspath.Fordetailsabout
staticanddynamicclasspaths,seeBringingJavaClassesintoMATLABWorkspace.
Step3.SetupthedatasourceusingDatabaseExplorer.
ThisstepisrequiredonlyforconnectingtoDatabaseExplorer.Ifyouwanttousethecommandlinetoconnecttoyour
database,seeConnecttoMySQLusingtheJDBCconnectioncommandline.
1. OpenDatabaseExplorerbyclickingtheAppstabontheMATLABToolstrip.Then,selectDatabaseExplorerfrom
theDatabaseConnectivityandReportingsectionintheappsgallery.Alternatively,enterdexploreatthe
commandline.Ifnodatasourcesaresetup,amessageboxopens.ClickOKtocloseit.Otherwise,theConnectto
aDataSourcedialogboxopens.ClickCanceltoclosethisdialogbox.
2. ClicktheDatabaseExplorertab,andthenselectNew>JDBC.
TheCreateaNewJDBCdatasourcedialogboxopens.
http://uk.mathworks.com/help/database/ug/mysqljdbcwindows.html
1/4
5/13/2015
MySQLJDBCforWindowsMATLAB&SimulinkMathWorksUnitedKingdom
3. SelectMYSQLfromtheVendorlist.Afterselectingthevendor,ifyoudidnotaddtheJDBCdriverfilepathtotheJava
classpath,thisdialogboxdisplaysthismessageatthebottom.Addressthismessagebyfollowingthesteps
describedinStep2.
4. EnterthedatabaseservernameintheServerNamefield,portnumberinthePortNumberfield,usernameinthe
Usernamefield,passwordinthePasswordfield,anddatabasenameintheDatabasefield.
5. ClickTesttotesttheconnection.Ifyourconnectionsucceeded,DatabaseExplorerdisplaysConnection
Successful!
6. EnteradatasourcenameintheDataSourceNamefieldintheCreateaNewJDBCdatasourcedialogbox.Use
anewdatasourcenamethatdoesnotappearintheexistinglistofdatasourcenames.ClickSave.ThenewJDBC
datasourceappearsinthelistofdatasourcesintheConnecttoaDataSourcedialogbox.
7. IfthistimeisthefirsttimethatyouarecreatingadatasourceusingDatabaseExplorer,theNewfiletostoreJDBC
connectionparametersdialogboxopens.UsethisdialogboxtocreateaMATfilethatsavesyourspecifieddata
sourceinformationforfutureDatabaseExplorersessions.ThisMATfilenameisstoredin
setdbprefs('JDBCDataSourceFile')andisvalidforallMATLABsessions.
NavigatetothefolderwhereyouwanttoputtheMATfile,specifyanameforitthatincludesa.matextension,and
clickSave.
Afteryoucompletethedatasourcesetup,connecttotheMySQLdatabaseusingDatabaseExplorerorthe
commandlinewiththeJDBCconnection.
Step4.ConnectusingDatabaseExplorerorthecommandline.
http://uk.mathworks.com/help/database/ug/mysqljdbcwindows.html
2/4
5/13/2015
MySQLJDBCforWindowsMATLAB&SimulinkMathWorksUnitedKingdom
ConnecttoMySQLusingDatabaseExplorer.
1. Aftersettingupthedatasource,connecttoyourdatabasebyselectingthedatasourcenamefortheMySQL
databasefromtheDataSourceslist.Enterausernameandpassword.ClickConnect.
DatabaseExplorerconnectstoyourdatabaseanddisplaysitscontentsinatabnamedwiththedatasourcename.
2. ClosetheconnectionusingDatabaseExplorerbyhoveringthecursorovertheClosebutton(
MySQLJDBCdatasourcenameonthedatabasetab.TheClosebuttonturnsintoaredcircle(
)nexttothe
).Clickitto
closethedatabaseconnection.IfyouwanttocloseDatabaseExplorerandalldatabaseconnections,clickthe
Closebutton(
)inthetoprightcorner.
IfDatabaseExplorerisdocked,clicktheClosebutton(
Explorer.
http://uk.mathworks.com/help/database/ug/mysqljdbcwindows.html
)toclosealldatabaseconnectionsandDatabase
3/4
5/13/2015
MySQLJDBCforWindowsMATLAB&SimulinkMathWorksUnitedKingdom
ConnecttoMySQLusingtheJDBCconnectioncommandline.
1. UsetheVendornamevaluepairargumentofdatabasetospecifyaconnectiontoaMySQLdatabase.For
example,thefollowingcodeassumesyouareconnectingtoadatabasenameddbnameonadatabaseserver
namedsnamewithusernameusernameandpasswordpwd.
conn=database('dbname','username','pwd',...
'Vendor','MySQL',...
'Server','sname');
2. Closethedatabaseconnectionconn.
close(conn)
SeeAlso
close|database|javaaddpath
MoreAbout
WorkingwithDatabaseExplorer
BringingJavaClassesintoMATLABWorkspace
http://uk.mathworks.com/help/database/ug/mysqljdbcwindows.html
4/4