Connecting to MySQL using JDBC

16 views (last 30 days)
Nikhil
Nikhil on 18 Mar 2012
Edited: Rinky Yadav on 17 Feb 2019
Hi all,
Please could someone help me with the connection parameters when using MySQL and the JDBC driver?
- Is it necessary to add the datasource using ‘querybuilder’ / ‘confds’ before calling ‘database’?
- When calling database('databasename','username', 'password','driver','databaseurl'), what is the format of the driver argument? Help and other resources (<http://desk.stinkpot.org:8080/tricks/index.php/2006/02/how-to-get-matlab-to-talk-to-mysql/>) say it should be:
‘com.mysql.jdbc.Driver’.
Then I receive a “Driver Not Found/Loaded” error message.
I get the same error if I try the path to the jar file from MySQL: ‘C:/Program Files/Java/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar’
-------------------------
Extra Info: I’ve added the driver filename to the java classpath file but am not sure if I used the correct delimiter. (I just started a new line).
Also, the whole syntax for my ‘database’ call is:
conn = database('trial','myuser','mypassword','C:/Program Files/Java/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar','jdbc:mysql://PC1:3306/trial');
  1 Comment
Riadi Marta Dinata
Riadi Marta Dinata on 10 Feb 2016
try this code : url = 'jdbc:mysql://localhost:3306/'; conn = database('matlab_db', 'root', '', 'com.mysql.jdbc.Driver', url); curs = exec(conn,'SELECT * FROM barang'); curs = fetch(curs) curs.data
tableName = 'barang'; fields = {'kode_barang', 'nama_barang', 'deskripsi', 'harga','kategori', 'gambar', 'status'};
%allData = {'BRG004', 'KOMPUTER 4', 'Harga murah.....harga ok', '1250000', 'Komputer', 'kom4.jpg', 'Tersedia'};
var1=get(handles.guivar1,'String'); var2=get(handles.guivar2,'String'); var3=get(handles.guivar3,'String'); var4=get(handles.guivar4,'String'); var5=get(handles.guivar5,'String'); var6=get(handles.guivar6,'String'); var7=get(handles.guivar7,'String');
allData = {var1, var2, var3,var4,var5,var6, var7}; tic fastinsert(conn,tableName,fields,allData) toc
for more detail try this study matlab to mysql

Sign in to comment.

Accepted Answer

Aldin
Aldin on 18 Mar 2012
At first you need to copy/paste your driver (jar file: mysql-connector-java-5.1.18-bin.jar in in this folder (by me): C:\Program Files\MATLAB\R2009b\java\jarext (The folder is jarext). Now, go in MATLAB workspace and add this code:
javaaddpath([matlabroot,'/java/jarext/mysql-connector-java-5.1.15-bin.jar']) . My connector is mysql-connector-java-5.1.15-bin.jar. You have to put your connector (mysql-connector-java-5.1.18-bin.jar). In this case your code will be: javaaddpath([matlabroot,'/java/jarext/mysql-connector-java-5.1.18-*bin.jar'])* And finally write this in worskpace:
database('databaseName','username','yourpassword','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/databaseName');
That's it
  4 Comments
Rinky Yadav
Rinky Yadav on 17 Feb 2019
Edited: Rinky Yadav on 17 Feb 2019
I tried everything but the jdbc driver is not added to classpath(static and dynamic). COuld you help me out in this?
conn = database('finaldb','root','password', ...
'Vendor','MySQL', ...
'Server','localhost');
and also,
I tried this set of instructions as well
and also tried the above instruction and getting this as the output:
Warning: Invalid file or directory 'C:\Program
Files\MATLAB\R2017a/java/jarext/mysql-connector-java-8.0.15-bin.jar'.
> In javaclasspath>local_validate_dynamic_path (line 271)
In javaclasspath>local_javapath (line 187)
In javaclasspath (line 124)
In javaaddpath (line 71)
In jdbcconnection (line 1)
>>

Sign in to comment.

More Answers (1)

Greg
Greg on 26 Mar 2012
how would this work on a mac? where do I copy my JDBC driver to? (since there's no matlab folder?)
  1 Comment
Nikhil
Nikhil on 26 Mar 2012
So Malcolm is saying it doesn't matter where you put the driver, just be sure to add the full path i.e. 'C:/folder1/.../folderN/mysql-connector-java-5.1.15-bin.jar' to the dynamic path of Matlab using the 'javaaddpath' function.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!