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

조회 수: 3 (최근 30일)
Henric Rydén
Henric Rydén 2014년 5월 15일
댓글: Henric Rydén 2014년 5월 15일
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)

채택된 답변

Friedrich
Friedrich 2014년 5월 15일
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
Henric Rydén
Henric Rydén 2014년 5월 15일
Thanks for your help, I made the changes you suggested and now it works!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by