3

I've tried most of the examples found here and the web, but I can't open a MS access database(2002 or 2013) and get an updatable result set using UCanAccess. The same code using the JDBC:ODBC driver/connection/works. I've written short test code to check concur_updatable to check this, so I must be missing something. I'm using JDK 1.7 on a Win7 machine. I also have another machine with the same results. This works:

/*
class jdbc, for testing jdbc:odbc CONCUR_UPDATABLE.
*/

import java.sql.*;

public class jdbc {

private static String dbFQN;

public static void main(String[] args) {

try {
dbFQN = ("C:\\phil\\programming\\kpjl2002.mdb");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + dbFQN;
System.out.println("Loading database: " + database);
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Fetch records from table
String selTable = "SELECT * FROM " + "tblLibrary" + " ORDER BY Artist, Cat, Cart";
s.execute(selTable);
ResultSet rs = s.getResultSet();

int concurrency = rs.getConcurrency();
if(concurrency == ResultSet.CONCUR_UPDATABLE)
{
System.out.println("rs is updatable");
} else {
System.out.println("rs Not updatable");
}

s.close();
conn.close();
} //close try
catch (Exception ex) {
ex.printStackTrace();
}   //close catch
}   //close main method

}   //close dbAccess class

The output is that rs is updatable.

This doesn't work:

/*
class ucan, for testing ucanaccess CONCUR_UPDATABLE.
C:\jdk1.7.0_79\jre\lib\ext\ucanaccess-2.0.9.5.jar
C:\jdk1.7.0_79\jre\lib\ext\hsqldb.jar
C:\jdk1.7.0_79\jre\lib\ext\jackcess-2.1.0.jar
C:\jdk1.7.0_79\jre\lib\ext\commons-lang-2.6.jar
C:\jdk1.7.0_79\jre\lib\ext\commons-logging-1.1.1.jar

also present:
C:\jdk1.7.0_79\jre\lib\ext\commons-logging-1.2.jar
C:\jdk1.7.0_79\jre\lib\ext\commons-lang3-3.4.jar
*/

import java.sql.*;

public class ucan {

private static String dbFQN;

public static void main(String[] args) {

try {
dbFQN = ("C:\\phil\\programming\\kpjl2002.mdb");
String database = "jdbc:ucanaccess://" + dbFQN;
System.out.println("Loading database: " + database);
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Fetch records from table
String selTable = "SELECT * FROM " + "tblLibrary" + " ORDER BY Artist, Cat, Cart";
s.execute(selTable);
ResultSet rs = s.getResultSet();

int concurrency = rs.getConcurrency();
if(concurrency == ResultSet.CONCUR_UPDATABLE)
{
System.out.println("rs is updatable");
} else {
System.out.println("rs Not updatable");
}

s.close();
conn.close();
} //close try
catch (Exception ex) {
ex.printStackTrace();
}   //close catch
}   //close main method

}   //close dbAccess class

the output is that rs is Not updatable. So I cannot update or insert rows in the resultset.

The code posted is the operative part of a larger project, where UCanAccess can read the table and put the contents in a jList and jTextarea, with formatting. When I started writing code to update or add a new record, I ran into the problem.

I apologize if this is a bit long. Anybody have an idea what I'm missing or doing wrong?

BTW, this is one of my 2 fav sites for good, usable Java answers.

UPDATE: Got an idea from old co-worker, the original database may have been copied from an original Access97 db. to a 2000 db. I had used 2013 Repair and Compact to make "new" 2002 and 2013 db's. Access must retain '97 type even when doing what I did. So, I created a new 2013 dummy db to test, and UCanAccess will report resultset as updatable. I will try to recreate the record data of the current db in a new database file, and see if that works. I'm hoping this is the problem, since UCanAccess doesn't support updatability with Access97 db's. I'll let ya'll know what I find. Thanks.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
PiBurner
  • 75
  • 11
  • Probably the Acces ODBC driver does not support updating resultsets. So you will have to use update statements instead. – André Schild Jul 23 '15 at 17:24

2 Answers2

1

I had used 2013 Repair and Compact to make "new" 2002 and 2013 db's. Access must retain '97 type even when doing what I did. So, I created a new 2013 dummy db to test, and UCanAccess will report resultset as updatable.

The "Compact and Repair Database" feature in Access does not change the database file version. If you have (what you suspect to be) an older-version database file then you should use the "Save Database As" feature under "File > Save & Publish" * to convert the database file to a newer version.

* (... at least that's where it is in Access 2010.)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Found and installed a copy of Access 2003. Opened the original db file, and yes, it's Access 97. I did the database conversion to 2000 format, and Ucanaccess will open and display when not using the SCROLL_SENSITIVE & CONCUR_UPDATABLE switches in the statement.execute(tablename) command. When I add those 2 to the switch, it says it's updatable, but doesn't display in the gui or the command window. Same code, but different statement. Pretty much what I got later yesterday by creating new database and cut-paste the data from old to new. – PiBurner Jul 24 '15 at 16:48
0

Well, after a nice weekend of eating brats and drinking good beer at Germanfest, I finally got things working. I decided to scrap the MS Access db and put the 472 records in a SQLite db. With that, I was able to get PreparedStatements to work to display the records in a jList and JTextArea, add a new record and update a couple fields in an existing record within the same run of the test application. Did it both as a command line run GUI and from NetBeans 8.0, so I think my problems are solved. After a couple of summer projects get done, I'll get back to re-writing the original VB app using Java. Thanks Gord, and everyone here.

PiBurner
  • 75
  • 11