Solved

Can't update DB in JAVA program

Posted on 2011-03-03
22
465 Views
Last Modified: 2013-11-24
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?
0
Comment
Question by:deko007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 5
22 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35033986
I don't believe the access driver supports CachedRowSet
0
 
LVL 92

Expert Comment

by:objects
ID: 35033987
> What can be the solution?


try a different db
0
 

Author Comment

by:deko007
ID: 35034262
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35034786
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
 

Author Comment

by:deko007
ID: 35044194
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35044217
I think you'd probably better post your code
0
 

Author Comment

by:deko007
ID: 35044270

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35044281
You're ignoring exceptions - that's a very bad idea. Call printStackTrace in each case
0
 

Author Comment

by:deko007
ID: 35044415
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35044437
>>
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
 

Author Comment

by:deko007
ID: 35046465
In this code ( http:#35034786)i can't see the solution to my code
Please if some one can help.....


















0
 

Author Comment

by:deko007
ID: 35068165
OK I figure it out  
the problem was in that i tried to update an null filed in the DB
Thank all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35068199
That's NOT the reason there was a problem with the code indicated
0
 

Author Comment

by:deko007
ID: 35068219
I replaced the line: crs.updateString(2, "w7");
With that line: crs.deleteRow();

and it works with no exception..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35068253
Where are either of those lines in the code you posted?
0
 
LVL 92

Expert Comment

by:objects
ID: 35074216
> 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
 

Author Comment

by:deko007
ID: 35080466
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
 
LVL 92

Expert Comment

by:objects
ID: 35080550
yes thats fine, sorry we couldn't help you.
0
 

Author Comment

by:deko007
ID: 35095870
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35095918
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
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 35099523
> java.lang.UnsupportedOperationException

which is what I mentioned earlier :)
0
 

Author Closing Comment

by:deko007
ID: 35175389
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 26
how to see all occupied ports on windows 10 laptop 15 92
mysql jsp example issue 32 92
wild fly 8 startup error 2 70
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question