學生管理系統
步驟:
- 先寫 login.jsp , 並且搭配一個LoginServlet 去獲取登錄信息。
- 在數據庫中創建用戶表, 裏面只要有id , username 和 password
- 創建UserDao, 定義登錄的方法
/**
* 該dao定義了對用戶表的訪問規則
*/
public interface UserDao {
/**
* 這裏簡單就返回一個Boolean類型, 成功或者失敗即可。
*
* 但是開發的時候,登錄的方法,一旦成功。這裏應該返回該用戶的個人信息
* @param userName
* @param password
*
* @return true : 登錄成功, false : 登錄失敗。
*/
boolean login(String userName , String password);
}
- 創建UserDaoImpl , 實現剛纔定義的登錄方法。
public class UserDaoImpl implements UserDao {
@Override
public boolean login(String userName , String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1. 得到連接對象
conn = JDBCUtil.getConn();
String sql = "select * from t_user where username=? and password=?";
//2. 創建ps對象
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, password);
//3. 開始執行。
rs = ps.executeQuery();
//如果能夠成功移到下一條記錄,那麼表明有這個用戶。
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps, rs);
}
return false;
}
}
- 在LoginServlet裏面訪問UserDao, 判斷登錄結果。 以區分對待
- 創建stu_list.jsp , 讓登錄成功的時候跳轉過去。
- 創建學生表 , 裏面字段隨意。
- 定義學生的Dao . StuDao
public interface StuDao {
/**
* 查詢出來所有的學生信息
* @return List集合
*/
List<Student> findAll();
}
- 對上面定義的StuDao 做出實現 StuDaoImpl
public class StuDaoImpl implements StuDao {
@Override
public List<Student> findAll() {
List<Student> list = new ArrayList<Student>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1. 得到連接對象
conn = JDBCUtil.getConn();
String sql = "select * from t_stu";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//數據多了,用對象裝, 對象也多了呢? 用集合裝。
while(rs.next()){ //10 次 ,10個學生
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setAge(rs.getInt("age"));
stu.setName(rs.getString("name"));
stu.setGender(rs.getString("gender"));
stu.setAddress(rs.getString("address"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps, rs);
}
return list;
}
}
- 在登錄成功的時候,完成三件事情。
- 查詢所有的學生。
1. 把這個所有的學生集合存儲到作用域中。
2. 跳轉到stu_list.jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//提交的數據有可能有中文, 怎麼處理。
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//1. 獲取客戶端提交的信息
String userName = request.getParameter("username");
String password = request.getParameter("password");
//2. 去訪問dao , 看看是否滿足登錄。
UserDao dao = new UserDaoImpl();
boolean isSuccess = dao.login(userName, password);
//3. 針對dao的返回結果,做出響應
if(isSuccess){
//response.getWriter().write("登錄成功.");
//1. 查詢出來所有的學生信息。
StuDao stuDao = new StuDaoImpl();
List<Student> list = stuDao.findAll();
//2. 先把這個集合存到作用域中。
request.getSession().setAttribute("list", list);
//2. 重定向
response.sendRedirect("stu_list.jsp");
}else{
response.getWriter().write("用戶名或者密碼錯誤!");
}
}
- 在stu_list.jsp中,取出域中的集合,然後使用c標籤 去遍歷集合。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>學生管理系統</title>
</head>
<body>
<br>學生列表<br>
<table border="1" width="700">
<tr>
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
<td>性別</td>
<td>住址</td>
</tr>
<c:forEach items="${list }" var="stu">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
<td>${stu.gender }</td>
<td>${stu.address }</td>
<td><a href="#">更新</a> <a href="#">刪除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>