AutoCAD 3DMAX C語言 Pro/E UG JAVA編程 PHP編程 Maya動畫 Matlab應用 Android
Photoshop Word Excel flash VB編程 VC編程 Coreldraw SolidWorks A Designer Unity3D
 首頁 > JAVA編程

JavaBean中使用JDBC方式進行事務處理

51自學網 2015-09-03 http://www.0532cf.com
  在JavaBean數據庫操作中,一項事務是指由一條或多條對數據庫更新的sql語句所組成的一個不可分割的工作單元。只有當事務中的所有操作都正常完成了,整個事務才能被提交到數據庫,如果有一項操作沒有完成,就必須撤消整個事務。我們通過提交commit()或是回滾rollback()來結束事務的操作。關于事務操作的方法都位于接口java.sql.Connection中。

   在第一次建立與數據庫的連接時,在缺省情況下,連接是在自動提交模式下的, jdbc api中,可以通過調用setAutoCommit(false) 來禁止自動提交事務。然后就可以把多條更新數據庫的sql語句做為一個事務,在所有操作完成之后,調用commit()來進行整體提交。倘若其中一項sql操作失敗,就不會執行commit()方法,而是產生相應的sqlexception,此時就可以捕獲異常代碼塊中調用rollback()方法撤消事務。下面是一個例子;

  比如:有兩張表,一個記錄用戶帳戶(useraccount),一個是系統帳(sysaccount),現在有一個用戶要向系統寄錢買東西,就要此行下面兩條語句:

String sql1 = "update useraccount set monery=monery-1000 where name='username'";
String sql2 = "update sysaccount set monery=monery+1000 where name='sysname'";

  但如果第一條執行了而第二條語句執行出錯了就會造成不良后果。這是就可以用手動提交的方式來防止這種事情的發生:主要代碼

try{
 .
 .
 .
 conn=DriverManager.getConnection("..."); //鏈接數據庫

 conn.setAutoCommit(false);//禁止自動提交事務
 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(); //統一提交。

 }catch(SQLException e){
  conn.rollback(); //倘若其中一項sql操作失敗,就不會執行commit()方法,而是產生相應的sqlexception,此時就可以捕獲 異常代碼塊中調用rollback()方法撤消事務。
  e.printStackTrace();
 }
 finally{
  if(stmt!=null){
   stmt.close();
  }
  if(conn!=null){
   stmt.close();
  }
 }

 

 

 
說明
:本教程來源互聯網或網友上傳或出版商,僅為學習研究或媒體推廣,51zixue.net不保證資料的完整性。
 
