最近敲了敲struts+jsp的增删改查 欧克~话不多说 -------->
准备工作:导入所需要的jar包、struts.xml、配置web.xml
1、分包分模块
创建 dao层 service层 pojo和utils包 当然 不要忘记最重要的Action层
2、创建实体类,在这我创建的是 Student类 ,给他get/set方法,toString方法,有参构造和无参构造(可不添加)
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
3、在dao层创建一个接口以及他的实现类
public interface StudentDaoI {
List findList();
Student findById(Integer id);
void UpdateStudent(Student student);
void AddStudent(Student student);
void Delate(Integer id);
}
public class StudentDaoImpl implements StudentDaoI{
@Override
public List findList() {
String sql="select * from student ";
List list=new ArrayList();
Connection connection = JDBCUtil.getConnection();
PreparedStatement pst=null;
ResultSet rs=null;
try {
pst = connection.prepareStatement(sql);
rs = pst.executeQuery();
Student stu=null;
while(rs.next()) {
stu=new Student(rs.getInt(1), rs.getString(2),rs.getInt(3));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭
JDBCUtil.close(rs, pst, connection);
return list;
}
public Student findById(Integer id) {
// TODO Auto-generated method stub
String sql="select * from student where id=?";
Student student=null;
PreparedStatement pst=null;
ResultSet rs=null;
Connection connection = JDBCUtil.getConnection();
try {
pst=connection.prepareStatement(sql);
pst.setInt(1, id);
rs=pst.executeQuery();
if (rs.next()) {
student=new Student(rs.getInt(1), rs.getString(2), rs.getInt(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBCUtil.close(rs, pst, connection);
return student;
}
@Override
public void UpdateStudent(Student student) {
// TODO Auto-generated method stub
String sql="update student set name=?,age=? where id=?";
Connection connection = JDBCUtil.getConnection();
PreparedStatement pst=null;
try {
pst=connection.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setInt(2, student.getAge());
pst.setInt(3, student.getId());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBCUtil.close(null, pst, connection);
}
@Override
public void AddStudent(Student student) {
// TODO Auto-generated method stub
String sql="insert into student set name=?,age=?";
Connection connection = JDBCUtil.getConnection();
PreparedStatement pst=null;
try {
pst=connection.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setInt(2, student.getAge());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBCUtil.close(null, pst, connection);
}
@Override
public void Delate(Integer id) {
// TODO Auto-generated method stub
String sql="delete from student where id=?";
Connection connection = JDBCUtil.getConnection();
PreparedStatement pst=null;
try {
pst=connection.prepareStatement(sql);
pst.setInt(1,id);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBCUtil.close(null, pst, connection);
}
}
4、在service层同样创建接口并实现 调用dao层的方法
public interface StudentServiceI {
List findList();
Student findById(Integer id);
void UpdateStudent(Student student);
void AddStudent(Student student);
void Delate(Integer id);
}
public class StudentServiceImpl implements StudentServiceI{
private StudentDaoI studentDao=new StudentDaoImpl();
@Override
public List findList() {
// TODO Auto-generated method stub
return studentDao.findList();
}
@Override
public Student findById(Integer id) {
// TODO Auto-generated method stub
return studentDao.findById(id);
}
@Override
public void UpdateStudent(Student student) {
// TODO Auto-generated method stub
studentDao.UpdateStudent(student);
}
@Override
public void AddStudent(Student student) {
// TODO Auto-generated method stub
studentDao.AddStudent(student);
}
@Override
public void Delate(Integer id) {
// TODO Auto-generated method stub
studentDao.Delate(id);
}
}
5、接下来最重要的来喽
public class StudenrAction {
private StudentServiceI studentService=new StudentServiceImpl();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
public Student student =new Student();
public String findList() {
List list=studentService.findList();
request.setAttribute("list", list);
return "list";
}
public String delete() {
int id = Integer.parseInt(request.getParameter("id"));
studentService.Delate(id);
return "del";
}
public String add() {
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
Student student=new Student(null, name, age);
studentService.AddStudent(student);
return "add";
}
public String toupdate() {
int id = Integer.parseInt(request.getParameter("id"));
Student student=studentService.findById(id);
request.setAttribute("student", student);
return "toupdate";
}
public String update() {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
Student student=new Student(id, name, age);
studentService.UpdateStudent(student);
return "update";
}
}
6、struts.xml中的代码
/index.jsp
findList
findList
/update.jsp
findList
7、jsp页面
index.jsp
add.jsp
ok~ struts+jsp+jdbc就算完成啦 祝大家事业有成!
ps:转载要注明出处哦