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

3 vues (au cours des 30 derniers jours)
Henric Rydén
Henric Rydén le 15 Mai 2014
Commenté : Henric Rydén le 15 Mai 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
MATLAB Version: (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)

Réponse acceptée

Friedrich le 15 Mai 2014
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']);
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 commentaire
Henric Rydén
Henric Rydén le 15 Mai 2014
Thanks for your help, I made the changes you suggested and now it works!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by