Thread Subject: MySQL JDBC

Subject: MySQL JDBC

From: Luca Cerone

Date: 24 Dec, 2007 12:35:33

Message: 1 of 12

Hi everybody,
I'm trying to connect Matlab (2007a) to a MySQL database,
using JDBC driver.
I downloaded the J connector from MySql Web Site.
I added the path of the .jar driver in the classpath.txt file.
I'm still not able to connect using JDBC driver (but I'm
able to connect using ODBC ones).
I use the following code line

conn =
database('unicredit','user','password','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/bank')

I get an error saying the driver can't be found.
How can I fix it??

Thank you all very much in advance.

I also wish you a Merry Christmas! (here in Italy is
Holiday.. if you don't celebrate it, consider it as a "Best
Wishes" ^_^).

Subject: MySQL JDBC

From: Eric

Date: 25 Dec, 2007 19:04:36

Message: 2 of 12

On Dec 24, 4:35 am, "Luca Cerone" <cer...@to-see.it> wrote:
> Hi everybody,
> I'm trying to connect Matlab (2007a) to a MySQL database,
> using JDBC driver.
> I downloaded the J connector from MySql Web Site.
> I added the path of the .jar driver in the classpath.txt file.
> I'm still not able to connect using JDBC driver (but I'm
> able to connect using ODBC ones).
> I use the following code line
>
> conn =
> database('unicredit','user','password','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/bank')
>
> I get an error saying the driver can't be found.
> How can I fix it??
>
> Thank you all very much in advance.
>
> I also wish you a Merry Christmas! (here in Italy is
> Holiday.. if you don't celebrate it, consider it as a "Best
> Wishes" ^_^).

Maybe these links would help:
http://www-math.mit.edu/~spielman/ECC/javaMatlab.html
http://forums.java.net/jive/message.jspa?messageID=111283

JDBC in Matlab is a problem that I've been struggling with as well, so
I (and probably many others!) would really benefit from whatever you
find should you choose to post whatever you discover.

Hope this helps,
Eric

Subject: MySQL JDBC

From: Luca Cerone

Date: 27 Dec, 2007 09:16:02

Message: 3 of 12

Thanks Eric,
but I could barely understand what those links are about.
Of course if I can use JDBC driver I'll post how I made it.
What sounds strange to me is that under Unix OS there are no
ODBC drives so you can surely use them!

Thanks again -Luca-

> Maybe these links would help:
> http://www-math.mit.edu/~spielman/ECC/javaMatlab.html
> http://forums.java.net/jive/message.jspa?messageID=111283
>
> JDBC in Matlab is a problem that I've been struggling with
as well, so
> I (and probably many others!) would really benefit from
whatever you
> find should you choose to post whatever you discover.
>
> Hope this helps,
> Eric

Subject: MySQL JDBC

From: Eric

Date: 3 Jan, 2008 01:04:20

Message: 4 of 12

On Dec 27 2007, 1:16 am, "Luca Cerone" <cer...@to-see.it> wrote:
> Thanks Eric,
> but I could barely understand what those links are about.
> Of course if I can use JDBC driver I'll post how I made it.
> What sounds strange to me is that under Unix OS there are no
> ODBC drives so you can surely use them!
>
> Thanks again -Luca-
>
>
>
> > Maybe these links would help:
> >http://www-math.mit.edu/~spielman/ECC/javaMatlab.html
> >http://forums.java.net/jive/message.jspa?messageID=111283
>
> > JDBC in Matlab is a problem that I've been struggling with
> as well, so
> > I (and probably many others!) would really benefit from
> whatever you
> > find should you choose to post whatever you discover.
>
> > Hope this helps,
> > Eric

Hmm, I overlooked the fact that you were using Unix and the database
toolbox. I was thinking that you were connecting to mysql straight
from the command window.

One thing I heard about the Java classpath and Matlab is that people
need to put their jars in the current directory (and have the current
directory in classpath.txt) for everything to work. I think that's
mentioned (briefly) in one of the links I posted, or I may have read
it somewhere else.

Eric

Subject: MySQL JDBC

From: Albert Reuther

Date: 9 Jan, 2008 16:49:02

Message: 5 of 12

Eric <ericdbw@gmail.com> wrote in message <0a3ec023-ff89-4b00-8a45-
59d2591fa736@i12g2000prf.googlegroups.com>...
> On Dec 27 2007, 1:16 am, "Luca Cerone" <cer...@to-see.it> wrote:
> > Thanks Eric,
> > but I could barely understand what those links are about.
> > Of course if I can use JDBC driver I'll post how I made it.
> > What sounds strange to me is that under Unix OS there are no
> > ODBC drives so you can surely use them!
> >
> > Thanks again -Luca-
> >
> >
> >
> > > Maybe these links would help:
> > >http://www-math.mit.edu/~spielman/ECC/javaMatlab.html
> > >http://forums.java.net/jive/message.jspa?messageID=111283
> >
> > > JDBC in Matlab is a problem that I've been struggling with
> > as well, so
> > > I (and probably many others!) would really benefit from
> > whatever you
> > > find should you choose to post whatever you discover.
> >
> > > Hope this helps,
> > > Eric
>
> Hmm, I overlooked the fact that you were using Unix and the database
> toolbox. I was thinking that you were connecting to mysql straight
> from the command window.
>
> One thing I heard about the Java classpath and Matlab is that people
> need to put their jars in the current directory (and have the current
> directory in classpath.txt) for everything to work. I think that's
> mentioned (briefly) in one of the links I posted, or I may have read
> it somewhere else.
>
> Eric


While putting jars in your current directory is the simplest, Matlab has its own
java classpath file. Take a look at these commands: javaclasspath, javaaddpath, and javarmpath for manipulating the java class path. You can
also edit it without these commands, but I much prefer using the commands
in my startup.m to add paths automatically.

Subject: MySQL JDBC

From: Eric

Date: 14 Jan, 2008 19:42:26

Message: 6 of 12

On Jan 9, 8:49 am, "Albert Reuther" <reut...@ll.mit.edu> wrote:
> Eric <eric...@gmail.com> wrote in message <0a3ec023-ff89-4b00-8a45-
>
> 59d2591fa...@i12g2000prf.googlegroups.com>...
>
>
>
> > On Dec 27 2007, 1:16 am, "Luca Cerone" <cer...@to-see.it> wrote:
> > > Thanks Eric,
> > > but I could barely understand what those links are about.
> > > Of course if I can use JDBC driver I'll post how I made it.
> > > What sounds strange to me is that under Unix OS there are no
> > > ODBC drives so you can surely use them!
>
> > > Thanks again -Luca-
>
> > > > Maybe these links would help:
> > > >http://www-math.mit.edu/~spielman/ECC/javaMatlab.html
> > > >http://forums.java.net/jive/message.jspa?messageID=111283
>
> > > > JDBC in Matlab is a problem that I've been struggling with
> > > as well, so
> > > > I (and probably many others!) would really benefit from
> > > whatever you
> > > > find should you choose to post whatever you discover.
>
> > > > Hope this helps,
> > > > Eric
>
> > Hmm, I overlooked the fact that you were using Unix and the database
> > toolbox. I was thinking that you were connecting to mysql straight
> > from the command window.
>
> > One thing I heard about the Java classpath and Matlab is that people
> > need to put their jars in the current directory (and have the current
> > directory in classpath.txt) for everything to work. I think that's
> > mentioned (briefly) in one of the links I posted, or I may have read
> > it somewhere else.
>
> > Eric
>
> While putting jars in your current directory is the simplest, Matlab has its own
> java classpath file. Take a look at these commands: javaclasspath, javaaddpath, and javarmpath for manipulating the java class path. You can
> also edit it without these commands, but I much prefer using the commands
> in my startup.m to add paths automatically.

Oh, yes, that is a much better approach. Placing jars in the current
directory is not a good way to add jars to your classpath. I was
trying to imply that there was an issue with how Matlab handles the
Java classpath, and that a workaround was to put the jar in the
current working directory. A poster on this forum (http://
forums.java.net/jive/message.jspa?messageID=111283) suggests that this
"workaround" worked for him/her.

However I shouldn't have suggested this solution without first testing
it myself! I don't think that there is an issue with the Java
classpath. However, the last poster on the link above does suggest
that there is an issue with how Matlab handles the Java class loader,
and this may be the cause of the pain encountered by people using JDBC
in Matlab.

Here is an approach to connecting to MySQL using JDBC that works on
R2006b (7.3.0) and only requires the MySQL Connector/J driver (i.e.:
does not require Matlab database toolbox). If it doesn't work you most
likely did not add the driver to your Matlab's Java classpath or you
have an issue with your database.

1) Add the JDBC jar to your static classpath.
2) Run the following code:
import java.lang.Thread
import java.lang.Class
import java.sql.DriverManager

