欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement13855.docx

    • 资源ID:68854198       资源大小:921.49KB        全文页数:19页
    • 资源格式: DOCX        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement13855.docx

    Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03.JDBC Statement(1)上季我们建建立了与数数据库的连连接,本季季我们将对对数据库进进行操作使使用Staatemeent 执执行DDLL、使用SStateementt 执行DDML,常常用的插入入、更新、删删除及查询询数据哈 Staatemeent 简简介Stattemennt 提供供了一个操操作数据库库语句的功功能,可通通过它来创创建表、插插入记录、修修改记录、删删除记录等等操作 获得SStateementt可以从数数据库连接接Connnectiion中获获得StaatemeentConneectioon coonn = neww ConnnecttionUUtil().geetConnnecttion();     Sttatemment stmtt = cconn.creaateSttatemment();ConneectioonUtiil.jaavapackaage ccom.mmichaael.jjdbc;     impoort jjava.sql.Connnectiion;     iimporrt jaava.ssql.DDriveerMannagerr;     impport javaa.utiil.Prroperrtiess;     pubbliccclasss ConnnecttionUUtil                /第一种种方法               puublicc Connnecttion getCConneectioon()                         Connnectiion cconn = nuull;                         ttry                                    /Cllass.forNName加加载驱动                                   CClasss.forrNamee("coom.myysql.jdbcc.Driiver"");                                   /DrivverMaanageer获得连连接                                   connn = DrivverMaanageer.geetConnnecttion("jdbbc:myysql:/loocalhhost:33066/jdbbc_dbb","rroot"","myysqlaadminn");                                   rreturrn coonn;                          cattch (Exceeptioon e)                                    e.prinntStaackTrrace();                                                  rreturrnnulll;                              /第二种种方法               puublicc Connnecttion getCConneectioon(Sttringg driiver,Striing uurl,SStrinng usser,SStrinng paasswoord)                         Connnectiion cconn = nulll;                         trry                                    /Claass.fforNaame加载载驱动                                   Cllass.forNName(drivver);                                   /DrriverrManaager获获得连接                                   cconn = DrriverrManaager.getCConneectioon(urrl,usser,ppasswword);                                   retturn connn;                          ccatchh (Exxcepttion e)                                    e.prrintSStackkTracce();                                                  retturnnnull;                              /第第三种方法法               publlic CConneectioon oppenCoonnecctionn()                         SStrinng drriverr = """                         Sttringg urll = """                         Sttringg useer = ""                         SStrinng paasswoord = ""                         Proppertiies pprop = neew Prroperrtiess();                         CConneectioon coonn = nulll;                         trry                                    /加载属属性文件                                   pprop.loadd(thiis.geetClaass().gettClasssLoaader().geetRessourcceAsSStreaam("DDBConnfig.proppertiies");                                   drriverr = pprop.getPPropeerty("driiver"");                                   urrl = propp.gettProppertyy("urrl");                                   userr = pprop.getPPropeerty("useer");                                   passswordd = pprop.getPPropeerty("passsworrd");                                   /Cllass.forNName加加载驱动                                   CClasss.forrNamee(driiver);                                   /DDriveerMannagerr获得连接接                                   connn = DDriveerMannagerr.gettConnnectiion(uurl,uuser,passswordd);                                   reeturnn connn;                          catcch (EExcepptionn e)                                    e.pprinttStacckTraace();                                                 rreturrnnulll;                     TestSStateementt.javvapackaage ccom.mmichaael.jjdbc;     impoort jjava.sql.Connnectiion;     iimporrt jaava.ssql.SSQLExxcepttion;     impoort jjava.sql.Stattemennt;     puubliccclasss TeestSttatemment                pubblicsstatiicvoiid geetStaatemeent()                         Connnecttion connn = nnew CConneectioonUtiil().opennConnnectiion();                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   Syystemm.outt.priintlnn(stmmt);                          cattch (SQLEExcepptionn e)                                    / TODOO Autto-geeneraated catcch bllock                                   ee.priintSttackTTracee();                                              Main.javaapackaage ccom.mmichaael.mmain;     impoort ccom.mmichaael.jjdbc.ConnnectiionUttil;     iimporrt coom.miichaeel.jddbc.TTestSStateementt;     pubbliccclasss Maiin                /*                * paaram argss                */               ppubliicstaaticvvoid mainn(Strring arrgs)                          ConnnecttionUUtil cu = neww ConnnecttionUUtil();                         /第一种方方法                         Sysstem.out.prinntln("第一种种方法:""+cu.getCConneectioon();                         /第第二种方法法                         Systtem.oout.pprinttln(""第二种方方法:"+cu.ggetCoonnecctionn("coom.myysql.jdbcc.Driiver"","jdbbc:myysql:/loocalhhost:33066/jdbbc_dbb","rroot"","myysqlaadminn");                         /第三三种方法                         SSysteem.ouut.prrintlln("第第三种方法法:"+ccu.oppenCoonnecctionn();                         TesttStattemennt.geetStaatemeent();                     测试结果: 使用SStateementt 执行DDDL可以使用用Stattemennt来执行行一个数据据定义语句句,例如:创建一张张表TestSStateementt.javvapackaage ccom.mmichaael.jjdbc;     impoort jjava.sql.Connnectiion;     iimporrt jaava.ssql.SSQLExxcepttion;     impoort jjava.sql.Stattemennt;     puubliccclasss TeestSttatemment                pubblicsstatiicvoiid geetStaatemeent()                         Connnecttion connn = nnew CConneectioonUtiil().opennConnnectiion();                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   Syystemm.outt.priintlnn(stmmt);                          cattch (SQLEExcepptionn e)                                    / TODOO Autto-geeneraated catcch bllock                                   ee.priintSttackTTracee();                                                       pubblicsstatiicvoiid crreateeTablle()                         /DDDL数据定定义语句                         CConneectioon coonn = neww ConnnecttionUUtil().oppenCoonnecctionn();                         SStrinng sqql = "creeate tablle CuustommerTbbl(idd intt priimaryy keyy autto_inncremment,namee varrcharr(20),emaail vvarchhar(220)""                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   /执行SQQL语句                                   sstmt.execcute(sql);                          ccatchh (SQQLExcceptiion ee)                                    / TOODO AAuto-geneerateed caatch blocck                                   e.pprinttStacckTraace();                         fiinallly                                   iff(connn!=nnull)                                             try                                                        connn.cloose();                                              ccatchh (SQQLExcceptiion ee)                                                        cconn = nuull;                                                       ee.priintSttackTTracee();                                                                                           Main.javaapackaage ccom.mmichaael.mmain;     impoort ccom.mmichaael.jjdbc.ConnnectiionUttil;     iimporrt coom.miichaeel.jddbc.TTestSStateementt;     pubbliccclasss Maiin                /*                * paaram argss                */               ppubliicstaaticvvoid mainn(Strring arrgs)                          ConnnecttionUUtil cu = neww ConnnecttionUUtil();                         /第一种方方法                         Sysstem.out.prinntln("第一种种方法:""+cu.getCConneectioon();                         /第第二种方法法                         Systtem.oout.pprinttln(""第二种方方法:"+cu.ggetCoonnecctionn("coom.myysql.jdbcc.Driiver"","jdbbc:myysql:/loccalhoost:33306/jdbcc_db"","rooot","myssqladdmin"");                         /第三种种方法                         Syystemm.outt.priintlnn("第三三种方法:"+cuu.opeenConnnecttion();                         TTestSStateementt.gettStattemennt();                         TesttStattemennt.crreateeTablle();                     测试结果:  使用SStateementt 执行DDML更新 Inssert TestSStateementt.javvapackaage ccom.mmichaael.jjdbc;     impoort jjava.sql.Connnectiion;     iimporrt jaava.ssql.SSQLExxcepttion;     impoort jjava.sql.Stattemennt;     puubliccclasss TeestSttatemment                pubblicsstatiicvoiid geetStaatemeent()                         Connnecttion connn = nnew CConneectioonUtiil().opennConnnectiion();                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   Syystemm.outt.priintlnn(stmmt);                          cattch (SQLEExcepptionn e)                                    / TODOO Autto-geeneraated catcch bllock                                   ee.priintSttackTTracee();                                                       pubblicsstatiicvoiid crreateeTablle()                         /DDDL数据定定义语句                         CConneectioon coonn = neww ConnnecttionUUtil().oppenCoonnecctionn();                         SStrinng sqql = "creeate tablle CuustommerTbbl(idd intt priimaryy keyy autto_inncremment,namee varrcharr(20),emaail vvarchhar(220)""                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   /执行SQQL语句                                   sstmt.execcute(sql);                          ccatchh (SQQLExcceptiion ee)                                    / TOODO AAuto-geneerateed caatch blocck                                   e.pprinttStacckTraace();                         fiinallly                                   iff(connn!=nnull)                                             try                                                        connn.cloose();                                              ccatchh (SQQLExcceptiion ee)                                                        cconn = nuull;                                                       ee.priintSttackTTracee();                                                                                                    /DML数数据操作语语句-CCRUD:creaate、rretriive、uupdatte、deeletee               publlicsttaticcvoidd tesstInssert()                         /DDL数数据定义语语句                         Connnecttion connn = nnew CConneectioonUtiil().opennConnnectiion();                         Strring sql = "iinserrt innto CCustoomerTTbl(nname,emaiil) vvaluees('MMichaael','micchaelljavva.coom')""                         tryy                                    Sttatemment stmtt = cconn.creaateSttatemment();                                   /执行SQQL语句                                   sstmt.execcuteUUpdatte(sqql);                          cattch (SQLEExcepptionn e)                                    / TODOO Autto-geeneraated catcch bllock                                   ee.priintSttackTTracee();                         finaally                                   if(coonn!=nulll)                                             tryy                                                        coonn.cclosee();                                              cattch (SQLEExcepptionn e)                                                        connn = nulll;                                                       e.pprinttStacckTraace();                                                                                           Main.javaapackaage ccom.mmichaael.mmain;     impoort ccom.mmichaael.jjdbc.ConnnectiionUttil;     iimporrt coom.miichaeel.jddbc.TTestSStateementt;     pubbliccclasss Maiin                /*                * paaram argss                */               ppubliicstaaticvvoid mainn(Strring arrgs)                          ConnnecttionUUtil cu = neww ConnnecttionUUtil();                         /第一种方方法                         Sysstem.out.prinntln("第一种种方法:""+cu.getCConneectioon();                         /第第二种方法法                         Systtem.oout.pprinttln(""第二种方方法:"+cu.ggetCoonnecctionn("coom.myysql.jdbcc.Driiver"","jdbbc:myysql:/loocalhhost:33066/jdbbc_dbb","rroot"","myysqlaadminn");                         /第三三种方法                         SSysteem.ouut.prrintlln("第第三种方法法:"+ccu.oppenCoonnecctionn();                         TesttStattemennt.geetStaatemeent();                         /TTestSStateementt.creeateTTablee();                         TTestSStateementt.tesstInssert();                     测试结果: 

    注意事项

    本文(Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement13855.docx)为本站会员(you****now)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开