JDBC transaction is turned on

Source: Internet
Author: User
Tags prepare

Based on this article, the red part represents the added code, accompanied by an explanatory note.

Http://www.cnblogs.com/xyyz/p/7119076.html

1  PackageCom.xyyz.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 8 Importcom.xyyz.utils.JDBCUtils;9 Ten  Public classJdbcdemo { One  A      Public Static voidMain (string[] args)throwsException { - query (); - Insert (); the query (); - update (); - query (); - Delete (); + query (); -     } +  A     /** at * Query Code -      *  -      * @throwsException -      */ -      Public Static voidQuery ()throwsException { -Connection Connection =jdbcutils.getconnection (); in         //Get database connection -String sql = "SELECT * FROM Jdbctestdata"; to         //Get Precompiled Objects +PreparedStatement preparestatement =connection.preparestatement (SQL); -         //Execute SQL statement theResultSet ResultSet =preparestatement.executequery (); *         //traversing print SQL statements $          while(Resultset.next ()) {Panax NotoginsengSystem.out.println (Resultset.getint (1) + "--" + resultset.getstring (2)); -         } the         //turn off freeing resources + Jdbcutils.closeall (ResultSet, preparestatement, connection); A     } the  +     /** - * Insert Data $      */ $      Public Static voidInsert ()throwsException { -         //Get database connection -Connection Connection =jdbcutils.getconnection (); the         //This opens the transaction, finally commits, successfully inserts a piece of data. Connection.setautocommit (false);WuyiString sql = "INSERT into Jdbctestdata value (?,?,?)"; the         //Get Precompiled Objects -PreparedStatement Prepare =connection.preparestatement (SQL); WuPrepare.setstring (1, "6"); -Prepare.setstring (2, "Little white"); AboutPrepare.setstring (3, "30"); $         inti =prepare.executeupdate (); - connection.commit (); -System.out.println ("i=" +i); -         //turn off freeing resources AJdbcutils.closeall (NULL, prepare, connection); +     } the  -     /** $ * Update Data the      */ the      Public Static voidUpdate ()throwsException { the  the         //Get database connection -Connection Connection =jdbcutils.getconnection (); in         //This opens the transaction, but does not commit, so the final execution of the modification is useless.  the        Connection.setautocommit (false); theString sql = "Update jdbctestdata set name=?", age=? where id=? "; About         //Get Precompiled Objects thePreparedStatement Prepare =connection.preparestatement (SQL); thePrepare.setstring (1, "Little Red"); thePrepare.setstring (2, "20"); +Prepare.setstring (3, "6"); -         inti =prepare.executeupdate (); theSystem.out.println ("i=" +i);Bayi         //Execute SQL statement theJdbcutils.closeall (NULL, prepare, connection); the     } -  -     /** the * Delete Data the      */ the      Public Static voidDelete ()throwsException { the         //Get database connection -Connection Connection =jdbcutils.getconnection (); theString sql = "Delete from jdbctestdata where id =?"; the         //Get Precompiled Objects thePreparedStatement Prepare =connection.preparestatement (SQL);94Prepare.setstring (1, "6"); the         inti =prepare.executeupdate (); theSystem.out.println ("i=" +i); the         //Execute SQL statement98Jdbcutils.closeall (NULL, prepare, connection); About     } - 101}

JDBC transaction is turned on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.