current_thread = java.lang.Thread.currentThread();
class_loader = current_thread.getContextClassLoader();
class = java.lang.Class.forName('com.mysql.jdbc.Driver', true,
class_loader);

database_url = 'jdbc:mysql://host/db';
conn = java.sql.DriverManager.getConnection(database_url, user, pass);
stmt = conn.createStatement();
result = stmt.executeQuery(some_query);
col_count = result.getMetaData().getColumnCount();

data = [];
while result.next()
    for i=1:col_count
        data = [data result.getString(i)];
    end
end

Eric

Subject: MySQL JDBC

From: M K

Date: 14 Jan, 2008 21:14:03

Message: 7 of 12

I connect via the database toolbox and JDBC drivers to a
database with the classpath.txt file edited in my version of
matlab and the JDBC drivers in the same directory as the
Matlab code.

I have problems when I compile the code into a stand alone
application and want to put it on a machine without matlab.
I've been told I need to edit the equivalent classpath.txt
file for the MCR Installer, but I can't find this. Has
anyone done this??

Compiling the stand alone application worked fine with ODBC
drivers so long as I added the source and correct database
name on the deployed machine.

Thanks,
MK

Subject: MySQL JDBC

From: Eric

Date: 14 Jan, 2008 23:00:17

Message: 8 of 12

