In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use JDBC connection for transaction processing in JavaBean. It is very detailed and has a certain reference value. Friends who are interested must read it!
In JavaBean database operations, a transaction is an indivisible unit of work consisting of one or more SQL statements that update the database. Only when all operations in the transaction are completed normally can the entire transaction be committed to the database, and if one operation is not completed, the entire transaction must be undone. We end the transaction by committing commit () or rolling back rollback (). The methods about transaction operations are located in the interface java.sql.Connection.
When JavaBean establishes a connection to the database for * * times, by default, the JDBC connection is in autocommit mode. In JDBC API, you can disable the autocommit of transactions by calling setAutoCommit (false). You can then treat multiple SQL statements that update the database as a transaction, and after all the operations are complete, call commit () to commit as a whole. If one of the SQL operations fails, the commit () method is not executed, but the corresponding sqlexception is generated, and the rollback () method is called in the exception block to undo the transaction. Here is an example
For example, there are two tables, a record user account (useraccount) and a system account (sysaccount). Now if a user wants to send money to the system to buy something, he or she needs the following two sentences on this line:
String sql1 = "update useraccount set monery=monery-1000 where name='username'"
String sql2 = "update sysaccount set monery=monery+1000 where name='sysname'"
However, if the * clause is executed and the second statement is executed incorrectly, it will have adverse consequences. This can be prevented by manual submission: the main code:
Try {
.
.
.
Conn=DriverManager.getConnection ("..."); / / Link database
Conn.setAutoCommit (false); / / disable auto-commit of transactions
Stmt = conn.Create....
String sql1 = "update useraccount set monery=monery-1000 where name='usename'"
String sql2 = "update sysaccount set monery=monery+1000 where name='sysname'"
Stmt=conn.createStatement ()
Stmt.executeUpdate (sql1)
Stmt.executeUpdate (sql2)
Conn.commit (); / / Unified submission.
} catch (SQLException e) {
Conn.rollback (); / / if one of the sql operations fails, the commit () method will not be executed
/ / instead, the corresponding sqlexception is generated, and the rollback () method is called in the exception code block to undo the transaction.
E.printStackTrace ()
}
Finally {
If (stmtasking null) {
Stmt.close ()
}
If (connexual null) {
Stmt.close ()
}
}
These are all the contents of the article "how to use JDBC connections for transactions in JavaBean". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 286
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.