2

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;
        }
    }
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
B.Cos
  • 418
  • 1
  • 13
  • 27
  • 1
    Your code is buggy, no reason to open a CONCUR_UPDATABLE resultset without neither using the resultset update feature nor closing the resultset after its rendering. You're just explicitly creating a lock on the dbms. Use the Statement stmt=conn.createStatement() (without arguments) and it will work. – jamadei Dec 01 '15 at 10:11
  • Thanks jamadei, u help me solve the problem, i thought lock dbms is good idea – B.Cos Dec 02 '15 at 02:50
  • but now has a problem when i save data into database error-->net.ucanaccess.jdbc.UcanaccessSQLException: user lacks privilege or object not found: – B.Cos Dec 02 '15 at 05:46
  • So it is wrong your sql too. May you post it? – jamadei Dec 02 '15 at 07:22

1 Answers1

1

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418