Java——Web開發之簡單的學生資訊管理系統
阿新 • • 發佈:2018-11-27
這僅僅是一個跟著視訊學習的小系統,大牛就繞道啦~
系統實現的功能:
- 與mysql資料庫連線,實現登陸功能
- 管理員登陸成功後能檢視學生資訊
- 擴充套件功能(完成刪除與更新學生資訊的操作)
系統實現過程:
- 寫一個簡單的登陸介面login.jsp ,並且使用LoginServlet獲取登陸資訊
- 在資料庫裡建立使用者資訊表
- 建立UserDao介面,定義登陸方法,建立UserDaoImpl類,實現UserDao介面的登陸方法
- 在LoginServlet裡面訪問UserDao ,判斷登陸是否成功
- 建立stu_list.jsp,登陸成功則跳轉
- 在資料庫建立學生表
- 定義學生的StuDao介面,再定義一個StuDaoImpl實現StuDao介面的查詢所有學生資訊的方法
- 登陸成功後,查詢所有學生,把這個所有的學生集合儲存到session作用域中,跳轉到stu_list.jsp
- 在stu_list.jsp中取出session作用域中的集合,然後使用jstl裡的c標籤遍歷集合
先給出web工程的目錄結構~
程式碼給出~
jdbc.properties
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> StuManagerSystem:<br> <form action="LoginServlet" method="post"> 賬號:<input type="text" name="username"><br> 賬號:<input type="password" name="password"><br> <input type="submit" value="登陸"><br> </form> </body> </html>
LoginServlet.java
package web.stu.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import web.stu.bean.Student; import web.stu.dao.StuDao; import web.stu.dao.UserDao; import web.stu.dao.impl.StuDaoImpl; import web.stu.dao.impl.UserDaoImpl; //用於處理登陸的Servlet public class LoginServlet extends HttpServlet { 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); //3.重定向 跳轉 response.sendRedirect("stu_list.jsp"); }else{ response.getWriter().write("登陸失敗!"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
UserDao.java
package web.stu.dao;
/*
該dao定義了對使用者表的訪問規則
*/
public interface UserDao {
boolean login(String username,String password);
}
UserDaoImpl.java
package web.stu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import web.stu.dao.UserDao;
import web.stu.util.JDBCUtil;
public class UserDaoImpl implements UserDao {
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 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;
}
}
stu_list.jsp
<%@ 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>StuManagerSystem</title>
</head>
<body>
<br><h2>學生列表</h2><br>
<table border="1" width="700">
<tr align="center">
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
<td>電話號碼</td>
<td>操作</td>
</tr>
<c:forEach items="${sessionScope.list }" var="stu">
<tr align="center">
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
<td>${stu.num }</td>
<td><a href="#">更新</a> <a href="#">刪除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
JDBCUtil.java
package web.stu.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password= null;
static{
try {
//1. 建立一個屬性配置物件
Properties properties = new Properties();
// InputStream is = new FileInputStream("jdbc.properties");
//使用類載入器,去讀取src底下的資原始檔。 後面在servlet
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//匯入輸入流。
properties.load(is);
//讀取屬性
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取連線物件
* @return
*/
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverClass);
//靜態程式碼塊 ---> 類載入了,就執行。 java.sql.DriverManager.registerDriver(new Driver());
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
//2. 建立連線 引數一: 協議 + 訪問的資料庫 , 引數二: 使用者名稱 , 引數三: 密碼。
conn = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 釋放資源
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
Student.java
package web.stu.bean;
public class Student {
private int id;
private String name;
private String num;
private int age;
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 String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StuDao.java
package web.stu.dao;
import java.util.List;
import web.stu.bean.Student;
public interface StuDao {
//查詢所有學生資訊,返回List集合
List<Student> findAll();
}
StuDaoImpl.java
package web.stu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import web.stu.bean.Student;
import web.stu.dao.StuDao;
import web.stu.util.JDBCUtil;
public class StuDaoImpl implements StuDao {
//實現StuDao介面,做查詢功能
public List<Student> findAll() {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Student> list=new ArrayList<Student>();
try {
//1.得到連線物件
conn=JDBCUtil.getConn();
String sql="select *from stu";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Student stu=new Student();
stu.setId(rs.getInt("id"));
stu.setAge(rs.getInt("age"));
stu.setName(rs.getString("name"));
stu.setNum(rs.getString("number"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(conn, ps,rs);
}
return list;
}
}