필터 지우기
필터 지우기

Connecting to MySQL using JDBC

조회 수: 18 (최근 30일)
Nikhil
Nikhil 2012년 3월 18일
편집: Rinky Yadav 2019년 2월 17일
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
Riadi Marta Dinata
Riadi Marta Dinata 2016년 2월 10일
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

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

채택된 답변

Aldin
Aldin 2012년 3월 18일
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
JohnGalt
JohnGalt 2016년 5월 20일
Rinky Yadav
Rinky Yadav 2019년 2월 17일
편집: Rinky Yadav 2019년 2월 17일
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)
>>

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

추가 답변 (1개)

Greg
Greg 2012년 3월 26일
how would this work on a mac? where do I copy my JDBC driver to? (since there's no matlab folder?)
  댓글 수: 1
Nikhil
Nikhil 2012년 3월 26일
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.

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

카테고리

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