1、java实验代码104201 柴琪飞实验一:DBC 实践(增删改查)pkage 、sh、domin;blic clss Perso priva in ;pivteStrig nam; pate n ag; pivate String p; publi Person()/构造函数 su(); ulso(it j,Srin stin1, it i, Sin string2) ths、i = j; h、nae= string1;thi、age = ; this、pre = tng2; ubli ingtI() reu id;publicoi sI(in i) this、id =id;public S
2、trng etame() retun name; plic id etNm(tig ne) this、nam =name; public nt gtAge() retrn age; pli voidseAge(it ae) his、ge=ag; public Sring ePr() retrn pre; blic void stPe(Srin pe) his、r =r; verrde ulic String totng() reurn rso d + d+ ”,ne + nae + , ge= + age + , re= pe + ”; acage 、si、dao;imor aa、sql、SQ
3、LException;io jav、til、Lt;iort、shi、doain、;publc interface ProDa publi void add(eson p) thws SLcepion; ublic void update(Person p)throw SQLExetio; ulc vodelete(inti)thrs SQLExceptn; publcPern findyId(int id)throwsSLExcepti; pulic st finAl() ho QLxcepin ting sl =slet id,ne,ae,pre fromstnt”; retun (ist)
4、this、jdcTemplet、query(sq, ew ResulteHandler() ulic Objet ander(esultSe rs)hows SQLxepo fin Lisperns = new rrLserson(); Pesonp =null; ile(rs、next()) p= ne Person(); 、tI(rs、etIn(1)); p、sNam(rs、geStrng(2)); p、stAge(rs、geIn()); p、sePr(rs、ettin(4); peso、dd(); etur persos; );pakage 、shi、bas;imprt va、sql、R
5、esultSet;mpot java、sq、SQEception;plc nrface ResltetHaler pulic Oject doHadle(ReltSt rs) trows QLEcepto;pacae、util、db;iprtjaa、sq、*;imprt、mysql、dbc、Connecton;mprt ja、il、sourceundle;publi las Dtlds ublc fina sti String UR;pblic fnal stictrng USERAME;pubc fial tatic Strin PSSWOR ;publ inal satic StrinDR
6、IVR;privae Utilds() 定义私有构造方法 /利用绑定prvate acRsueBndl = RsoBnle、geBudl(、uti、db、dbonig”); static UL = rb、gettig(jd、ur”); USERNAE = rb、getStri(db、usernme); ASWORD= b、gettig(b、psswrd); DRIR = b、gtStrg(dbc、diver); r Class、rName(DIVER); cth (lasNotFoudExcepion) e、pintSackTrce(); /*satic /使用静态块加载驱动程 try ass
7、、orNa(DRVER); cach(lssNtFoundExcetin e) /ODO teneratd ach blck 、rinttckTrac(); /ulic tatic Conctin getConnctn()/连接 Contion conn nul; try on (Conecton) Drieranager、getConnection(UL, USERNAME,PASSD); ctch(SQLxcption) /ODO ugeneratd cachblok e、pinStckace(); Sym、out、print(sbai); reurn conn; ubi static o
8、id coe(esutSet r,tamn sae,Connectio on)/关闭try i (rs!nul) rs、cos(); if (st!=nul) ae、cloe(); f (onn!=ull) on、close();atch (SQLExcepon e) / TODO utenete ctc loc e、pntSackTrce(); ckage 、util、b;importva、sql、esute;imort jaa、ql、QLExcet;iport 、tl、DBU;port 、mysql、jdbc、Concton;imprt 、msql、jdbc、eparStemen;impr
9、t 、hi、bse、RsulStHandler;pblc clsdbTeml pblit pate(Strig ql, Objet、as)thros SExcetion/在JbcTmplet类 nction conn = nul; epardStteent ps =nll; tryconn BUtilds、geConncion();ps = (PreparedSttement) conn、paretatmet(sql); /设置占位符得参数 i(g!ull) for(nt i= 0; rs、legt;+) ps、etObject(+1,gsi); retur ps、xeutUde();cac(
10、SQExceton e) e、piStakTrace(); reun -1; finaly DBUtilds、clos(null, ps, conn); public bjctquer(trng sl,eultSetHandler handlr,Objet、ars)throw SLExeptoonnecti conn = null; rpareStaemnt p = nl; RsultSt rs = nul; try n DBtilds、getontion(); ps=(PredStteme) conn、prepreStatemn(ql); f(arg!=nl) for (t i = 0; %
11、Srgpath= ruest、geonextPah();String basePath= equst、getSchee()+”:/”+eqes、eServerName()+:+request、etServerPor()+path+”; hea basehre=basePath taequiv=pragmaconet-cce mea euv=”cache-cntrol”cnn”ocache ea-equiexps cntet”0” met quiv=eywscontent=keywrd1,keword2,keyod3”mta eqdescrito”cntentThis iypae !-lin r
12、el=”styeshet typ=text/ss href=syes、cs”- stylepe = ”xt/c -si:5p; textdecoato:none; colo:red; a:hovr oor:blue; bacgrouncolor:#fffff;stle ea ha hrf ”ist、”查瞧所有学生信息/tllst、jp pae lngge=java impoja、til、*” geEncoding=”U8”%pag imort=、si、min、Peson” pag ipor=”、ui、DBUtils %g mpot=”、util、db、JdcTempte %pg ipr=”jv
13、a、utl、is %!DCTPEHTL PBIC-/3/DTD TML 4、Trastional/ENht ead bae ref=bsPat% mta -euiv=achecnrocntn=nocema quiv=xpies ntnt0” mea iv=kywod content=keywr1,keord,keyword- ronDo da n eronDaoIpl(); LisPrs prs dao、findl(); 学生信息列表 添加学生信息/a tal borer = ”1 t th编号/th t年龄/th th描述 th操作/th /tr % for(int =0;i %p、getNam() %/ td=p、getge() /td t%=p、gPre() %/td t a hrf= udt、jsp?id=修改/a a hre
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1