多语言展示
当前在线:1076今日阅读:26今日分享:39

javaWeb使用ajax实现动态删除

写个简短例子实现javaWeb使用ajax实现动态删除数据,并更新页面。
工具/原料
1

eclipse

2

jquerty.jar mysql-conntection.jar

方法/步骤
1

编写login.jsp

    用户名:    密码:           

2

编写LoginServlet1.web.xml配置    LoginServlet    cn.com.test.servlet.LoginServlet      LoginServlet    /servlet/LoginServlet  2.LoginServlet关键代码:public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding('utf-8'); response.setCharacterEncoding('utf-8');     String userName=request.getParameter('userName');     String pwd=request.getParameter('pwd');          if(userName.equals('admin') && pwd.equals('admin')){         StudentDao stuDao=new StudentDao();     List stuList=stuDao.findAllStudent();     request.setAttribute('stuList', stuList);     request.getRequestDispatcher('/OperatorStudent.jsp').forward(request, response);         } }

3

OperatorStudent.jsp编写My JSP 'OperatorStudent.jsp' starting page

学生信息


<% List stuList = (List) request.getAttribute('stuList'); if (stuList != null && stuList.size() > 0) { for (int i = 0; i < stuList.size(); i++) { Student stu = (Student) stuList.get(i); %> <% } } %>
学号 姓名 年龄 地址 操作
<%=stu.getId()%> <%=stu.getUserName()%> <%=stu.getAge()%> <%=stu.getAddress()%>

4

编写DelStudentweb.xml配置:    DelStudent    cn.com.test.servlet.DelStudent      DelStudent    /servlet/DelStudent  代码:public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding('utf-8'); response.setCharacterEncoding('utf-8'); String id = request.getParameter('id'); if (id.substring(0, 4).equals('del_')) { int stuId = Integer.parseInt(id.substring(4)); StudentDao stuDao = new StudentDao(); int result = stuDao.deleteUserById(stuId); if (result > 0) { List stuList = stuDao.findAllStudent(); request.setAttribute('stuList', stuList); request.getRequestDispatcher('/OperatorStudent.jsp').forward( request, response); } } else { } }

5

实体类Studentpublic class Student { private int id; private String userName; private int age; private String address;//getter setter...

6

链接数据库,关闭数据库的操作public class BaseDao {        private final static String Driver='com.mysql.jdbc.Driver.class';        //数据库连接字符串 private final static String URL='jdbc:mysql://localhost:3306/test1'; //数据库登录用户名 private final static String USERNAME = 'root'; //登录密码 private final static String PASSWORD = 'root'; protected  Connection con=null; protected PreparedStatement pstm = null; protected ResultSet rs =null; String sql =null; /** * 获得数据库连接 * @return 数据库连接对象 */ public  Connection getConnection(){ try { //加载驱动 Class.forName('com.mysql.jdbc.Driver'); //获得数据库连接 con = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; } //释放数据库资源 public void closeConnection(){ try{ if(rs!=null){ rs.close(); } if(pstm !=null){ pstm.close(); } if(con!=null && con.isClosed()==false){ con.close(); } }catch(Exception e){ e.printStackTrace(); } } }

7

对数据的查询及删除//查询学生信息public List findAllStudent(){ List studentList=new ArrayList(); String sql='select * from student'; try{ con=super.getConnection(); pstm=con.prepareStatement(sql); rs=pstm.executeQuery(); while(rs.next()){ Student stu=new Student(); stu.setId(rs.getInt('id')); stu.setUserName(rs.getString('userName')); stu.setAge(rs.getInt('age')); stu.setAddress(rs.getString('address')); studentList.add(stu);   } }catch(Exception e){ e.printStackTrace(); } finally{ super.closeConnection(); } return studentList; }//删除信息public int deleteUserById(int id){ int result=0; String sql='delete from student where id=?'; try{ con=super.getConnection(); pstm=con.prepareStatement(sql); pstm.setInt(1,id);     result=pstm.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } finally{ super.closeConnection(); } return result; }

推荐信息