版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java Web应用实例servlet实现oracle基本增删改查分类: JAVA WEB 应用实例 2012-08-31 16:51 3515人阅读评论(0收藏举报htt p:/blog.csd n.n et/a542214712/article/details/7924444很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的,所以从今天开始,我将与大家分享一下我学JAVA WEB写的一些小实例 ,我个人是不太喜欢书本上的晦涩的概念的,所以我花了更多的时间在一些应用实例上,我觉得这 样的学习方式很适合我,由简到繁,由浅入深废话不多话,开始我们的第一个实
2、例:servlet实现oracle基本增删改查开发环境:JDK7 +Myeclipse10+tomcat7.0+oracle 11g首先附上数据库创建脚本:sql view plaincopy1. create table student(2. id VARCHAR2(50 PRIMARY KEY NOT NULL,3. name varchar2(50 NOT NULL4. calssGrent varchar2(50NOT NULL ,5. result varchar (12 NOT NULL6.;7. insert into student(id,name,calssGrent,res
3、ult','33' , '98'values (perseq.nextval,'张三下图为代码结构图和功能演示界面做的比较糟糕只为实现功能:V ZLjflvaweb寻.my eel ipse三.settings.丄弓5rccomF I即org- Isy2 execdemoJ5 servletJjj AddjavaIT AddPsgeJavaU deletejavaM MMC_01.javaJT| Update.JavaUpdatePagejavaG5 WebRoot国j xlaspath|XI .mymetadata:3(l .projectMM
4、C_01.java页面/主界面提供添加,修改删除的方法超修改删除肚哪22213住IBMlay|l23123刘守21323121,刘宇宇133阴11罔功的揺java view p lai ncopy1. Package org.lsy.servlet;2.3. i mportjava.io.lOExce ption;4. i mportjava.io .P rintWriter;5. i mportjava.sql.Connection;6. i mportjava.sql.DriverManager;7. i mportjava.sql.ResultSet;8. i mportjava.sql
5、.Statement;9.10. im portjavax.servlet.ServletExce ption;11. i mportjavax.servlet.htt p. Htt pServlet;12. im portjavax.servlet.htt p. Htt pServletRequest;13. im portjavax.servlet.htt p. Htt pServletRes po nse;14.15. i mportoracle.jdbc.oracore.TDS Patch;16.17. p ublicclass MMC_01 extends Htt pServlet1
6、8. 19./驱动程序就是之前在class path中配置的jdbc的驱动程序的jar包中20.21./连接地址是由各个数据库生产商单独提供的,所以需要单独记住22.p ublic static finalString DBURL ="jdbc:oracle:thi n: localhost:1521:LIUSY23./连接数据库的用户名24.p ublic static finalString DBUSER ="scott"25./连接数据库的密码26.p ublicstatic finalString DBP ASS ="tiger"27.
7、28.ep ublicvoid doGet(HttpServletRequest request, HttpServletResponse respons29.throws ServletException, lOException30.31./设置请求和响应的编码,不然页面上容易出现乱码32.request.setCharacterEncoding("UTF-8"33.respo nse.setCharacterEncoding("UTF-8"34.35.respo nse.setContentT ype("text/html;UTF-8&q
8、uot;36./输出流37.P rintWriter out = respo nse.getWriter(;38./数据库连接代码39.try40.41.Connection conn =null; /表示数据库的连接的对象42.Statement stmt =null; /表示数据库的更新操作43.'67','String sql= 刘 12 宇','33','98'""insert into student(id,name,calssGrent,result values(44.System.out .p
9、rintln(sql ;45./ 1 、使用Class类加载驱动程序46.Class.forName("oracle.jdbc.driver.OracleDriver"47./ 2、连接数据库48.conn = DriverManager.getConnection(DBURL, DBUSER, DBP ASS;49./ 3、Statement 接口需要通过 Connection 接口进行实例化操作50.stmt = conn.createStatement(;51.偷一下懒,/用rs获取查询结果,这里的SQL语句最好不要用*我的字段比较少所以就52.ResultSet r
10、s=stmt.executeQuery("select *from student"53.out. println(HH54.out. println(HH55.out. println(56.out. println(57./输出表头部分58.out .p rint(59."班级成绩修改删除"60.while (rs.next(61.62./获取数据库中的ID63.String id=rs.getString("id"64./输出一列元素65.out. print(""+rs.getString( "n
11、ame"66.67.""+rs.getString( "calssGrent" +"" + ""+rs.getString( "result" +"" +68."+id+ "'> 修改"+69."+id+ "'> 删除"70.71.72.out. println(73.out. println(74.out. println(75.conn.close(;76.77.78.cat
12、ch (79.80.e.p rintStack81.82.out.flush(;83.out.close(;84.85.86. 87.HHUp date Page.java II获取要修改的数据,并把修改过的数据提交给Up datejava view p lai ncopy1. Package org.lsy.servlet;2. i mportjava.io.lOExce ption;3. i mportjava.io .P rintWriter;4. i mportjava.sql.Connection;5. i mportjava.sql.DriverManager;6. i mport
13、java.sqI.ResultSet;7. i mportjava.sql.Statement;9. i mportjavax.servlet.ServletExce ption;10. import javax.servlet.http.HttpServlet;11. import javax.servlet.http.HttpServletRequest;8.12. im portjavax.servlet.htt p. Htt pServletRes po nse;13.14. i mportcom.sun.cry pto.p rovider.RSAC ip her;15.16. p u
14、blicclass Up date Pageextends Http Servlet17. 18. /连接地址是由各个数据库生产商单独提供的,所以需要单独记住19.LIUSY"p ublic static finalString DBURL ="jdbc:oracle:thin:localhost:1521:20./连接数据库的用户名21.p ublic static finalString DBUSER ="scott"22./连接数据库的密码23.p ublic static finalString DBP ASS ="tiger"
15、;24.25.e26.27.28.String id=request.get Parameter("id"29./设置请求和响应的编码,不然页面上容易出现乱码30.request.setCharacterEncoding("UTF-8"31.respo nse.setCharacterEncoding("UTF-8"32.respo nse.setContentT ype("text/html;UTF-8"33.P rintWriter out = respo nse.getWriter(;34.try35.36.
16、Connection conn =null ; /表示数据库的连接的对象37.Statement stmt =null ; /表示数据库的更新操作38./ 1 、使用Class类加载驱动程序39.Class.forName("oracle.jdbc.driver.OracleDriver"40./ 2、连接数据库publicvoid doGet(HttpServletRequest request, HttpServletResponse responsthrows ServletException, lOException41.conn = DriverManager.g
17、etConnection(DBURL, DBUSER, DBP ASS;42./ 3、Statement 接口需要通过 Connection 接口进行实例化操作java43.stmt = conn.createStatement(;44./用rs获取查询结果,这里的SQL语句最好不要用*我的字段比较少所以就偷一下懒:>545.ResultSet rs=stmt.executeQuery("select *from student where id='"id+ 5"46.while(rs.next(47.48.out. pnntln(HH .49.ou
18、t. pnntln(HH .50.out. pnntln(II II51.out. pnntln(II II52./输入成绩的表单,提交至Up date53.out. pnnt(IIII54.out. pnnt("姓名:+rs.getString("name" +"'w plaincopy1. "white-s pace:pre">name='name' size= 20 >"2.out. pnnt("班级:+rs.getString("calssGrent"
19、+"'name='grent' size=20>"3.out. pnnt("成绩:+rs.getString("result"+"'name='result' size=20>"/为了作为查询数据库的条件,这里要把ID传过去5.out. pnnt("+id+6.out. pnnt("修改7.out. pnnt(II II8.out. pnntln(HH .9.10.out.flush(;11.out.close(;12.13.catch (Exc
20、e ption e 14.e. printStackTraceC4."'name='id'>"'> 重置'>"/ TODO: handle exce ption8. Up date.java接收要修改的数据,并更新数据库java view p lai ncopy1. Package org.lsy.servlet;2.3. i mportjava.io.lOExce ption;4. i mportjava.io .P rintWriter;5. i mportjava.sql.Conn
21、ection;6. i mportjava.sql.DriverManager;7. i mportjava.sql.Statement;8.9. i mportjavax.servlet.ServletExce ption;10. im portjavax.servlet.htt p. Htt pServlet;11. i mportjavax.servlet.htt p. Htt pServletRequest;12. im portjavax.servlet.htt p. Htt pServletRes po nse;13.14. p ublicclass Up date extends
22、 Htt pServlet15. 16./驱动程序就是之前在class path中配置的jdbc的驱动程序的jar包中17.18./连接地址是由各个数据库生产商单独提供的,所以需要单独记住19.Y"p ublic static finalString DBURL ="jdbc:oracle:thin:localhost:1521:LIUS20./连接数据库的用户名21.p ublic static finalString DBUSER ="scott"22./连接数据库的密码23.p ublic static finalString DBP ASS =&
23、quot;tiger"25.epublicvoid doGet(HttpServletRequest request, HttpServletResponse respons24.26.throws ServletException, lOException27.28.try29.30.31.request.setCharacterEncoding("UTF-8"32.respo nse.setCharacterEncoding("UTF-8"33.respo nse.setContentT ype("text/html;UTF-8&
24、quot;34.P rintWriter out = respo nse.getWriter(;35.Connection conn =null ;/表示数据库的连接的对象36.Statement stmt =null ; /表示数据库的更新操作37./获取表单提交的参数湖北省武昌区2012届高三年级元月调研测试英语试题本试卷第一至第三部分为选择题,共100分;第四部分为非选择题,共 50分,全卷共12页。注意事项:1.答题前,考生务必将自己的学校、班级、姓名、准考证号填写在答题卷指定位置,认真核对与准考证号条形码上的信息是否一致,并将 准考证号条形码粘贴在答题卷上的指定位置。2.选择题的作答
25、:选出答案后,用2B铅笔把机读卡上对应题目的答案标号涂黑,如需改动,用橡皮擦干净后,再选涂其他答案标号。答在试题卷上无效。3 .非选择题的作答:用黑色墨水的签字笔直接答在答题卷上的每题所对应的答题区域内。答在试题卷上或答题卷指定区域外无效第一部分:听力(共两节,满分30分)做题时,先将答案划在试卷上。录音内容结束后,你将有两分钟的时间将试卷上的答案转涂到答题卡上。第一节(共5小题;每小题1 . 5分,满分7. 5 分)听下面5段对话。每段对话后有一个小题,从题中所给的A、B、C三个选项中选出最佳选项,并标在试卷的相应位置。听完每段对话后,你都有10秒钟的时间来回答有关小题和阅读下一小题。每段对
26、话仅读一遍。档 ZLjavaweb 耳 .my eclipse 吕.settings 丄m srccom匕ky匸org13- Isy三 execdemo | 丄了 servletJlj Addjava r?| AddPagejava fl deletejava fij MMC.Ol.java |J| Update.JavaXj UpdatePagejava& WebRootX -mymetadataX .project1. What does the man speaker sug do?gest the woma nA. Open the door carefullyQ ZLjflv
27、aweb三.my eel ipse 吕.settingssrecom匸ky:二 org- Isy三 execdemo |i3 servlet£ Addjava 回 AddPageJava 团 deletejavia JJ MMC.Ol.java iJ| Update.java2£j UpdatePagejavaG5 WebRoot国xiasspath|X| .mymet日data.X| .projecte Joh n fix the lockC. Fix the door in time2. What's the probable relationship betw
28、een the speakers?Boss and secretary .Teacher and stude ntHusba nd and wife3. What was the lecture about?Effective ways to give up smok ingThe harmful effects of sec on d-ha nd smokeC.Prof . Smith's new inventionWhere are the sp eakers at the moment?A.At home . B . In a restaurantC . In the stree
29、t.A.Why does the man go to see the doctor?He fell off a ste p and twisted his ankleB. He fell off a bike and hurt his legsC. He hurt his ankle in a football match第二节(共15小题;每题1 . 5分,满分22 . 5分)听下面5段对话或独白。每段对话或独白后有几个小题,从题中所给的A,B,C三个选项中选出最佳选项,并标在试卷的相应位置。听每段对话或独白前,你将有时 间阅读各个小题,每小题5秒钟;听完后,每小题将给出5秒钟的作答时间。每
30、段 对话或独白读两遍。听第6段材料,回答第6、7题。来源:Zxxk.ComA.To make an apo logyB . To make a comp laintC . To make an appo intmentWhy does the man make the call?When will the woman come to the man's home?A.At 3:00 pm on FridayB.At 11:00 am on SaturdayC. At 3:00 pm on Saturday听第7段材料,回答第8、9题。来源:Zxxk.ComA.What do we kn
31、ow about the man's dad?He was angry with the manB.He met with his friends last night.来源:Z_xx_k.ComC.A.Stay at homeB . Go into hospitalC . Have a heart op eration.He has been bad-te mp ered in the last monthWhat does the doctor say the man's dad might have to do?听第8段材料,回答第10至12题。10 . What wil
32、l the woman do on Thursday after having a pi ano lesson?C . Take care of childrenA . Read a book . B . Watch a movie 11 . What will the woman be busy doing on Tuesday?A.Practising her violinB.Helping her neighboursC.12.A.Very easyB . Too busy . C . Too boringWriting her English paper.How does the wo
33、man feel about her life?17 46./ 1、使用Class类加载驱动程序听第9段材料,回答第13至16题。13 .What's the plan about?New p roduction methods and advertisements.B.Costs of advertisementsC.Television commercials14A.Finance sectionB . Personnel and staffC . Personnel and finance section.Who has the woman been trying to talk
34、 to about the human resources?15.It's not exp ensive any more to do that39.System.out .p rintln(id;B.String name=request.get Parameter("n ame"C.String grent=request.get Parameter("gre nt"String id=request.get Parameter(16.A.Higher p rofits, BWhat will be the result of the inv
35、estment according to the woman?43.更新SQLHigher quality p roductsHI H+ 段材料,回答第45."calssGre nt="' +grent+ "',result='" +result+ "' where id='" +id+ "'"71.47.Class.forName("oracle.jdbc.driver.OracleDriver"48./ 2、连接数据库conn = DnverM
36、anager.getConnection(DBURL, DBUSER, DBP ASS;50./ 3B. By p osting notices around.接口进行实例化操作 C.51.stmt = conn.createStatement(;52. What should we pay besides the first month's rent?A. Water and electricityif(pd!= 0 B .54.55.C A security deposit56.out. println(HHA.out. println(58.out. println("
37、C59.查看列表out. print(""更新成功60.out. println(II II ;61.out. println(第二部分:英语知识运用(共两节,62.63. C64.out.close(;65.stmt.close(;of what had happ ened when the attack tookplace .HA.68.A. application B.assu mp tion C70. purely by examination23.It is generally acknowledged that education shouldn't b
38、e16.A- measured B - confirmed C - classified D - identified 24- The examination was coming, but she couldn't go to sleep easily74.throws ServletException, lOExceptionA- crowded B - broke77.settled78.79. add Page .java,添加数据页面java view p lai ncopy1. package org.lsy.servlet;2.3. i mportjava.io.IOEx
39、ce ption;4. i mportjava.io .P rintWriter;5.6. i mportjavax.servlet.ServletExce ption;7. i mportjavax.servlet.htt p. Htt pServlet;8. i mportjavax.servlet.htt p. Htt pServletRequest;9. i mportjavax.servlet.htt p. Htt pServletRes po nse;10.11. p ublicclass Add Page extends HttpServlet12. 13.14.epublicv
40、oid doGet(HttpServletRequest request, HttpServletResponse respons15.throws ServletException, lOException17./设置请求和响应的编码,不然页面上容易出现乱码18.request.setCharacterEncoding("UTF-8"19.respo nse.setCharacterEncoding(20.respo nse.setContentT ype(21.P rintWriter out =respo nse.getWriter(;22.out. pnntln(H
41、H .23.out. pnntln(""24.out. pnntln(II II ;25.out. pnntln(II II ;26./输入成绩的表单,提交至27.out. pnnt(IIII28.out. pnnt(" 姓名:”;29.out. pnnt("班级:“;30.out. pnnt("成绩:"31.out. pnnt("添加'> 重置'>"32.out. pnntln("查看列表"33.out. pnntln(II II ;34.out. pnntln(H
42、H .35.out.flush(;36.out.close(;37.38.39. "text/html;UTF-8"add"UTF-8"add 接收add page传来的传,更新数据库java view p lai ncopy1. package org.lsy.servlet;3. i mportjava.io.IOExce ption;4. i mportjava.io. PrintWriter;5. i mportjava.sql.Connection;6. i mportjava.sql.DriverManager;7. i mportjava.
43、sql. Prep aredStatement;8. i mportjava.sql.ResultSet;2.9. i mport java.sql.Statement;10.11. i mportjavax.servlet.ServletExce ption;12. im portjavax.servlet.htt p. Htt pServlet;13. im portjavax.servlet.htt p. Htt pServletRequest;14. i mportjavax.servlet.htt p. Htt pServletRes po nse;15.16. im portcom
44、..a ernal.i mp l.io.UTF8Reader;17.18. im portoracle.jdbc.oracore.TDS Patch;19.20. p ublicclass Add extends Http Servlet21. 22./驱动程序就是之前在class path中配置的jdbc的驱动程序的jar包中23.24./连接地址是由各个数据库生产商单独提供的,所以需要单独记住25.Y"p ublic static finalString DBURL ="jdbc:oracle:thin:localhost:
45、1521:LIUS26./连接数据库的用户名27.p ublic static finalString DBUSER ="scott"28./连接数据库的密码29.P ublic staticfinalString DBP ASS ="tiger"30.31.Override32.p rotectedvoiddoP ost(Htt pServletRequest req, Htt pServletRes po nse resp33.throwsServletExce ption, lOExce ption4./ TODO Auto-g
46、e nerated method stub36.doGet(req, res p;39.epublicvoid doGet(HttpServletRequest request, HttpServletResponse respons40.throws ServletException, lOException6.conn = DriverManager.getConnection(DBURL, DBUSER, DBP ASS;41.42./设置请求和响应的编码,不然页面上容易出现乱码43.request.setCharacterEncoding("UTF-8"44.res
47、po nse.setCharacterEncoding("UTF-8"45.46.respo nse.setContentT ype("text/html;UTF-8"47.48./输出流P rintWriter out = respo nse.getWriter(;49./数据库连接代码50.try51.52.Connection conn =null ; /表示数据库的连接的对象53.Statement p stmt =null ; /表示数据库的更新操作54.String nameString=request.get Parameter("n ame"55.String grentString
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 防造假管理制度
- 2024届新疆维吾尔自治区下学期高三数学试题第一次摸底考试试卷
- 2024届上海市闸北区高三下学期期中数学试题文试卷
- 2024年岳阳道路客运从业资格证模拟考试
- 2024年天津客运从业资格证摸拟题
- 2024年固原客运从业资格证模拟考试题库下载
- 2024年那曲客运从业资格证考试真题保过
- 2024年新疆客运资格证应用能力考试
- 2024年新疆客运资格证应用能力考试题库及答案
- 2024年呼和浩特客运资格证理论考试答题技巧
- 显控历史数据sk案例
- 外研版(2023)必修 第一册Unit 4 Friends forever Starting out课件(共20张PPT)
- 浙人美2011版二年级美术上册《我们喜欢的动画形象》教案及教学反思
- 锂离子电池储能电站早期安全预警及防护
- 非对称竞争模式-颠覆和超越竞争对手的战略系统
- 江苏省南通市通州区2021-2022学年高二上学期期中质量检测物理试题Word版含答案
- 物业公司 监控录像查看记录表
- 变电站标示牌
- 职称申报个人业务自传【6篇】
- 半挂车产品设计规范手册
- 2023年事业单位面试结构化六大题型必看
评论
0/150
提交评论