jsp-mysql总结表的操作合集.docx_第1页
jsp-mysql总结表的操作合集.docx_第2页
jsp-mysql总结表的操作合集.docx_第3页
jsp-mysql总结表的操作合集.docx_第4页
jsp-mysql总结表的操作合集.docx_第5页
已阅读5页,还剩4页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

jsp+mysql进行简单的增删改查总结第一步创建一个新的包用于封装学生属性在新包里面创建新的类存入学生属性public class shuxing String name;int age;String sex;int id;右击鼠标选择soure-gennerate gettersand setters来封装数据第二步创建另一个新的包用于主体serlvet的操作Servlet里面操作:1. 因为doget比dopost拥有更强的保密性,所以把dopost的语句也沿用doget的形式doPost(request,response);注意删除的时候是不是把也删除了,会报错。2将设置请求的编码格式为UTF-8防止后面输入学生信息的时候出现乱码,之所以放在前面是怕后面写的话就不可以public公用了。3读取后面表中按钮的名称,用一个新的名字来获取随后跳转需要调用的语句String opeationName = request.getParameter(openration);/取出条件的OPENRATION,看执行哪一种语句4查看取出的名称是否为空。opeationName = opeationName=null?:opeationName.trim();/取出来看下是不是空,不是空去掉空格5用if()else()语句来写调用的程序用前面取出的名字一:删除方法:opeationName.equals(delete)(1)提取要删除的ID String id=request.getParameter(id);(2).查看是否为空,为空输出“”if(id=null)id=;(3).trycatch()语句用于报错 tryint result = doDelete(id);/调用到doDelete的方法List qw = new ArrayList();/创建新的集合用于存放数据qw = getData();/获取数据request.setAttribute(qw ,qw);/ 缓存范围中设置具体的属性:第一个参数是属性名,字符串类型的数据。,也是以后获取的依据条件,第二个是插入的数据,支持对象属性。(4)跳转进入最开始的jsp页面即查询语句的JSP界面二增加opeationName.equals(add)(1).增加不需要提取什么属性。直接获取在JSP中提交的数据即可,但是要观察是否为空String newname=request.getParameter(name);if(newname=null)newname=;String newid=request.getParameter(id);if(newid=null)newid=;String newsex=request.getParameter(sex);if(newsex=null)newsex=;String newage=request.getParameter(age);if(newage=null)newage=;(2).一样的try语句需要注意的是在DOADD后面写上属性的名称方便后面调用int result = doAdd(newname,newid,newage,newsex);(3).跳转到查询页面三查询页面(1)创建一个新的集合用于存放数据List qw = new ArrayList();Try用于报错tryqw = getData();catch(Exception e)e.printStackTrace();缓存数据request.setAttribute(qw ,qw);跳转 request.getRequestDispatcher(index.jsp).forward(request, response);四:开始查询的主程序:public List getData() throws ExceptionList qw = new ArrayList();/驱动程序名String driverName=com.mysql.jdbc.Driver;/数据库用户名String userName=root;/密码String userPasswd=123456;/数据库名String dbName=xueshenbiao;/表名String tableName=tubiao;/联结字符串String url=jdbc:mysql:/localhost/+dbName+?user=+userName+&password=+userPasswd;Class.forName(com.mysql.jdbc.Driver).newInstance();java.sql.Connection connection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String sql=SELECT * FROM tubiao ;ResultSet rs = statement.executeQuery(sql); /获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData();/确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount();以上都是连接数据库的操作下面的是获取数据的操作while(rs.next()/循环操作shuxing a1=new shuxing();int Id = rs.getInt(1);String Name = rs.getString(2);String Sex = rs.getString(3);int Age = rs.getInt(4);a1.setId(Id);a1.setAge(Age);a1.setName(Name);a1.setSex(Sex);qw.add(a1);return qw;五:删除主程序:int result =0;/用个result来做参数现在用不到以后可以在删除不成功的时候用于页面的跳转String deleteAll=DELETE FROM tubiao WHERE id + = +id+;result = statement.executeUpdate(deleteAll);return result;六.增加主程序:public int doAdd(String newname,String newid,String newage,String newsex ) throws Exception/注意对应/驱动程序名String condition1=INSERT INTO tubiao(name,id,age,sex) VALUES+(+newname+,+newid+,+newage+,+newsex+); /注意一一对应。不对应就在tubiao后面写参数 int result = statement.executeUpdate(condition1); 七:修改之所以放在最后是因为步骤比较多(1)。首先需要查找要修改的原始数据的值,opeation不直接调用修改的方法而是首先找到需要修改的学生的信息;步骤和查询信息差不多shuxing a1 = new shuxing();String id = request.getParameter(id);/查这个ID的学生信息/驱动程序名String driverName=com.mysql.jdbc.Driver;/数据库用户名String userName=root;/密码String userPasswd=123456;/数据库名String dbName=xueshenbiao;/表名String tableName=tubiao;/联结字符串tryString url=jdbc:mysql:/localhost/+dbName+?user=+userName+&password=+userPasswd;Class.forName(com.mysql.jdbc.Driver).newInstance();java.sql.Connection connection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String sql=SELECT * FROM tubiao where id=+id;ResultSet rs = statement.executeQuery(sql); /获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData();/确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount();while(rs.next()int Id = rs.getInt(1); String Name = rs.getString(2); String sex = rs.getString(3); int Age = rs.getInt(4); a1.setId(Id); a1.setAge(Age); a1.setName(Name); a1.setSex(sex);rs.close(); statement.close(); connection.close(); catch(Exception e)e.printStackTrace();(2).缓存数据request.setAttribute(a1, a1);(3).跳入修改的JSP进行修改的操作(4).由JSP跳转进入调用修改的方法与前面的类似opeationName.equals(modify)String id = request.getParameter(id);/调用修改方法 if(id=null)id=; String newname=request.getParameter(name);if(newname=null)newname=; String newage=request.getParameter(age);if(newage=null)newage=; String newsex=request.getParameter(sex);if(newsex=null)newsex=;tryint result = doModify(id,newname,newage,newsex);List qw = new ArrayList();qw = getData();request.setAttribute(qw ,qw);catch(Exception e)e.printStackTrace();request.getRequestDispatcher(index.jsp).forward(request, response);注意的是修改主程序中在插入update的时候分开来写String condition1=UPDATE tubiao SET name = +newname+ WHERE id =+id; String condition2=UPDATE tubiao SET sex = +newsex+ WHERE id =+id; String condition3=UPDATE tubiao SET age = +newage+ WHERE id =+id; int result = statement.executeUpdate(condition1);int result2 = statement.executeUpdate(condition2);int result3 = statement.executeUpdate(condition3);/获得数据结果集合八JSP中的操作增加的JSP:注意action对应的东西就好Form action=chaxun1?openration=add?如果是整句话意思:查找chaxun1中openration为add,跳转修改的JSP也差不多Form action=chaxun1?openration=add查询的JSP麻烦一点,因为有很多的超链接1首先要先获取之前集合里面的数据List shuxingList = new ArrayList();if(request.getAttribute(qw)!=null)shuxingList =(List)request.getAttrib

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论