Search code examples
javasqlucanaccess

Java application hangs when using UCanAccess to save data


I had a program for store student info into Microsoft accesss database I using ucanaccess, jre 1.8 and jdk 1.8 however the data I directly write into database can display in my program but the data I input in program cannot write into database, it just freeze on the moment i save data to database

here is my connection class

class myConnection{
    ResultSet re;

        String strurl = "jdbc:ucanaccess://student.accdb";

    public myConnection(){}
    public ResultSet getResult(String sql){
        try{
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

            Connection conn=DriverManager.getConnection(strurl);

            Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet re=stmt.executeQuery(sql);
            return re;
        }
        catch(Exception e){
            System.out.println("getResult------"+e.toString());
            return null;
        }
    }

    public boolean executeSql(String sql){
        try{
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

            Connection conn=DriverManager.getConnection(strurl);
            Statement stmt=conn.createStatement();
            stmt.executeUpdate(sql);
            conn.commit();
            return true;
        }
        catch(Exception e){
            System.out.println("executeSql----"+e.toString());
            return false;
        }
    }
}

Solution

  • Specifying ResultSet.CONCUR_UPDATABLE and failing to close the ResultSet leaves a lock on the table(s) involved. Since you are not actually updating the ResultSet you can simply omit that option (and thereby use the default ResultSet.CONCUR_READONLY) to avoid the issue.