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.77c668.com | 39199k.com | www.822822.com | www.627069.com | 08159n.com | www.520195.com | www.hj003.com | www.627069.com | LXYL352.com | www.7240i.com | feicai0892.com | www.cs55222.com | 500500.com | yt8801.com | www.55070k.com | 56988k.vip | www.4972ss.com | www.5522i.cc | u08199.com | www.ao799.com | www.hr1866.com | 9679r.com | www.dzcp7777.com | yhneimenggu.vip | www.hg3569.com | www.95wcp.com | 7792u.com | www.997263.com | 78666j.com | www.hm6622.com | 4465n.com | www.848777n.com | 00333885.com | www.063sunbet.com | www.596531.com | 3544e.com | www.57578h.com | 50099z.com | www.12136z.com | 4694b.com | www.9b800.com | rtdqg.cn | www.208178.com | www.66332v.com | www.39695t.com | www.862455.com | 8742c.com | www.91233z.com | 3559xx.com | www.3399nsb.com | zhcpuu.com | www.4136j.com | 22117m.com | www.1705.com | 83086l.com | www.99113o.com | yh77.com | www.5446qq.com | 32212s.com | www.c668.cc | 8827jjj.com | www.19019b.com | 3522n.vip | www.58118h.com | hj4566com | www.5146z1.com | a61653.com | www.5504ii.com | 567zcp.com | www.50999i.com | 01572.com | www.9966.so | 93888js.com | www.4bet005.com | 80368cc.com | www.yh234f.com | 0683g.com | www.12136i.com | lll3405.com | www.1033.app | 2418m.com | www.3890j.com | 22207t.com | www.756398.com | www.0279003.com | yz5388.com | www.4567749.com | 1434b.com | www.8865tt.com | 9339999t.com | www.12015.cc | www.xh5111.com | www.558402.com | www.hg8259.com | 5200e.com | www.3157t.com | www.pjzxyl.com | 9949t.com | www.5220666.com | 145301.com | www.hg27388.com | 4997c.com | www.791637.com | www.xx9090.com | www.1688hg.com | www.5086x.com | www.28758n.com | www.037211.com | www.kj639.com | aaa5701.com | www.84499b.com | www.e26055.vip | www.377666c.com | yyh917.com | www.00840d.com | www.wn285.com | d5959.com | www.hg8993.com | 77606m.com | www.42042.cc | www.25288q.com | 35252q.com | www.45598k.com | 8547u.com | www.799657.com | www.47bm.com | 2018385188.com | www.900845.com | www.678130.com | 55957k.com | www.49956c.com | www.k22365.com | 8381kk.com | www.78919a.com | www.199228.com | 73999r.com | www.xpj55123.com | www.803244.com | www.2544k9.com | 53262ss.com | www.ycu8.com | www.2945m.com | 866666m.com | www.701651.com | www.9377msc.com | 3778hh.com | www.672566.com | www.448965.com | mg887788.com | www.68568v.com | www.77mgm777.com | 3121v.com | 0747oo.com | www.1764h.com | www.lbj682.com | aipin66.me | www.ihg5599.com | www.xpj88004.com | 461653.com | www.ya248.com | www.932285555.com | yhjilin.vip | www.362803.com | www.biying980vip.com | 8790d.com | 35252d.com | www.7239v.com | www.yh1618.com | r1458.com | www.332069.com | www.357107.com | 97297b.com | 115505.com | www.55885h.com | www.39678.com | 44kk8332.com | 2648.com | www.9356c.com | www.qmc0055.com | www.hg98029.com | 3614f.com | www.339967.com | www.06386666.com | www.yh04567.com | 8722gggg.com | www.407873.com | www.63606j.com | www.hg2968.com | 8016aa.com | www.544949.com | www.30350r.com | www.21365vv.com | 71707i.com | www.71233o.com | www.9646g.com | www.233449.com | 500000550.com | 99225156.com | www.652932.com | www.vnsr2345.com | www.js5666.com | ddcp77.com | xpj99.am | www.564658.com | www.flb905.com | www.778849.com | 3844bb.com | 44488c.com | www.783802.com | www.8905i.com | www.06982.com | 33p1.cc | 6688xpj.com | www.914906.com | www.535081.com | www.8966kk.com | 500000595.com | 500000357.com | www.338031.com | www.226467.com | www.8k315.com | www.692tyc.com | v8738.com | 4023b.com | www.810917.com | www.9187t.com | www.667686.com | www.81866v.com | 55967t.com | 1665v.com | www.576120.com | www.256818.com | www.c44xx.com | www.f32126.cc | 2012vip66.com | 644237.com | www.330627.com | www.61233g.com | www.1429a1.com | www.44488.cc | www.wnsr848.com | 2jsttt.com | pt7008.com | bj499.com | www.976271.com | www.68993233.com | www.63606g.com | www.cntwsm.com | www.hg313.com | nn5443.com | 8037h.com | 88052.net | www.377183.com | www.50989h.com | www.81619.com | www.mng55.com | www.32666o.com | 54140077.com | 7720b.net | 33395757.com | xpjok.com | 1483nn.com | www.rcw789.com | www.022j.cc | www.115107.com | www.1754x.com | www.9925546.com | www.lhcbm.com | www.649589.com | www.86611l.com | 78118877.com | 01885s.com | 6q604.com | 61320099.com | 2613k.com | 5360qq.com | www.320992.com | www.cp8009.cc | www.36788d.com | www.8582ee.com | www.792083.com | www.15k0048.com | www.1168s.com | www.awwv.cc | www.0153.com | www.www-36222.com | www.bet365608.com | 88021a.com | 500000756.com | 2349002.com | 550111b.com | 8988uu.com | 30175555.com | u8381.com | app22299.com | 69441199.com | yyy4255.com | x444000.com | 0907hb.com | 8381nn.com | 888.cm | 8988bb.com | 3mgmvvv.com | www.5446kk.com | www.22c25.com | www.3a006.com | www.hg6222.com | www.612210.com | 66300vip11.com | 8159ppp.cc | www.9130.com | www.79900.com | www.0600q.cc | www.973776.com | 3556vip6.com | 3846o.com | www.ao399.com | www.998855v.com | www.80030077.com | www.558402.com | 6789blr.biz | dz883.com | www.wns0028.com | www.bwinyz06.com | www.1368z.cc | bojue009.com | 2844j.com | www.302848.com | www.7240h.com | www.568001.com | 1859004.com | mgm3242k.com | www.000435.com | www.087i.com | www.289971.com | 3454777.com | www.dfs998.com | www.998855c.com | www.582010.com | targe889q.net | 0206.com | www.xpj3398.com | www.15365x.com | www.371f.cc | 7552.com | 26444a.com | www.177572.com | www.71233c.com | 2418.com | 9355ema.com | www.xpj9259.com | www.hr1811.com | www.327536.com | 6556pk.com | www.87680a.com | www.2997773.com | www.500232.com | gf808.com | www.6677msc.com | www.xpj11885.com | www.342918.com | 36406677.com | www.hg3373.com | www.2418l.com | 6613699.com | p5555.com | www.76775c.com | www.80075m.com | 4346.com | vns66666.cc | www.673888j.com | www.689044.com | 83138m.com | www.y2205.com | www.6678568.com | laok000.com | 0747i.com | www.0057b.com | www.c5986.com | 013373.com | www.474.net | www.087u.com | 11tt8331.com | 6220ii.com | www.hg8ff.com | www.420611.com | rbbet88.com | www.65579b.com | www.hx6697.com | 69448866.com | www.9289988.com | www.41518.com | 51133hhh.com | www.0099lll.com | www.81678p.com | 4880r.com | www.717xpj.com | www.7240q.com | 3189ee.com | 22296ae.com | www.861002.com | bc21888.com | 5446kk.com | www.467155.com | 22234066.com | 5002yyy.com | www.2997709.com | 4774s.com | www.gh0029.com | www.siji05.com | 2127b.com | www.v32126.cc | www.xj07949.com | 59599v.com | www.272875.com | www.89893.cc | 8159kk.cc | www.hjcp222.com | www.599047.com | 3016sss.com | www.45598c.com | 66300vip50.com | www.sjg500.com | www.997286.com | 90307w.com | www.1113890.com | www.hc8858.com | hd8449.com | www.4694e.com | 16690088.com | www.hg1233.com | www.xy52.vip | 777hg111.com | www.09888cp.com | www.61655v.com | www.0686777.com | www.530.cc | 69441199.com | www.27363q.com | c388p.com | www.28758z.com | www.51515r.com | 5856o.com | www.xpj556677.com | 333235.com | www.h7788b.com | www.2373g.com | 009.so | www.32638.com | 11018x.com | www.g78306.com | www.129985.com | www.7168803.com | www.50732s.com | jj01234.com | www.79095m.com | js89i.vip | www.870000.com | 36508w.com | www.7830q.com | www.916720.com | 3089m.com | www.944990.com | 550111b.com | www.30350r.com | 1654zz.com | www.jy959.com | 33432f.com | www.hg22331.com | www.560758.com | www.8520f.com | www.66026.cc | 3245g.com | www.0270l.com | 29918f.com | www.68365g.com | q45638.com | www.b3410.com | 1397.com | www.v00888.com | 8159mm.cc | www.gt111999.com | 45888c.com | www.138cpj.com | P35zz.com | www.449555.com | 4997t.com | www.842211.com | 5804z.com | www.346788.com | 2934q.com | www.mailebi.com | 3178e.com | www.h1765.com | 35222c.com | www.68277666.com | 7508f.com | www.6668ylg.com | 5003ppp.com | www.4996wh.com | 6396x.com | www.4546900.com | 3550r.com | www.pj9010.com | 4052p.com | www.113744.com | 2306k.com | www.3333y.cc | www.94911.cc | www.378015.com | www.28481166.com | 2078k.com | www.drf0876.com | blhvip9.com | www.bwinyz08.com | 5478.com | www.954321l.com | w35151.com | www.2222y.cc | www.3657e.com | www.178326.com | www.335503.com | 3559g.com | www.7415z.com | 0644.com | www.13434c.com | 2649s.com | www.cp500.in | www.06797x.com | xifuny.com | www.1168q.com | 2778.com | www.939509.com | 748v.com | www.278202.com | www.49487.com | 3890z.com | www.707727.com | r35151.com | www.755213.com | www.658226.com | zxd2229.com | www.60886i.com | 8547ff.com | www.951077.com | www.77537b.com | 3559g.com | www.4521dh.com | 55797f.com | www.960121.com | www.vip8xpj.com | 00116556.com | www.1434c.com | zhcp91.com | www.357256.com | www.511289.com | 83378t.com | www.66621d.com | www.yh8216.com | 9411zzz.com | www.887767.com | 3121ee.com | www.552703.com | www.55537.com | sjg002.com | www.15355f.com | www.u30226.com | long7988.com | www.ks2.com | 9001343.com | 0008c.com | www.huangma21.com | 76886q.com | www.514977.com | www.yw5.com | e99345.am | www.hw8877.com | www.kk7788.com | 0715563.com | www.3933k.cc | www.hg6058.com | 1813dd.com | www.6364w.com | www.88gew.com | 889q.net | www.055h.cc | www.kk7788.com | 6082.com | www.936370.com | www.yinhe400.com | 80368d.com | www.436106.com | www.6696877.com | z92266.com | www.081307.com | www.06820n.com | 2649e.com | 51200uu.com | www.9971i.com | www.9591718.com | 4508ww.com | www.50066c.com | www.9289988.com | 86226n.com | www.799425.com | www.8839zz.com | qycp05.com | 6766rr.com | www.692021.com | www.3459k.com | 88894f.com | www.068wy.com | www.tai444.com | 8977uu.com | hlw1177.com | www.hr1811.com | www.777444q.com | 4775544.com | www.898364.com | www.sb5509.com | 3178jj.com | 83086w.com | www.52303f.com | www.643sunbet.com | tzvip2026.com | 35ff.vip | www.vn888345.cc | www.377088.com | 3550t.com | 52688h.com | 3568m.com | www.80065t.com | www.168345.com | yabo692.com | 3156www.com | www.96386d.com | www.777540.com | 28839b.com | m333999.com | www.50051b.com | www.2266608.com | 55rr8332.com | www.0014v.com | www.yk222c.com | sun7766.com | 3566nn.com | www.632590.com | www.758908.com | www.25288i.com | 88850bb.com | 8988i.com | www.68682q.com | www.f4042.com | xpjbak.com | 4880w.com | www.376519.com | bwin8q.com | aa4675.com | www.c134.vip | www.40288e.com | www.hg7111.com | hwcp22.com | js14n.com | www.912278.com | www.vnsr2345.com | www.js7896.com | 6245y.com | 138.am | www.730625.com | www.y63568.com | www.893333.com | 3016ddd.com | js2.cc | 1389k.com | www.5086r.com | www.yh234h.com | www.a80070.com | feicai0757.com | ahhsxj.com | www.178215.com | www.66653x.com | www.89599k.com | www.88837y.com | 0747v.com | 85698c.com | www.50026i.com | www.9818f.cc | www.40288m.com | www.797077.com | hgw168g.com | 8037mm.com | 01234sss.com | www.567306.com | www.77016.net | www.hg8pp.com | www.3730-251.com | 80892tt.com | 7599z.com | 8294m.com | www.342977.com | www.29178c.com | www.w84h.com | www.9788t.com | www.9996ll.com | 22296jj.com | v444000.com | 7779964.com | www.536350.com | www.54400m.com | www.88325o.com | www.15786.com | www.9068rr.com | 2544b7.com | 4426123.com | 37570z.com | www.78949v.com | www.hm2555.com | www.500166.com | www.00778q.com | www.9906132.com | www.h067.com | 3089o.com | mg4377.com | www.893023.com | www.97655d.com | www.vnsr4567.com | www.y456x.com | 2618g.com | 11aa8332.com | www.506729.com | www.999xm.cc | www.ya2019l.com | www.89155444.com | www.958443.com | www.hg8562.com | www.26777.cc | 88903838.com | 39830.com | 7555.com | 2941122.com | 2757x.com | 4048eee.com | www.003561.com | www.769211.com | www.5095c.com | www.4923t.com | www.3846aa.com | www.0002737.com | www.44c668.com | www.52564.com | www.25288r.com | www.333222n.com | 3834504.com | ff555l.com | 62222q.com | 3676vip.com | 809h20.com | ll1915.com | 22117p.com | sss01234.com | 2013880.com | 2618s.com | www.053059.com | www.524277.com | www.766898.com | www.c805.cc | www.2109k.com | www.26163l.com | www.86267l.com | www.224975.com | www.hg0335.com | www.1434z.com | www.12136d.com | www.j32031.com | www.630611.com | www.x69096.com | www.4136r.com | www.55545p.com | www.039907.com | www.56655d.com | www.18765.com | www.v56789.com | www.vic040.com | www.y0090.com | www.14988.com | www.bmw0004.com | www.1828338.com | www.bct5123.com | www.447858.com | www.gssuu168.com | www.5966zzz.com | www.468546.com | www.yzcp2023.com | www.19019v.com | www.5981e.com | www.35252a.com | www.30350l.com | www.86339w.com | www.86333c.com | www.sdsy.com | www.60108f.com | www.7793h.com | www.bxcp3333.com | www.876879.com | www.599290.com | www.223075.com | n888go.com | t365r.com | 3473p.com | 983888d.com | 2222k04.com | 50029900.com | jl55555.com | 88021a.com | www.617706.com | www.25288g.com | www.026555.com | www.bw9988.com | www.4136q.com | www.33pjjt.com | www.7969.com | www.50080m.com | www.fyyy6.com | www.511647.com | 54141144.com | 7779e.cc | 7742ww.com | hbs107.com | f35151.com | www.kim218.com | www.537msc.com | www.03tyc.com | www.26123jj.com | www.7920y.com | www.84499n.com | www.hy6936.com | www.338057.com | 4992535.com | 47749s.com | 4557.com | 588rrr.cc | www.39695a.com | www.88597.com | www.j92776.com | www.12136v.com | www.71399s.com | www.890482.com | www.50080v.com | 80850v.com | 51133ii.com | 2643r.com | www.a17848.com | www.113470.com | www.xpj16683.com | www.77537.com | www.c6167.com | 159666j.com | hg28.com | yz5488.com | www.ylg777555.com | www.941289.com | www.163a2.com | www.428.com | www.sxyl6.com | 3131.cc | o4255.com | 8722eeee.com | www.2090922.com | www.11ledian11.cc | www.3w28.com | www.3126l.com | www.081502.com | 38238y.com | 62222p.com | www.vns0488.com | www.5966bbb.com | www.n94600.com | www.wct3.com | s444000.com | 52520524.com | 2846b.com | www.47272x.com | www.4996jc.com | www.60108c.com | www.223075.com | 1413.com | 9895n.com | www.1829q.com | www.673888c.com | www.77802h.com | www.58gc8.com | 3hg7788.com | 6e604.com | www.pj9075.com | www.89677z.com | www.26299.com | hr499.com | 8hga.com | www.bm10000.com | www.009789.com | www.6547.com | www.606920.com | 67877g.com | feicai0559.com | www.mm2229.com | www.69567d.com | www.c2809.com | 390955.com | t8705.com | www.6861767676.com | www.0041331.com | www.3478s.cc | 21866g.com | 80368tt.com | www.yh33566.com | www.xpj5.net | www.808762.com | ub66.net | zhcpxx.com | www.581777.com | www.22062b.com | 56987zz.com | 0166556.com | www.8694p.com | www.2021q.com | www.cp8006.cc | 05075555.com | j8159.cc | www.c44qq.com | www.18399.com | 915906545.com | 67877r.com | www.01416.com | www.sx1822.com | m0084.com | 73055l.com | www.15960000.com | www.5856877.com | www.073552.com | 3656vv.com | www.537msc.com | www.33668.com | www.183799.com | 40033f.com | www.6861767676.com | www.7920o.com | www.202403.com | 53262x.com | www.pj5767.com | www.55717k.com | www.41518c.com | w8381.com | www.39266.com | www.0270m.com | bcylc22.com | xpj58058.com | www.rycp157.com | www.qbwc1.com | 40661114.com | www.vns518.com | www.2848ddd.com | www.191079.com | hbs107.com | www.134222.com | www.80175.com | jss1150.cn | www.81866i.com | www.33111166.com | www.424221.com | 888x2.com | www.67297.com | www.32123s.com | 82997.co | www.654bet.com | www.hg7122.com | 2381rrr.com | 3121p.com | www.c30666.com | www.411515.com | 55545k.com | www.bet66622.com | www.am5432.com | 075nn.com | www.6888tm.com | www.37022.cc | qq365s.com | www.kelake88.com | www.96386h.com | 2245dl.com | www.777444q.com | www.36585555.com | 11018r.com | www.25673c.com | www.5952122.com | 7196ss.com | www.jsth518.com | www.1434h.com | h99345.am | www.38200i.com | www.62778855.com | 1077zz.com | www.65358.com | www.035647.com | 914907.com | www.58777z.com | www.60123c.com | 123456yy.cc | www.408409.com | www.25czj.com | 3957q.com | www.tyc002.com | www.622209.com | 32126t.net | www.00773d.com | www.51515u.com | hpzx88.com | www.00xpj58.com | 2381lll.com | www.7893w41.com | www.81233r.com | 85850.com | www.3814b.com | www.744343.com | 62222k.com | www.80188h.com | 8905h.com | www.tyc775.com | www.91233f.com | 0234vv.com | www.bai3355.com | www.330291.com | ly88888824.com | www.amjs3456.com | 9484H.com | www.600w04.com | www.924377.com | u86811.com | www.77789yh.com | 2618i.com | www.xg0088z.com | www.c147.vip | HGW3300.com | www.5719.com | feicai0790.com | www.1600yy.com | www.753956.com | 6830dd.com | www.x4449.com | 00442015.com | www.8722msc.com | www.81678v.com | www.7435e.com | www.07163s.com | 888333.com | www.j83377.com | 234915.com | www.cp48.com | www.335214.com | www.yh6001.com | www.35155x.com | 3522a.vip | www.8c777.com | 2019.cc | www.3846m.com | 55331j.com | www.09569r.com | www.806875.com | 33115jj.com | www.15355e.com | 3435s.com | www.0007795.com | 1222r.com | www.938a.cc | 7933311.com | www.y186x.com | www.389544.com | www.6491f.com | www.c6127.com | 009900.com | www.61655y.com |