[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Can't update DB in JAVA program

Posted on 2011-03-03
22
Medium Priority
?
485 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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 200 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

591 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