Solved

Can't update DB in JAVA program

Posted on 2011-03-03
22
460 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
difference of if loops 23 62
configure dependency in POM for new database 3 34
CSV file parsing thru Java 13 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

838 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