jsp+mysql进行简单的增删改查总结第一步创建一个新的包用于封装学生属性在新包里面创建新的类存入学生属性publicclass shuxing {String name;int age;String sex;int id;右击鼠标选择soure-gennerategettersand 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).try{}catch()语句用于报错try{int result = doDelete(id);//调用到doDelete的方法List<shuxing>qw = new ArrayList<shuxing>();//创建新的集合用于存放数据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<shuxing>qw = newArrayList<shuxing>();Try用于报错try{qw = getData();}catch(Exception e){e.printStackTrace();}缓存数据request.setAttribute("qw" ,qw);跳转request.getRequestDispatcher("index.jsp").forward(request, response);四:开始查询的主程序:public List<shuxing>getData() throws Exception{List<shuxing>qw = new ArrayList<shuxing>();//驱动程序名String driverName="com.mysql.jdbc.Driver";//数据库用户名String userName="root";//密码String userPasswd="123456";//数据库名String dbName="xueshenbiao";//表名String tableName="tubiao";//联结字符串Stringurl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();java.sql.Connectionconnection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String sql="SELECT * FROM tubiao ";ResultSetrs = statement.executeQuery(sql);//获得数据结果集合ResultSetMetaDatarmeta = 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;}六.增加主程序:publicint doAdd(Stringnewname,Stringnewid,Stringnewage,Stringnewsex ) 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";//联结字符串try{Stringurl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();java.sql.Connectionconnection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String sql="SELECT * FROM tubiao where id="+id;ResultSetrs = statement.executeQuery(sql);//获得数据结果集合ResultSetMetaDatarmeta = 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="";}try{int result = doModify(id,newname,newage,newsex);List<shuxing>qw =new ArrayList<shuxing>();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<shuxing>shuxingList = new ArrayList<shuxing>();if(request.getAttribute("qw")!=null){shuxingList =(List)request.getAttribute("qw");}2.在画好表格框架之后增加ADD按钮<Input type=submit name="add" value="增加"onclick="javascript:window.location.href='canshu.jsp'">3.JS语句用于点击按钮跳转进入JSP页面循环输出数据for(int i=0;i<shuxingList.size();i++){shuxing a = shuxingList.get(i);4.获取输出数据<td><%=a.getName()%></td><td><%=a.getAge() %></td><td><%=a.getSex()%></td><td><%=a.getId() %></td>5.添加删除和修改按钮<a href="chaxun1?openration=tomodify&id=<%=a.getId()%>">修改</a>|<a href="chaxun1?openration=delete&id=<%=a.getId()%>">删除</a></td>超链接删除,对应的openration的数值获取调用的方法,并且绑定好ID号。