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