上一篇:爭鳴:Java的Web框架 讓我如何去愛你  下一篇:返回列表
彩客网 www.321693.com | www.8499t.com | www.3479u.com | www.66653j.com | www.16878r.com | www.501347.com | www.liuguan000.com | www.07xyc.com | www.52303z.com | www.32123h.com | www.150822.com | www.202115.com | www.150928.com | www.50989h.com | www.23636f.com | www.61655f.com | www.55228h.com | www.29277h.com | www.41518v.com | www.60007f.com | www.15355s.com | www.lu126.com | www.xinyc3.com | www.739366.com | www.635893.com | www.616097.com | www.246841.com | www.76520r.com | www.yi816.com | www.502892.com | www.43131q.com | www.4189d.com | www.5522l.cc | www.621764.com | www.81520l.com | www.68568z.com | www.zg39.com | www.744559.com | www.029781.com | www.84499k.com | www.50788c.com | www.100v.cc | www.071wy.com | www.m.55xj.vip | www.369888aa.com | www.805357.com | www.460689.com | www.25688m.com | www.844689.com | www.91233n.com | www.6364l.com | www.501254.com | www.33588c.com | www.290282.com | www.50064p.com | www.912278.com | www.316106.com | www.8039b.com | www.535666e.com | www.33997c.com | www.hcw018.com | www.00840a.com | www.856657.com | www.235117.com | www.662by.com | www.278965.com | www.202115.com | www.178311.com | www.055aa.cc | www.50064k.com | www.791571.com | www.237544.com | www.9889hc.com | www.80075f.com | www.3890t.com | www.339968.com | www.26163c.com | www.775709.com | www.288hc.com | www.99788b.com | www.c144.vip | www.80767w.com | www.566850.com | www.zuan333.com | www.36788b.com | www.506951.com | www.12455k.com | www.66621i.com | www.gyfc0.com | www.170792.com | www.98528f.com | www.52072o.com | www.645880.com | www.00840c.com | www.295126.com | www.927793.com | www.2875r.com | www.60123t.com | www.444072.com | www.am6543.com | www.3478o.cc | www.8667e.com | www.12455l.com | www.183699.com | www.qbwc5.com | www.35918j.com | www.66332x.com | www.318187.com | www.535666e.com | www.950665.com | www.8970.com | www.25688p.com | www.65707d.com | www.170792.com | www.541711.com | www.cp718.net | www.122by.com | www.022n.cc | www.2109b.com | www.500cd.com | www.3479h.com | www.3890b.com | www.9478a.com | www.33997h.com | www.36166e.com | www.66653d.com | www.73166l.com | www.60007s.com | www.96386k.com | www.178761.com | www.085366.com | www.771245.com | www.843633.com | www.939884.com | www.rcw8844.com | www.ct6676.com | www.889489.com | www.652592.com | www.599846.com | www.cp718.com | www.290282.com | www.969761.com | www.755690.com | www.rcw8855.com | www.757209.com | www.566305.com | www.231844.com | www.90848a.com | www.43131r.com | www.50054n.com | www.87668b.com | www.8667k.com | www.095wy.com | www.df94.com | www.594911.com | www.321693.com | www.65707c.com | www.66509.cc | www.893023.com | www.393477.com | www.506969.com | www.50077c.com | www.8499k.com | www.943893.com | www.526901.com | www.80767u.com | www.73736c.com | www.5086f.com | www.rcw8855.com | www.178795.com | www.95111w.com | www.069wy.com | www.569361.com | www.26299w.com | www.11clf.com | www.339208.com | www.50052f.com | www.022f.cc | www.755860.com | www.50052v.com | www.13czj.com | www.196509.com | www.61233z.com | www.qmkl9.com | www.111306.com | www.26299j.com | www.717226.com | www.67258b.com | www.tj92.com | www.77802v.com | www.088wy.com | www.177804.com | www.25688m.com | www.xinyc5.com | www.71399y.com | www.899495.com | www.23579d.com | www.979233.com | www.66332x.com | www.318548.com | www.622307.com | www.50080t.com | www.770816.com | www.35155a.com | www.901172.com | www.00840i.com | www.782500.com | www.66598b.com | www.684077.com | www.60007s.com | www.594877.com | www.5441n.com | www.353990.com | www.636by.com | www.178281.com | www.fh7557.com | www.67258a.com | www.700539.com | www.50074j.com | www.629683.com | www.16878g.com | www.150822.com | www.hcw018.com | www.77210j.com | www.609815.com | www.222xm.cc | www.50054k.com | www.208990.com | www.967699.com | www.36166x.com | www.139733.com | www.914906.com | www.5854r.cc | www.50074f.com | www.560652.com | www.lc03.com | www.07163r.com | www.96386s.com | www.763829.com | www.2934n.com | www.66653x.com | www.346277.com | www.csw288.com | www.122by.com | www.00840i.com | www.81520n.com | www.398846.com | www.905844.com | www.3126h.com | www.71399p.com | www.90305d.com | www.303861.com | www.xinyc5.com | www.df76.com | www.288hc.com | www.44050.cc | www.54400x.com | www.81520h.com | www.223085.com | www.550421.com | www.823023.com | www.hc8878.com | www.5666hc.com | www.9205a.com | www.68689k.com | www.43131f.com | www.71233v.com | www.202423.com | www.316083.com | www.501203.com | www.501207.com | www.394211.com | www.393411.com | www.378016.com | www.551409.com | www.550164.com | www.586974.com | www.xinyc3.com | www.389544.com | www.218292.com | www.160951.com | www.77802y.com | www.81520l.com | www.50026j.com | www.91233s.com | www.39500m.com | www.cll168.com | www.66ckb.com | www.hx1112.com | www.811675.com | www.648689.com | www.357477.com | www.128368.com | www.66332l.com | www.83033h.com | www.15355d.com | www.bxcp3333.com | www.854242.com | www.526071.com | www.168212.com | www.33598o.com | www.32123l.com | www.588yc.com | www.791361.com | www.338051.com | www.50051s.com | www.01883.cc | www.csw288.com | www.332071.com | www.35918j.com | www.8839q.com | www.996099.com | www.307797.com | www.86267hh.com | www.202115.com | www.810612.com | www.177741.com | www.26878x.com | www.976527.com | www.271902.com | www.99638g.com | www.092wy.com | www.404977.com | www.26299u.com | www.44yfa.com | www.610026.com | www.65707f.com | www.061wy.com | www.550116.com | www.34788g.com | www.fyyy1.com | www.282990.com | www.8667z.com | www.606285.com | www.80065x.com | www.9889hc.com | www.444160.com | www.35918y.com | www.776860.com | www.50054d.com | www.c1393.com | www.81678d.com | www.48wcp.com | www.106367.com | www.bxcp3.com | www.xinyc8.com | www.3890v.com | www.23040.com | www.3416l.com | www.182498.com | www.chi32.com | www.81233n.com | www.30xw.com | www.34788n.com | www.820585.com | www.81520h.com | www.661929.com | www.16588t.com | www.535666e.com | www.588yc.com | www.281877.com | www.983699.com | www.89894s.com | www.517602.com | www.007hy.cc | www.49956b.com | www.698300.com | www.6832e.com | www.87668x.com | www.897030.com | www.66653.com | www.89894e.com | www.682542.com | www.073wy.com | www.33997r.com | www.374311.com | www.gyfc0.com | www.50989h.com | www.101377.com | www.651514.com | www.076wy.com | www.77210j.com | www.178113.com | www.775790.com | www.528qx.com | www.50788r.com | www.127356.com | www.636309.com | www.ovfd2.com | www.59191.cc | www.16878f.com | www.202406.com | www.700539.com | www.fcyl3.com | www.750966.com | www.80767b.com | www.78949b.com | www.332760.com | www.611306.com | www.789733.com | www.fcff0.com | www.1368t.cc | www.cll168.com | www.57578h.com | www.50788y.com | www.025728.com | www.336779.com | www.631012.com | www.707232.com | www.917052.com | www.sytg9.com | www.23czj.com | www.022j.cc | www.3416o.com | www.3890a.com | www.6832z.com | www.5091e.com | www.37124.com | www.39500h.com | www.41518z.com | www.66332z.com | www.60123t.com | www.77803b.com | www.80075m.com | www.88266s.com | www.90305c.com | www.60108b.com | www.12455m.com | www.11303.cc | www.084wy.com | www.hx1114.com | www.901582.com | www.770640.com | www.556742.com | www.355225.com | www.91233g.com | www.33112g.com | www.12455m.com | www.8000hc.com | www.443300a.com | www.801933.com | www.411515.com | www.88266s.com | www.77803t.com | www.3005w.com | www.csw288.com | www.565378.com | www.81233p.com | www.71399m.com | www.070wy.com | www.811030.com | www.371680.com | www.86267p.com | www.88266d.com | www.333xm.cc | www.610390.com | www.106356.com | www.50064s.com | www.055n.cc | www.665609.com | www.84499t.com | www.00840s.com | www.c1636.com | www.331616.com | www.34788z.com | www.bxcp3.com | www.557031.com | www.36788v.com | www.5666hc.com | www.526087.com | www.32123m.com | www.30xw.com | www.178135.com | www.9478o.com | www.680221.com | www.77114u.com | www.cll168.com | www.647311.com | www.35918k.com | www.8000hc.com | www.377501.com | www.51515c.com | www.98528f.com | www.54400v.com | www.88cmc.com | www.196067.com | www.3933v.cc | www.193722.com | www.9958c.com | www.422727.com | www.00840p.com | www.653750.com | www.78949t.com | www.807266.com | www.35918z.com | www.801896.com | www.80075b.com | www.700539.com | www.52072j.com | www.544087.com | www.810837.com | www.83993s.com | www.sjgc9.com | www.61233x.com | www.c5237.com | www.35918u.com | www.809572.com | www.35918x.com | www.345576.com | www.078wy.com | www.55885h.com | www.773925.com | www.7793n.com | www.535666e.com | www.989447.com | www.87668b.com | www.375736.com | www.flff8.com | www.81520k.com | www.514611.com | www.9889hc.com | www.50052k.com | www.524947.com | www.qmkl1.com | www.61233m.com | www.108382.com | www.688824.com | www.500go.com | www.99788j.com | www.318073.com | www.luck808.com | www.2875g.com | www.39500l.com | www.273503.com | www.802133.com | www.1555hc.com | www.16878l.com | www.106572.com | www.595411.com | www.989774.com | www.9889hc.com | www.77801d.com | www.73166e.com | www.281110.com | www.623190.com | www.98528f.com | www.065wy.com | www.00840r.com | www.99788q.com | www.66332r.com | www.192722.com | www.398411.com | www.693119.com | www.913075.com | www.c1360.com | www.11fzc.com | www.788cb.com | www.27676.cc | www.43818t.com | www.81520x.com | www.29277d.com | www.81678b.com | www.081290.com | www.192733.com | www.202703.com | www.202435.com | www.190883.com | www.257188.com | www.324377.com | www.275780.com | www.284488.com | www.130881.com | www.068671.com | www.044592.com | www.082985.com | www.77802q.com | www.50788d.com | www.90305f.com | www.51331j.com | www.35231.com | www.3207.com | www.c6883.com | www.xinyc8.com | www.755860.com | www.587989.com | www.217650.com | www.77210g.com | www.00840w.com | www.33598c.com | www.062wy.com | www.xinyc8.com | www.653355.com | www.389544.com | www.281877.com | www.77802k.com | www.5881.cc | www.xinyc5.com | www.551409.com | www.020724.com | www.33112a.com | www.9149n.com | www.ck9393.com | www.568380.com | www.83993w.com | www.50788w.com | www.ywzuqiu.com | www.679313.com | www.192880.com | www.80767g.com | www.004ac.com | www.718322.com | www.035989.com | www.50066c.com | www.qbwc2.com | www.514977.com | www.50054e.com | www.0014u.com | www.715121.com | www.84499t.com | www.5095e.com | www.717772.com | www.78700k.com | www.3126z.com | www.645477.com | www.61233g.com | www.585hc.com | www.511039.com | www.86267z.com | www.c5617.com | www.186379.com | www.13633.cc | www.589744.com | www.39500k.com | www.hc3656.com | www.068673.com | www.220v.cc | www.361944.com | www.66332s.com | www.910155.com | www.51331c.com | www.hx7766.com | www.89894g.com | www.9356u.com | www.422268.com | www.78700.com | www.578860.com | www.68568t.com | www.631069.com | www.60007k.com | www.629813.com | www.41518q.com | www.607325.com | www.51331m.com | www.550973.com | www.6832w.com | www.235672.com | www.096wy.com | www.77802j.com | www.hw8877.com | www.33997a.com | www.671206.com | www.33588m.com | www.321693.com | www.ztc7.com | www.08588n.com | www.643552.com | www.5854r.cc | www.76520y.com | www.783500.com | www.9149i.com | www.081503.com | www.899621.com | www.67767.cc | www.80767x.com | www.730719.com | www.3478i.cc | www.51331e.com | www.526820.com | www.qmkl1.com | www.61655g.com | www.186783.com | www.811756.com | www.0014q.com | www.32123g.com | www.338072.com | www.luck887.com | www.4189d.com | www.35918v.com | www.296979.com | www.786819.com | www.089wy.com | www.65707x.com | www.lcw997.com | www.695756.com | www.dfyk3.com | www.30601.cc | www.99788n.com | www.99677f.com | www.566217.com | www.918109.com | www.99fzc.com | www.26878n.com | www.33598v.com | www.003675.com | www.370766.com | www.809182.com | www.hj7987.com | www.500pt.com | www.33588s.com | www.99788h.com | www.77801y.com | www.196037.com | www.23040.com | www.602983.com | www.guocai667.com | www.267by.com | www.3479b.com | www.8816s.com | www.35918g.com | www.66653c.com | www.89894t.com | www.26299s.com | www.41518r.com | www.50026w.com | www.51331b.com | www.51331g.com | www.50054a.com | www.50026a.com | www.50052t.com | www.34788l.com | www.16878w.com | www.77801x.com | www.50052s.com | www.83993x.com | www.60007n.com | www.89929.cc | www.4189d.com | www.910xj.com | www.11gpc.com | www.cb2588.com | www.907883.com | www.652776.com | www.420611.com | www.165370.com | www.65707d.com | www.81233z.com | www.07813.com | www.076wy.com | www.979819.com | www.666937.com | www.596112.com | www.bizhong55.com | www.81678z.com | www.05500w.com | www.9155e.cc | www.yy63.com | www.649677.com | www.225095.com | www.50052d.com | www.89929.cc | www.hx1182.com | www.608986.com | www.091865.com | www.80065m.com | www.15010.cc | www.960885.com | www.558610.com | www.88266v.com | www.73166f.com | www.069wy.com | www.868216.com | www.210927.com | www.81678d.com | www.04czj.com | www.611874.com | www.73736d.com | www.8499k.com | www.909888.com | www.hcw823.com | www.50026d.com | www.ch8567.com | www.332790.com | www.83033z.com | www.fcff4.com | www.371951.com | www.77801b.com | www.988456.vip | www.257512.com | www.39500b.com | www.xcw866.com | www.78700c.com | www.982pk.com | www.493552.com | www.97655m.com | www.976115.com | www.97655y.com | www.528qx.com | www.375769.com | www.36788x.com | www.674577.com | www.07163n.com | www.922109.com | www.57578b.com | www.c7268.com | www.80767d.com | www.bai84.com | www.069961.com | www.33ckb.com | www.090392.com | www.78wcp.com | www.106560.com | www.66ffu.com | www.99968z.com | www.hcjt7.com | www.71233s.com | www.777768a.com | www.66652h.com | www.926576.com | www.90305b.com | www.602436.com | www.15199.cc | www.202403.com | www.ya619.com | www.66376m.com | www.910424.com | www.65707d.com | www.569829.com | www.2408e.cc | www.096308.com | www.987580.com | www.16878x.com | www.570477.com | www.7777ae.com | www.76520r.com | www.773618.com | www.8839f.com | www.001562.com | www.870933.com | www.84499.cc | www.106560.com | www.910424.com | www.66870.cc | www.80075l.com | www.xcw866.com | www.055y.cc | www.15355d.com | www.369061.com | www.fcff7.com | www.41518h.com | www.86267u.com | www.599842.com | www.zcwf4.com | www.68689m.com | www.094499.com | www.689144.com | www.22ttz.com | www.53900k.com | www.277474.com | www.701548.com | www.qbwc4.com | www.47370.cc | www.16188a.com | www.131763.com | www.558423.com | www.949430.com | www.096wy.com | www.36166b.com | www.50080c.com | www.197093.com | www.551475.com | www.922109.com | www.01czj.com | www.8667t.com | www.80065z.com | www.66332v.com | www.115779.com | www.401345.com | www.711893.com | www.936370.com | www.5xct.com | www.1035i.com | www.62355.com | www.61233z.com | www.96386f.com | www.34788o.com | www.50732y.com | www.71233l.com | www.040425.com | www.151220.com | www.195090.com | www.239838.com | www.286967.com | www.523780.com | www.303766.com | www.330892.com | www.308139.com | www.296380.com | www.196146.com | www.fh7557.com | www.068673.com | www.87668u.com | www.87668w.com | www.68568u.com | www.50026p.com | www.71233hh.com | www.68568s.com | www.36788z.com | www.8499u.com | www.084wy.com | www.zfcp5.com | www.lcw997.com | www.746133.com | www.547177.com | www.346918.com | www.068652.com | www.39957d.com | www.52303t.com | www.2373b.com | www.qilc7.com | www.xcw866.com | www.506931.com | www.033253.com | www.29178a.com | www.44607.com | www.6ttz.com | www.rcw8855.com | www.371951.com | www.76520j.com | www.60007t.com | www.575hc.com | www.98528f.com | www.332071.com | www.53900f.com | www.36787b.com | www.5xch.com | www.607219.com | www.xinyc2.com | www.26299n.com | www.2350o.com | www.890517.com | www.298980.com | www.80075n.com | www.065wy.com | www.629965.com | www.80075w.com | www.23040.com | www.934277.com | www.550806.com | www.50064k.com | www.080wy.com | www.652361.com | www.71399c.com | www.3552d.com | www.760320.com | www.83033m.com | www.2632a.com | www.622709.com | www.50052g.com | www.097wy.com | www.444083.com | www.78949f.com | www.965900.com | www.003561.com | www.2373g.com | www.576530.com | www.bxcp3.com | www.960777a.com | www.031566.com | www.1888ac.com | www.wcp686.com | www.41518x.com | www.668742.com | www.26299y.com | www.960913.com | www.61233x.com | www.hld22.com | www.83993e.com | www.ya350.com | www.065966.com | www.074wy.com | www.165090.com | www.575hc.com | www.178215.com | www.636by.com | www.099386.com | www.h679.com | www.77114a.com | www.hy5504.com | www.50051u.com | www.811203.com | www.77801b.com | www.587944.com | www.8499z.com | www.luck878.com | www.21czj.com | www.60123s.com | www.876488.com | www.52072j.com | www.412688.com | www.9889hc.com | www.77802l.com | www.tcp969.com | www.60007m.com | www.370766.com | www.89wcp.com | www.43131t.com | www.663738.com | www.3479c.com | www.86267t.com | www.811257.com | www.84040.com | www.178615.com | www.978351.com | www.83033j.com | www.339219.com | www.cp8006.cc | www.60108t.com | www.186957.com | www.895030.com | www.5095k.com | www.60108f.com | www.542511.com | www.igcp4.com | www.53900b.com | www.106569.com | www.696776.com | www.595hc.com | www.77803r.com | www.168232.com | www.666507.com | www.ya350.com | www.33588g.com | www.68689j.com | www.393477.com | www.910576.com | www.083wy.com | www.52303q.com | www.68682b.com | www.335037.com | www.701541.com | www.c6712.com | www.2373p.com | www.61655t.com | www.77114a.com | www.332065.com | www.630565.com | www.949498.com | www.13czj.com | www.7782f.com | www.84499y.com | www.76520i.com | www.178225.com | www.403770.com | www.631257.com | www.868422.com | www.637012.com | www.65yb.com | www.022xj.cc | www.7793v.com | www.36788k.com | www.81678t.com | www.36166t.com | www.65707u.com | www.81678f.com | www.095588.com | www.183676.com | www.243552.com | www.327556.com | www.359629.com | www.393121.com | www.431770.com | www.401271.com | www.500227.com | www.rcw8811.com | www.401271.com | www.383897.com | www.335240.com | www.178225.com | www.96386i.com | www.52303q.com |