<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
姓名 | |
性别 | 男 女 |
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<td>姓名</td>
<td><input type="text" name="name" value="${user.name }" ></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="男" <c:if test="${user.sex eq '男' }">checked="checked"</c:if>>男
<input type="radio" name="sex" value="女" <c:if test="${user.sex eq '女' }">checked="checked"</c:if>>女
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<c:forEach var="user" items= "${users}" varStatus="status">
<tr align="center">
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
<td><a href="${pageContext.request.contextPath}/findById.action?id=${user.id}">编辑</a> |
<a href="${pageContext.request.contextPath}/delete.action?id=${user.id}" onclick=" return confirm('确定删除吗')">删除</a></td>
</tr>
</c:forEach>
学号 | 姓名 | 性别 | 操作 |
---|
package cn.itcast.dao.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import cn.itcast.dao.UserDao;
import cn.itcast.po.Student;
import cn.itcast.utils.MydbUtils;
public class UserDaoImpl implements UserDao {
Connection conn = null;
ResultSet set = null;
PreparedStatement stmt = null;
//展示用户
public List findAll() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List users =new ArrayList();
try {
conn = MydbUtils.connction();
String sql = “select * from t_student”;
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
if(rs.next()){
Student u = new Student();
u.setId(rs.getInt(“id”));
u.setName(rs.getString(“name”));
u.setSex(rs.getString(“sex”));
users.add(u);
}
return users;
} catch (Exception e) {
// TODO: handle exception
}
finally{
MydbUtils.reasle(conn, rs, stmt);
}
return null;
}
//删除用户
public void delete(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = MydbUtils.connction();
String sql = “delete from t_student where id=?”;
stmt = (PreparedStatement) conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
finally{
MydbUtils.reasle(conn, rs, stmt);
}
}
//通过id查找用户
public Student findById(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = MydbUtils.connction();
String sql = "select * from t_student where id=?";
stmt = (PreparedStatement) conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if(rs.next()){
Student u = new Student();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setSex(rs.getString("sex"));
return u;
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
MydbUtils.reasle(conn, rs, stmt);
}
return null;
}
//更新用户
public void update(Student s) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = MydbUtils.connction();
String sql = "update t_student set name=?,sex=?where id=?";
stmt = (PreparedStatement) conn.prepareStatement(sql);
stmt.setString(1, s.getName());
stmt.setString(2,s.getSex());
stmt.setInt(3, s.getId());
stmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
finally{
MydbUtils.reasle(conn, rs, stmt);
}
}
}