On Jan 14, 1:14 pm, "M K" <n...@none.com> wrote:
> I connect via the database toolbox and JDBC drivers to a
> database with the classpath.txt file edited in my version of
> matlab and the JDBC drivers in the same directory as the
> Matlab code.
>
> I have problems when I compile the code into a stand alone
> application and want to put it on a machine without matlab.
> I've been told I need to edit the equivalent classpath.txt
> file for the MCR Installer, but I can't find this. Has
> anyone done this??
>
> Compiling the stand alone application worked fine with ODBC
> drivers so long as I added the source and correct database
> name on the deployed machine.
>
> Thanks,
> MK

MK, try adding the jar to your Windows system classpath on the machine
without Matlab: Control Panel -> System -> Advanced -> Environment
Variables. Add the location of your JDBC driver to the CLASSPATH
variable in the "System variables" box then reboot. After you reboot
if you open up a command window and type "echo %CLASSPATH%" you should
see the location of your JDBC driver.

HTH, Eric

Subject: MySQL JDBC

From: jaeger2000

Date: 22 Jan, 2008 08:21:11

Message: 9 of 12

With MySQL and MATLAB:

This works for me:
conn = database(Database_Name,USER,PASSWORD,'com.mysql.jdbc.Driver',
['jdbc:mysql://',IP_ADDRESS,':3306/',Database_Name]')

For step by step info.
See: http://desk.stinkpot.org:8080/tricks/index.php/2006/02/how-to-get-matlab-to-talk-to-mysql/


Ian Gregory.

Subject: MySQL JDBC

From: Ian Gregory

Date: 23 Feb, 2008 04:32:05

Message: 10 of 12

MK, did you ever get MySQL and MATLAB working in a
standalone program using the MATLAB compiler?

I tried placing a reference to the .jar file (mysql-
connector-java-3.1.12-bin.jar) in the operating system
CLASSPATH like Eric suggested with no luck.

I have also tried placing the reference in the:
MATLAB Comoponent Runtime\v76\toolbox\local\classpath.txt

Executing the following (compiled) code:

function test_compiled_database
    IP_ADDRESS='192.168.1.64'
    Database_Name='My_Database'
    USER='root'
    PASSWORD='password'

    conn = database
(Database_Name,USER,PASSWORD,'com.mysql.jdbc.Driver',
['jdbc:mysql://',IP_ADDRESS,':3306/',Database_Name]');

end

I receive the error:
------------------------------------------
??? Undefined variable "com" or
class "com.mathworks.toolbox.database.databaseConnect".
Error in ==>database.database at 91
Error in ==> test_compiled_database at 7

MATLAB:undefinedVarOrClass
------------------------------------------

kind regards,

Ian.


Subject: MySQL JDBC

From: Ian Gregory

Date: 24 Feb, 2008 08:21:06

Message: 11 of 12

Hello,

I got a compiled MATLAB database program to work.

Appears to be a bug in earlier MATLAB versions??

For MATLAB 2007b all you need on the remote machine is the
compiled program and the required files from the
MCRINSTALLER.
Then copy the .jar file to any location you like and
reference the file in the classpath.txt file.

ie. Insert this line (for my setup in classpath.txt)

$matlabroot/java/jar/mysql-connector-java-3.1.12-bin.jar

And run a compiled version of the code (I posted earlier).

NOTE: If it doesn't work for the above:
1.) Check that MySQL is running and the computer you are
running the program on can see the database.
2.) If you are having trouble locating a compiler in
MATLAB. Check that you are using the same bit operating
system. That is; if you are using 64bit MATLAB, mbuild -
setup will not be able to locate your 32bit compilers.
You will need to install MATLAB 32bit version and use the
32bit compilers.

HTH,

Ian Gregory.





Subject: MySQL JDBC

From: mostod

Date: 24 Feb, 2008 13:20:02

Message: 12 of 12

I recently installed MATLAB 2007a on a computer running Fedora 8. While installing I got an error which was something like
libXp.so.6: cannot open shared object file: No such file or directory. Running the following command fixed it
su -c 'yum install libXp libXp-devel'

http://toddirectory.com/?x=&Directory=/Computers/Software/Databases/MySQL/

As you all know Security-Enhanced Linux (SELinux) in Fedora would prevent MATLAB from running. I also came across a thread in MATLAB Newsgroup about the same issue.

http://mosdirectory.com/?x=&Directory=/Computers/Education/Certification/MySQL/

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
compiler M K 14 Jan, 2008 16:15:16
jdbc M K 14 Jan, 2008 16:15:16
standalone application M K 14 Jan, 2008 16:15:16
database toolbox M K 14 Jan, 2008 16:15:16
java classpath Albert Reuther 9 Jan, 2008 11:50:07
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com