Struts2+JSP+JDBC实现学生信息的增删改查

最近敲了敲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<Student> 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<Student> findList() {
	String sql="select * from student ";
	List<Student> list=new ArrayList<Student>();
	
	Connection connection = JDBCUtil.getConnection();
	PreparedStatement pst=null;
	ResultSet rs=null;
	try {
		pst = connection.prepareStatement(sql);
		rs = pst.executeQuery();
		Student stu=null;
		while(rs.next()) {
		
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP(JavaServer Pages)是一种动态网页开发技术,可以使用Java代码嵌入到HTML页面中。实现增删改查功能需要借助Java Web开发框架,比如Spring MVC、Struts2等。下面是一个使用JSPJDBC实现增删改查功能的示例。 1.建立数据库连接 在JSP页面中使用JDBC连接数据库,可以在页面上显示数据库中的数据。 ```java <% Connection conn=null; Statement stmt=null; ResultSet rs=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); stmt=conn.createStatement(); rs=stmt.executeQuery("select * from user"); while(rs.next()){ %> <tr> <td><%=rs.getString("id")%></td> <td><%=rs.getString("username")%></td> <td><%=rs.getString("password")%></td> <td><%=rs.getString("email")%></td> <td><%=rs.getString("phone")%></td> <td><input type="button" value="修改"></td> <td><input type="button" value="删除"></td> </tr> <% } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } } %> ``` 2.添加数据 在JSP页面中使用JDBC添加数据到数据库中。 ```java <% Connection conn=null; PreparedStatement pstmt=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); String sql="insert into user(username,password,email,phone) values(?,?,?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, request.getParameter("username")); pstmt.setString(2, request.getParameter("password")); pstmt.setString(3, request.getParameter("email")); pstmt.setString(4, request.getParameter("phone")); pstmt.executeUpdate(); response.sendRedirect("index.jsp"); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } } %> ``` 3.修改数据 在JSP页面中使用JDBC修改数据库中的数据。 ```java <% Connection conn=null; PreparedStatement pstmt=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); String sql="update user set username=?,password=?,email=?,phone=? where id=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, request.getParameter("username")); pstmt.setString(2, request.getParameter("password")); pstmt.setString(3, request.getParameter("email")); pstmt.setString(4, request.getParameter("phone")); pstmt.setString(5, request.getParameter("id")); pstmt.executeUpdate(); response.sendRedirect("index.jsp"); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } } %> ``` 4.删除数据 在JSP页面中使用JDBC删除数据库中的数据。 ```java <% Connection conn=null; PreparedStatement pstmt=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); String sql="delete from user where id=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, request.getParameter("id")); pstmt.executeUpdate(); response.sendRedirect("index.jsp"); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } } %> ``` 以上是一个简单的使用JSPJDBC实现增删改查功能的示例,但这种方式并不是安全的,因为JSP代码容易受到SQL注入攻击,建议使用Spring MVC等Java Web框架实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值