How do I connect to a mySQL database in a compiled application?

8 views (last 30 days)
Henric Rydén
Henric Rydén on 15 May 2014
Commented: Henric Rydén on 15 May 2014
I've written a script that successfully communicates with my local mySQL database using a JDBC driver. Here is the relevant code in the script:
javaaddpath(['C:' filesep 'jdbcDriver' filesep 'mysql-connector-java-5.1.21-bin.jar']);
dbname = 'abc';
username = 'abc';
password = 'abc';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://localhost:3306/' dbname];
conn = database(dbname, username, password, driver, dburl);
Now I want to compile this and use it on a server. I've placed the driver in I've tried also tried setting the java path in javalibrarypath.txt according to this similar problem
but it doesn't work. Does anyone have a suggestion to how to solve this?
output of
ver:
----------------------------------------------------------------------------------------------------
MATLAB Version: 8.2.0.701 (R2013b)
MATLAB License Number: DEMO
Operating System: Microsoft Windows 8 Version 6.2 (Build 9200)
Java Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------
MATLAB Version 8.2 (R2013b)
Curve Fitting Toolbox Version 3.4 (R2013b)
Database Toolbox Version 5.0 (R2013b)
Image Processing Toolbox Version 8.3 (R2013b)
MATLAB Compiler Version 5.0 (R2013b)
Optimization Toolbox Version 6.4 (R2013b)
Signal Processing Toolbox Version 6.20 (R2013b)
Statistics Toolbox Version 8.3 (R2013b)

Accepted Answer

Friedrich
Friedrich on 15 May 2014
Hi,
1.) When you compile the code simply add the jar as sharred ressource and do not call javaaddpath. So compile with
mcc -m YourCode.m -a 'C:\jdbcDriver\mysql-connector-java-5.1.21-bin.jar'
and change the code to
if ~isdeployed
javaaddpath(['C:' filesep 'jdbcDriver' filesep 'mysql-connector-java-5.1.21-bin.jar']);
end
2.) When you place it on the server does the SQL Server is running on that Server as well? So is this
['jdbc:mysql://localhost:3306/' dbname];
still valid or do you need a different hostname/port?
3.) What error do you get? What does conn.Message give as output when running compiled?
  1 Comment
Henric Rydén
Henric Rydén on 15 May 2014
Thanks for your help, I made the changes you suggested and now it works!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!