database connection in 2017b
Show older comments
Ok, I recently installed Matlab 2017b. They said something about changes in the database toolbox. I thought they affect the GUI mostly. However my code, which is working well in 2017a, won't work in 2017b.
if true
dbpath = 'D:\xxxxx.mdb';
url = [['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='''';DBQ='] dbpath];
con = database('','','','sun.jdbc.odbc.JdbcOdbcDriver', url);
tic
qry = exec(con,['SELECT Mode,ID,Station,R,U,f,Valid,SectionNr FROM tblSectionData WHERE ID >= 53500 ORDER BY ID']);
qry = fetch(qry);
temp = qry.Data;
close(qry);
close(con);
toc
end
I checked in the GUI and the DB Driver is also listed there, like in 2017a, with the same name. I'm even able to open an connection with the GUI and import data. I don't want to do this through the GUI all the time though.
Has anyone got an idea what I need to change in order to make the code work in 2017b?
I can only guess that somehow this "translator" which works the SQL statements on the Access database is working differently in 2017b.
Accepted Answer
More Answers (4)
Silvia Dell'Acqua
on 14 Dec 2017
Here are some instruction on how to move from the JDBC-ODBC bridge to the UCanAccess driver. The static connection is more reliable, but please note that in case you were deploying the code, you could only use the dynamic connection as the Matlab Runtime doesn’t work with the static one.
STATIC CONNECTION
[1] Download the UCanAccess drivers from http://ucanaccess.sourceforge.net/site.html[it’s a folder containing a bunch of files and subfolders]
[2] copy the whole folder somewhere on your local drive, e.g. ‘C:\Program Files\MATLAB\UCanAccess-4.0.2-bin’ (hereafter $driverfolder)
[3] create a .txt file ‘javaclasspath.txt’ and save it under ‘$prefdir’, that you can find launching the command >>prefdir
writhe inside the file the following
$driverfolder \ucanaccess-4.0.2.jar
$driverfolder \lib\commons-lang-2.6.jar
$driverfolder \lib\commons-logging-1.1.1.jar
$driverfolder \lib\hsqldb.jar
$driverfolder \lib\jackcess-2.1.6.jar
$driverfolder \loader\ucanload.jar
[4] create a .obs file ‘java.obs’ and save it under ‘$matlabroot\bin\$arch’, that you can find launching the commands >>matlabroot and >>computer(‘arch’)
writhe inside the file the following
-DUCANACCESS_HOME=$driverfolder
[5] change the sting connections inside the code from
url = ['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=' db_path];
conn = database('','','','sun.jdbc.odbc.JdbcOdbcDriver',url);
to
db_path = strrep(db_path,'\','/');
url = ['jdbc:ucanaccess://' db_path];
conn = database('','','','net.ucanaccess.jdbc.UcanaccessDriver',url);
where ‘db_path’ is the path where your DB is saved
DYNAMIC CONNECTION
[1] as [1] above
[2] copy the whole folder somewhere in a predefined path you are releasing with the executable or let the users copy it in their local machines (note that the code needs this path as an input – in case you want to let the users change it, consider to add a field in your gui interface)
[3] add to the code the following lines
javaaddpath([folder_driver '\ucanaccess-4.0.2.jar'])
javaaddpath([folder_driver '\lib\commons-lang-2.6.jar'])
javaaddpath([folder_driver '\lib\commons-logging-1.1.1.jar'])
javaaddpath([folder_driver '\lib\hsqldb.jar'])
javaaddpath([folder_driver '\lib\jackcess-2.1.6.jar'])
javaaddpath([folder_driver '\loader\ucanload.jar'])
where ‘folder_driver’ is the folder mentioned in point [2] (e.g. folder_driver = [pwd '\UCanAccess-4.0.2-bin'];)
[4] as [6] above
4 Comments
Steineklopfer
on 14 Dec 2017
Graeme
on 17 Jun 2020
Thanks for the thorough walkthrough above! I am trying to get this set up exactly as you've described with MATLAB R2019b, and having an issue. I have a few questions:
- Is the process above the same for recent versions of MATLAB, in particular for R2019b?
- I updated the file names and versions inside of the "javaclasspath.txt" file to match the latest download of UCanAccess.
- I'm not familiar with .obs files. I simply created a txt file in notepad and saved it with the extension .obs. Will this work?
- I confirmed from the UCanAccess site examples here that the driver names to use in the MATLAB code are still the same.
Here is my code:
db_path = 'C:\Data\file1.mdb'
db_path = strrep(db_path,'\','/');
url = ['jdbc:ucanaccess://' db_path];
conn = database('','','','net.ucanaccess.jdbc.UcanaccessDriver',url);
But when I run this I get a conn.Message of:
'JDBC Driver Error: No suitable driver found for jdbc:ucanaccess://C:/Data/file1.mdb'
Do you have any suggestions for what to try next? Thank you!
Graeme
on 17 Jun 2020
I've made two additional changes and am now getting a different error:
- I had mistakenly put the javaclasspath.txt in the main MATLAB\R2019b folder, but I relocated it to the $prefdir folder exactly as returned by the command "prefdir"
- Should the javaclasspath.txt file actually have a space in each line between the $driverfolder and the '\...' portion?
I relaunched MATLAB, and when I run the code above I get the following error. Thank you in advance for any help you can provide!
Error using database (line 59)
Java exception occurred:
java.lang.NoClassDefFoundError: Could not initialize class
net.ucanaccess.jdbc.UcanaccessDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.sql.DriverManager.isDriverAllowed(DriverManager.java:556)
at java.sql.DriverManager.isDriverAllowed(DriverManager.java:548)
at java.sql.DriverManager.getDriver(DriverManager.java:297)
at
com.mathworks.toolbox.database.DatabaseConnection.<init>(DatabaseConnection.java:105)
Graeme
on 17 Jun 2020
Ok, one more attempt and details to add!
I found that in my javaclasspath.txt file I had mistakenly typed "...hsqldb_2.5.0.jar" instead of the correct "...hsqldb-2.5.0.jar"
I corrected this, relaunched MATLAB, and reran the code.
I got both a warning and an error:
WARNING:You shouldn't use 'user' reserved word as column name in the table BatchHistory_Table (it refers to the database user).
Escape it in your SQL!
(e.g. SELECT [USER] FROM TABLE WHERE [USER]='Joe')
Does this warning just come up every time? Or does this mean something about my mdb file that I need to investigate/correct?
Then, I get a conn.Message of:
'JDBC Driver Error: UCAExc:::5.0.0-SNAPSHOT unexpected token: AND required: VALUE'
Any suggestions on this? Thank you!
Steineklopfer
on 9 Jan 2018
0 votes
Silvia Dell'Acqua
on 18 Jan 2018
I've faced the same issue
java.lang.OutOfMemoryError: GC overhead limit exceeded
java.lang.OutOfMemoryError: Java heap space
when trying to insert a much larger dataset (275.000 x 3 records of doubles).
You can easily fix it with one of the following
[1] increase the Java Heap memory
open Matlab, menu "Home", select "Preferences", then "General", then "Java Heap Memory"
[2] change the java.obs file adding this command
-XX:UseGCOverheadLimit
[3] split the query to insert subsets of data multiple times. Potentially you need to open/close the connection each time
1 Comment
Steineklopfer
on 21 Jan 2018
Stanislav Ginzburg
on 28 Mar 2018
0 votes
What about connection to Oracle 11/12 by driver jdbc java8 ?
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!