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.
Code :
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..
Re: Ask about autocommit etc.
Re: Ask about autocommit etc.
Quote:
Originally Posted by
copeg
Thank you very much Mr copeg, i will read this.. If i still confuse, i hope You want to help me.. :)