Can't update DB in JAVA program

Hi
Ihave a program in JAVA that connect to an access data base take this data to a CachedRowSetImpl object and than i put a conn.close(); statment after that i am trying to update DB with other data with the same  CachedRowSetImpl  object as shown:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn1 = DriverManager.getConnection("jdbc:odbc:Employees");
crs.acceptChanges(conn1);

but when the program comes to the crs.acceptChanges(conn1); line it stuck and do nothing

What can be the solution?
deko007Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
> java.lang.UnsupportedOperationException

which is what I mentioned earlier :)
0
 
objectsCommented:
I don't believe the access driver supports CachedRowSet
0
 
objectsCommented:
> What can be the solution?


try a different db
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
deko007Author Commented:
Hi thanks for reply

But i succeeded  do take data from ACcess DB and display it on screen with CachedRowSet
Thing just  stuck when it reaches the  crs.acceptChanges(conn1) line.
What's your suggestion?
 
0
 
CEHJCommented:
acceptChanges doesn't make sense at that point - you haven't made any. The following discusses the correct way to do this with the jdbc-odbc bridge:

http://java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/
0
 
deko007Author Commented:
Thank you CEHJ
But that's not what i meant,I program an application and not a JSP page,All things go as needed,but after one connection to DB is esablished and i want to close it with con.close() statement the connection to the DB is not closed,How can i forced it to be close?
This is my problem ! and that's why the other code can't connect and update the DB.
Thanks
0
 
CEHJCommented:
I think you'd probably better post your code
0
 
deko007Author Commented:

Here it is with pleasure:



public  void initRowSet()
  {

 
      Statement stmt = null;
      Connection conn = null;
      ResultSet resultSet = null;

      try {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

         conn = DriverManager.getConnection("jdbc:odbc:Employees");

         stmt = conn.createStatement();
         resultSet = stmt.executeQuery("SELECT * FROM EmpTable");
         
         // create CachedRowSet and populate
         crs = new CachedRowSetImpl();
         crs.populate(resultSet);conn.close();
        conn.close
     }catch(Exception f){}
     try{
      while (crs.next())
     {
         System.out.println("ID: " + crs.getInt(1) +
            ",  Name: " + crs.getString(2)+" ,Rate:"+crs.getInt(3));
      }
    } catch (SQLException se) { }

   
   try{
      crs.first();
     System.out.println(crs.getRow());
     crs.updateInt(3, 1);
     crs.updateRow();
     System.out.println("ID: " + crs.getInt(1) +
            ",  Name: " + crs.getString(2)+" ,Rate:"+crs.getInt(3));
   }catch(SQLException e){System.out.println(e.getMessage());}
    if(commitToDatabase( crs)){System.out.println("okkki");}
   
  }


 
 
/********************************************************************************************/
 public boolean commitToDatabase(CachedRowSetImpl crs)
 {
   Connection conn1 = null;

   try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      conn1 = DriverManager.getConnection("jdbc:odbc:Employees");
      System.out.println("ok7");
      // propagate changes and close connection
      crs.acceptChanges(conn1);
      System.out.println("ok8");
      conn1.close();
      System.out.println("ok9");
      return true;

   } catch (SQLException se) {
     
      return false;

   } catch (Exception e) {
     
      return false;
   }
}
0
 
CEHJCommented:
You're ignoring exceptions - that's a very bad idea. Call printStackTrace in each case
0
 
deko007Author Commented:
The exception is not the problem here,I know u  right in this issue.
When the program reach to this line: crs.acceptChanges(conn1);
It's not trowing any exception it's just stand and do nothing becuse the DB is locked!
I think becuse conn.close() isn't closing the connection
0
 
CEHJCommented:
>>
When the program reach to this line: crs.acceptChanges(conn1);
It's not trowing any exception it's just stand and do nothing becuse the DB is locked!
>>

I've already covered this point http:#35034786
0
 
deko007Author Commented:
In this code ( http:#35034786)i can't see the solution to my code
Please if some one can help.....


















0
 
deko007Author Commented:
OK I figure it out  
the problem was in that i tried to update an null filed in the DB
Thank all
0
 
CEHJCommented:
That's NOT the reason there was a problem with the code indicated
0
 
deko007Author Commented:
I replaced the line: crs.updateString(2, "w7");
With that line: crs.deleteRow();

and it works with no exception..
0
 
CEHJCommented:
Where are either of those lines in the code you posted?
0
 
objectsCommented:
> That's NOT the reason there was a problem with the code indicated

code you posted didn't indicate anything
thats just a tutorial on how to use CachedRowSet. You posted nothing that was specific to the problem.

0
 
deko007Author Commented:
I am new to cachedrowset api'the code that i posted was an exercise,
and it's just didn't updated the DB i tried severl ways but the way that i posted before solved the problem.
0
 
objectsCommented:
yes thats fine, sorry we couldn't help you.
0
 
deko007Author Commented:
OK if u don't think i this is the solution please solve it:

This is the Exception after reaching this line "crs.acceptChanges(conn1);" :

java.lang.UnsupportedOperationException
        at sun.jdbc.odbc.JdbcOdbcConnection.getTypeMap(Unknown Source)
        at com.sun.rowset.internal.CachedRowSetWriter.updateOriginalRow(Unknown Source)
        at com.sun.rowset.internal.CachedRowSetWriter.writeData(Unknown Source)
        at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)
        at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)
        at NatavServer.commitToDatabase(NatavServer.java:106)
        at NatavServer.initRowSet(NatavServer.java:86)
        at NatavServer.<init>(NatavServer.java:32)
        at NatavServer.main(NatavServer.java:24)
0
 
CEHJCommented:
My point was that your code you first posted has nothing to do with the problem. But never mind - good that it's working
0
 
deko007Author Commented:
O.K. when i did this code with MySql  all the methods worked good,I think your solution is  a complete solution,
I was confused becuse in some cases it works on access.
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.