Solved

Can't update DB in JAVA program

Posted on 2011-03-03
22
457 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
Non-recursive backtracking, using a stack 1 103
recursion example 16 111
Java DateChooser? 3 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now