Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Ask about autocommit etc.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Ask about autocommit etc.

    Hello everybody, i am new in Java Programming language.. I have a confussion when i try to develop database on my application.. Oke, this is my confussion.

    public boolean updateBook(Book b){
            String query = "UPDATE BOOK SET "
            + "id=?, categori=?, title=?, date=?, source=?, number=?, "
            + "year=?, count=?"
            + "WHERE id=?";
            PreparedStatement statement=null;
            try {
                statement = connection.prepareStatement(query);
                for(int i=1;i<=8;i++)
                    statement.setObject(i, b.getObject(i));
                statement.setInt(9, b.getId());
                connection.setAutoCommit(false);
                if(statement.executeUpdate()==1)
                    connection.commit();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Error when update data\n Error: "+ex.getMessage());
                try { connection.rollback(); } catch (SQLException exr) {}
                return false;
            }finally{try {if(statement != null)statement.close(); } catch (Exception e) { }}
            return true;
        }

    I want to know:
    1. Why the "connection.setAutoCommit()" is used?
    2. When should i set the "connection.setAutoCommit(false)" to "false" or set to "true"?
    3. Why the "connection.commit();" is used?
    4. Why in the try and catch, are used the "connection.rollback();" ? What is the meaning of this?

    I hope at least one of you can give me explanation about my trouble above.
    Please help me to solve it.. Thank you very much. GBU All..
    Last edited by Yoga Herawan; October 5th, 2011 at 07:27 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Ask about autocommit etc.


  3. The Following User Says Thank You to copeg For This Useful Post:

    Yoga Herawan (October 6th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Ask about autocommit etc.

    Quote Originally Posted by copeg View Post
    Thank you very much Mr copeg, i will read this.. If i still confuse, i hope You want to help me..