Jdbc-mysql Base engine InnoDB is support rollback

Source: Internet
Author: User
Tags getmessage rollback

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See Root 321, rivers with one.
Meekness Conscience Lord, willing to do without regrets to the most bitter. Reading exercise strong body and mind, Prudential advised and the line and cherish.

Data,
data, Lingen on the data. Cloud computing, AI and other technologies are based on data. You must be cautious about operating the database. Give the most bitter code here, and take a look at it, to have your own judgment. Encounter a choice, or ashamed to ask, think twice.
Javase:8
mysql:5.7.14
mysql-connector-java:5.1.44
mysql-front:5.4
Os:windows7 x64
Ide:myeclipse 2017

Engine information for the table where things are rolled back

Jdbcutils class

Package Com.jizuiku;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import java.sql.sqlexception;/** * * * @author to most bitter * @version V17.11.07 */public final class J dbcutils {/** * URL format, JDBC: Sub-Protocol: Sub-name//hostname: Port number/database name? property Name = Property Value & Property name = attribute value * There are multiple parameters in the configstring variable, which requires deep study of their specific meanings */ private static String configstring = "? useunicode=true&characterencoding=utf8&usessl=true";p rivate static String url = "Jdbc:mysql://localhost:3306/jdbcforjava" + configstring;//local MySQL database (No child name) port number 3306  Database jdbcforjavaprivate static String user = "root";p rivate static string password = "";//tool class, direct use, not raw object private jdbcutils () {}//registered driver, where the static code block is applied only once the feature static {try {Class.forName ("Com.mysql.jdbc.Driver");} catch ( ClassNotFoundException e) {//TODO auto-generated catch Blockthrow new Exceptionininitializererror (e);}} /** * Get a link to a specified database * */public static Connection getconnection () throws SQLException {return Drivermanager.getconnection (url, user, password);} /** * Frees three resources ResultSet preparedstatement Connection * */public static void Free (ResultSet rs, PreparedStatement PS, Connec tion con) {try {if (rs! = null) {Rs.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new Daoexce Ption (E.getmessage (), E); Finally {try {if (PS! = null) {Ps.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new daoexcept Ion (E.getmessage (), e);} Finally {try {if (con! = null) {Con.close ()}} catch (SQLException e) {//TODO auto-generated catch Blockthrow new Daoexce Ption (E.getmessage (), E);}}}}

Test class

Package Com.jizuiku;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet;import java.sql.sqlexception;/** * * * @author Blog Park-to the most bitter * @version V17.11.09 */public class Demo {public static void main (String [] args) {try {test ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Test () throws SQLException {Connection con = null; PreparedStatement PS = null;try {//Register driver and establish connection con = jdbcutils.getconnection ();//Open transaction Con.setautocommit (FALSE);// Create statement String sql = "Update book set quantity=quantity+10";p s = con.preparestatement (sql);//EXECUTE Statement ps.executeupdate ();// View the effect checkinfluence (con);//Rollback if (true) {throw new RuntimeException ("Start rollback");} Commit Transaction Con.commit ();} catch (RuntimeException e) {if (con! = null) {//Roll-back con.rollback (); SYSTEM.OUT.PRINTLN ("Things Roll Back"); Checkinfluence (con);} The exception is thrown and processed. throw e;} Finally {//frees resource jdbcutils.free (null, NULL, con);}} /** * Used to read the contents of the specified ID record <br/> * The SQL statement executed is: select Id,name,quantity,time, price from book where id=2 * * */public static void Checkinfluence (Connection conn) throws SQLException {Preparedstateme NT PS = null; ResultSet rs = null;//look at the effect of the statement execution String sql = "Select Id,name,quantity,time,price from book where id=2";p s = conn.preparest Atement (sql), rs = Ps.executequery (), if (Rs.next ()) {System.out.println (Rs.getobject ("id") + "" + rs.getobject ("name") + "" + rs.getobject ("quantity") + "" + Rs.getobject ("time") + "" + rs.getobject ("price");}}

Console output Results

Note: The database operation here, be careful.

Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.

Jdbc-mysql Base engine InnoDB is support rollback

Related Article

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.