虽然在办公室中经常使用,但是平时配置在轮廓上就可以了。 我说不知道也知道,说知道也还不能说,所以不行。 今天简单地写事务demo。
打包传输测试; 导入Java.SQL.connection; 导入Java.SQL.driver manager; 导入Java.SQL.prepared statement; 导入Java.SQL.sqlexception; publicclasstransactionceshi2{ publicstaticvoidmain [ ] args } throws sqlexception {///连接Java.SQL.connection conn=驱动程序管理器. getconnection (' JDBC : MySQL 3360//localhost 33603306/test ',' root/) 更改try//JDBC事务的默认提交方法,以防止自动提交conn.setautocommit(false )。 string SQL=' deletefromuserwhereuserid=' 25 ' and username=? '; pstam=conn.preparestatement (SQL; Ptam.setstring(1,'二无'); pstam.executeUpdate (; //if(1==1) {//throw new Exception (' ); //Conn.rollback (; conn.commit (; }catch(sqlexceptione ) try ) Conn.rollback ); }catch(sqlexceptionE1)//todo auto-generatedcatchblocke1. print stack trace ); }e.printStackTrace (; }catch(exceptione ) {conn.rollback ); } finally { try } conn.set auto commit (true ); }catch(sqlexceptione )//todo auto-generatedcatchblocke.print stack trace ); }//releasetheresourceoftheprogramtry { pstam.close (; conn.close (; }catch(sqlexceptione ) {e.printStackTrace ); } }
其实,事务真的是那么简单,正如现在人们常说的,特别是高分散的事务,只要沉稳简单地写事务,我觉得就没什么别的问题了。 普通事务和分散型事务,就像一开始一个人盖房子,后来小组分工盖房子,保证房子怎么盖。