JavaWeb第三记<最简单的一个查询,删除程序>

1-------------------------------------整体布局:如下图:


      数据库:

   


   运行效果图:


 


2-------------------------------------webContent下新建:三个jsp(hello2.jsp   students.jsp      success.jsp)

     ①hello2.jsp

     body里输入:<a href="listAllStudent">listAllstudent</a>

    ② student.jsp

     <body>
  <%
  List<Student>  stus=(List<Student>)request.getAttribute("students");  
  %>
   <table border="1"  cellpadding="10" cellspacing="0">
   <tr>
   <th>id</th><th>name</th><th>sex</th><th>age</th><th>birthday</th><th>Delete</th>
   </tr>
    <%
   for(Student  student:stus){
       %>
       <tr>
     <td><%=student.getId() %></td><td><%=student.getName() %></td>
     <td><%=student.getSex() %></td><td><%=student.getAge() %></td>
     <td><%=student.getBirthday() %></td><td><a  href="delectStudent?id=<%=student.getId()%>">Delect</a></td>
     </tr>
       <%
      }
   %>
   </table>
 </body>

③ success.jsp

  <body>
  删除成功
  <br/><br>
  <a  href="listAllStudent">返回原来的页面</a>
</body>

3------------------------.DelectStudentServlet   :

package com.iss.javaweb.mvc;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/delectStudent")
public class DelectStudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       public DelectStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         String  id=request.getParameter("id");
         StudentDao  studentDao=new  StudentDao();
         studentDao.deleteById(Integer.parseInt(id));
         request.getRequestDispatcher("/success.jsp").forward(request, response);
        
    }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

4  --------------------------------ListAllStudentServlet:

package com.iss.javaweb.mvc;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/listAllStudent")
public class ListAllStudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
  public ListAllStudentServlet() {
        super();
       }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao  studentDao=new  StudentDao();
        List<Student> students=studentDao.getAll();
        request.setAttribute("students",students);
        request.getRequestDispatcher("/students.jsp").forward(request, response);
    }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

5----------------StudentDao

package com.iss.javaweb.mvc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentDao {
    
    
    List<Student> students=new  ArrayList<>();
  public  void  deleteById(Integer id){
      Connection  connection=null;
       PreparedStatement  preparedStatement=null;
       ResultSet   resultSet=null;
       try {
           String  driverClass="com.mysql.jdbc.Driver";
           String  url="jdbc:mysql://localhost:3306/databaseWeb";
           String user="root";
           String password="1234";
           Class.forName(driverClass);
           connection=DriverManager.getConnection(url,user,password);
           String sql="Delete FROM tb_person2 Where id=?";
           preparedStatement=connection.prepareStatement(sql);
          preparedStatement.setInt(1, id);
          preparedStatement.executeUpdate();
        
    } catch (Exception e) {
          e.printStackTrace();
    }finally {        
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
       
 }
    public List<Student>  getAll(){
       Connection  connection=null;
       PreparedStatement  preparedStatement=null;
       ResultSet   resultSet=null;
       try {
           String  driverClass="com.mysql.jdbc.Driver";
           String  url="jdbc:mysql://localhost:3306/databaseWeb";
           String user="root";
           String password="1234";
           Class.forName(driverClass);
           connection=DriverManager.getConnection(url,user,password);
           String sql="SELECT id, name,sex,age,birthday FROM tb_person2";
           preparedStatement=connection.prepareStatement(sql);
           resultSet=preparedStatement.executeQuery();
           while (resultSet.next()) {
             int  id=resultSet.getInt(1);
             String name=resultSet.getString(2);
             String  sex=resultSet.getString(3);
             int  age=resultSet.getInt(4);
             Timestamp  birthday=resultSet.getTimestamp(5);
            
             Student  student=new  Student(id,  name, age,  sex, birthday);
             students.add(student);
             }
           } catch (Exception e) {
          e.printStackTrace();
    }finally {
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
        return students;
   }
}

6------------------------------Student

package com.iss.javaweb.mvc;

import java.sql.Timestamp;
import java.util.Date;
public class Student {
  private  int  id;
  private  String name;
  private int  age;
  private  String sex;
  private  Timestamp   birthday;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public Timestamp getBirthday() {
    return birthday;
}
public void setBirthday(Timestamp birthday) {
    this.birthday = birthday;
}
public Student(int id, String name, int age, String sex, Timestamp birthday) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.birthday = birthday;
}

   public  Student(){
    }
@Override
public String toString() {
    return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + "]";
}
 }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值