jsp+servlet+mysql實現班級資訊管理的web小專案
阿新 • • 發佈:2019-01-06
jsp+servlet+mysql實現班級資訊管理的javaweb小專案
功能描述:能夠建立和檢視班級列表,以及檢視班級的學生資訊,可以為每個班級新增學生。除此之外還實現了一個登陸介面。
在最後加上了資料庫建立的sql語句
專案下載地址:https://download.csdn.net/download/qq_29924227/10349307
專案主要參考了:http://blog.csdn.net/BlueSky_USC/article/details/72803068
登陸介面為:
班級資訊管理介面為:
學生資訊管理介面為:
專案目錄結構:
bean包裡的是用來儲存班級資訊的ClassInfoBean,java和用來儲存學生資訊的StudentInfoBean,java
dao包裡為兩個頁面的資料庫操作
servlet包裡為每個頁面對應的邏輯處理
util包裡提供對資料庫操作的工具類
下面為全部程式碼
ClassInfoBean.java
package bean;
public class ClassInfoBean {
/**
* 屬性和資料庫中的欄位相對應
*/
private Integer cid;
private String cname;
public ClassInfoBean() {
}
public Integer getCid() {
return cid;
}
public void setCid (Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
StudentInfoBean.java
package bean;
import java.math.BigDecimal;
public class StudentInfoBean {
private Integer sno;
private String sname;
private String ssex;
private Integer sage;
private Integer cid;
private BigDecimal sbalance;
public BigDecimal getSbalance() {
return sbalance;
}
public void setSbalance(BigDecimal sbalance) {
this.sbalance = sbalance;
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
}
ClassInfoDao.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.ClassInfoBean;
public class ClassInfoDao {
String CLASSFORNAME="com.mysql.jdbc.Driver";
String SERVANDDB="jdbc:mysql://localhost:3306/studentsystem?useSSL=false";
String USER="root";
String PWD="123456";
public void addClassInfo(ClassInfoBean bean) {
Connection conn = null;
Statement stmt = null;
String cid,cname;
try {
Class.forName(CLASSFORNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 獲取資料庫連線
conn = DriverManager
.getConnection(SERVANDDB,USER,PWD);
// 整理一條SQL語句
// System.out.println(bean.getCid());
cid=bean.getCid().toString();
cname=bean.getCname();
String sql = "INSERT INTO class_info (cid,cname) VALUES ('"+cid+"','"
+ cname + "')";
// 建立SQL執行物件
stmt = conn.createStatement();
// 執行sql語句
int row = stmt.executeUpdate(sql);
if (row != 1) {
throw new RuntimeException("新增班級失敗!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public List<ClassInfoBean> findAll() {
Connection conn = null;
Statement stmt = null;
List<ClassInfoBean> classList= new ArrayList<ClassInfoBean>();
try {
Class.forName(CLASSFORNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 獲取連線
conn = DriverManager
.getConnection(SERVANDDB,USER,PWD);
// 整理一條SQL語句
String sql = "select cid,cname from class_info";
// 建立執行sql的物件
stmt = conn.createStatement();
//執行sql語句
ResultSet rs =stmt.executeQuery(sql);
//遍歷結果集
while(rs.next()){
int cid =rs.getInt("cid");
String cname=rs.getString("cname");
ClassInfoBean bean = new ClassInfoBean();
bean.setCid(cid);
bean.setCname(cname);
classList.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return classList;
}
}
StudentInfoDao.java
package dao;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DBUtil;
import bean.ClassInfoBean;
import bean.StudentInfoBean;
public class StudentInfoDao {
public void addStudentInfo(StudentInfoBean bean) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 獲取資料庫連線
conn=DBUtil.getConnection();
// 整理一條SQL語句
String sql = "INSERT INTO student_info (sno,sname,ssex,sage,cid,sbalance) VALUES (?,?,?,?,?,?)";
// 建立SQL執行物件
pstmt=conn.prepareStatement(sql);
// 給預處理物件賦值
pstmt.setInt(1, bean.getSno());
pstmt.setString(2, bean.getSname());
pstmt.setString(3, bean.getSsex());
pstmt.setInt(4, bean.getSage());
pstmt.setInt(5, bean.getCid());
pstmt.setBigDecimal(6, BigDecimal.ZERO);
//執行SQL語句
int row=pstmt.executeUpdate();
if (row != 1) {
throw new RuntimeException("新增學生資訊失敗!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.release(conn, pstmt, null);
}
}
public List<StudentInfoBean> findAllStudentByClassId(int cid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs =null;
List<StudentInfoBean> stuList= new ArrayList<StudentInfoBean>();
try {
// 獲取連線
conn = DBUtil.getConnection();
// 整理一條SQL語句
//String sql = "SELECT sno,sname,ssex,sage,sbalance from student_info where cid=?";
// 建立執行sql的物件
pstmt = conn.prepareStatement("SELECT cid,sno,sname,ssex,sage,sbalance from student_info where cid=?");
//執行sql語句
pstmt.setInt(1, cid);
rs =pstmt.executeQuery();
//遍歷結果集
while(rs.next()){
int scid =rs.getInt("cid");
int sno=rs.getInt("sno");
String name=rs.getString("sname");
String ssex=rs.getString("ssex");
int sage=rs.getInt("sage");
BigDecimal sbalance =rs.getBigDecimal("sbalance");
//封裝成學生Bean物件
StudentInfoBean bean = new StudentInfoBean();
bean.setCid(scid);
bean.setSno(sno);
bean.setSname(name);
bean.setSage(sage);
bean.setSsex(ssex);
bean.setSbalance(sbalance);
//新增到佇列中
stuList.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.release(conn, pstmt, rs);
}
return stuList;
}
}
CheckLogin .java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckLogin
*/
@WebServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckLogin() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
if(request.getParameter("username").equals("admin")&&request.getParameter("password").equals("123456")) {
response.sendRedirect("classInfo.jsp");
}
else {
response.sendRedirect("login.jsp");
request.setAttribute("ifture", "ture");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
ClassInfoServlet.java
package 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 bean.ClassInfoBean;
import dao.ClassInfoDao;
public class ClassInfoServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//設定編碼,防止請求亂碼
req.setCharacterEncoding("UTF-8");
//獲取引數
String className=req.getParameter("className");
String classId=req.getParameter("classId");
//System.out.println(classId);
//建立ClassInfoBean物件儲存資訊
ClassInfoBean bean =new ClassInfoBean();
bean.setCname(className);
bean.setCid(Integer.valueOf(classId));
System.out.println(bean.getCid());
//建立資料庫操作物件
ClassInfoDao dao =new ClassInfoDao();
//新增班級資訊到資料庫
dao.addClassInfo(bean);
//查詢所有班級資訊
List<ClassInfoBean> classInfo=dao.findAll();
//儲存查詢的班級資訊
req.setAttribute("classInfo", classInfo);
//轉發請求
req.getRequestDispatcher("/classInfo.jsp").forward(req, resp);
}
}
StudentInfoServlet.java
package servlet;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.StudentInfoBean;
import dao.StudentInfoDao;
public class StudentInfoServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//設定編碼防止響應亂碼
req.setCharacterEncoding("UTF-8");
//獲取請求型別
String reqType=req.getParameter("reqType");
if("FIND_STUDENT_BY_CLASS_ID".equals(reqType)){
//獲取客戶端輸入的引數
int cid=Integer.parseInt(req.getParameter("cid"));
//建立學生資料庫操作物件
StudentInfoDao dao = new StudentInfoDao();
List<StudentInfoBean> allStudentInfo=dao.findAllStudentByClassId(cid);
//儲存查詢到的學生資訊
req.setAttribute("allStudentInfo", allStudentInfo);
//將查詢結果交給jsp處理
req.getRequestDispatcher("/studentInfo.jsp").forward(req, resp);
}
if("ADD_STU_INFO".equals(reqType)){
//獲取客戶端輸入的引數
int cid=Integer.parseInt(req.getParameter("classId"));
String stuname=req.getParameter("stuSname");
String stusex=req.getParameter("stuSsex");
int stuage=Integer.parseInt(req.getParameter("stuSage"));
int sno=Integer.parseInt(req.getParameter("stuSno"));
StudentInfoBean bean =new StudentInfoBean();
bean.setCid(cid);
bean.setSbalance(BigDecimal.ZERO);
bean.setSage(stuage);
bean.setSname(stuname);
bean.setSsex(stusex);
bean.setSno(sno);
//建立學生資料庫操作物件
StudentInfoDao dao = new StudentInfoDao();
dao.addStudentInfo(bean);
List<StudentInfoBean> allStudentInfo=dao.findAllStudentByClassId(cid);
//儲存查詢到的學生資訊到req作用域中
req.setAttribute("allStudentInfo", allStudentInfo);
//將查詢結果交給jsp處理
req.getRequestDispatcher("/studentInfo.jsp?cid="+cid+"").forward(req, resp);
}
}
}
DBUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
static String CLASSFORNAME="com.mysql.jdbc.Driver";
static String SERVANDDB="jdbc:mysql://localhost:3306/studentsystem?useSSL=false";
static String USER="root";
static String PWD="123456";
static{
// 載入資料庫驅動
try {
Class.forName(CLASSFORNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
// 獲取資料庫連線
conn = DriverManager
.getConnection(SERVANDDB,
USER, PWD);
}catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 關閉相關連線
public static void release(Connection conn,Statement stmt ,ResultSet rs){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
classInfo,jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="bean.ClassInfoBean" %>
<%@ page import ="java.util.List" %>
<!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>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div style="margin:0 auto;margin-top:20px;">
<ul>
<li><a href="#home">主頁</a></li>
<li><a href="#news">使用幫助</a></li>
<li><a href="#contact">個人資訊</a></li>
</ul>
</div>
<div style="margin:0 auto;margin-top: 40px;">
<form action="classInfoReq" method="POST">
<center>
<table width="40%"; border="1"; bgcolor="#FFFFC6">
<caption style="margin-bottom: 20px">
新增班級資訊
</caption>
<tr>
<td >班級名稱:</td>
<td><input type="text" name="className"></td>
</tr>
<tr>
<td>班級序號:</td>
<td><input type="text" name="classId"></td>
</tr>
<tr>
<td><input type="reset" value="重置"></td>
<td><input type="submit" value="確定"></td>
</tr>
</table>
</center>
</form>
</div>
<div style="margin:0 auto;margin-top: 40px;">
<center>
<table width="40%"; border="1"; bgcolor="#FFFFC6">
<caption style="margin-bottom: 20px">
班級資訊列表
</caption>
<tr>
<th>班級序號</th>
<th>班級名稱</th>
<th>班級明細</th>
</tr>
<% List<ClassInfoBean> classInfo=(List<ClassInfoBean>)request.getAttribute("classInfo");
if(classInfo!=null && !classInfo.isEmpty()){
for(ClassInfoBean classes : classInfo){
%>
<tr>
<td><%=classes.getCid() %></td>
<td><%=classes.getCname()%></td>
<td>
<a href="http://localhost:8080/class/findAllStudentByClassId?reqType=FIND_STUDENT_BY_CLASS_ID&cid=<%=classes.getCid()%>"
>檢視明細
</a>
</td>
</tr>
<%
}
}
%>
</table>
</center>
</div>
<select>
<option value="value1"> 選項1</option>
<option value="value2"> 選項2</option>
</select>
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>insert title</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<div style="margin:0 auto;">
<form action="CheckLogin" method="post">
<center>
<tr>
<td> 使用者名稱:</td>
<td> <input type="text" name="username"></td>
</tr>
<br>
<tr>
<td> 密碼: </td>
<td><input type="text" name="password"></td>
</tr>
<br>
<tr>
<td><input type="submit" value="確定"></td>
</tr>
</center>
</from>
</div>
<%
if(request.getParameter("ifture")!=null){
}
%>
</body>
</html>
main.css
這個是導航欄的css格式檔案
@charset "UTF-8";
th{
text-align: center;
}
td{
text-align: center;
}
body{
background-color: AliceBlue
}
ul
{
list-style-type:none;
margin:0 auto;
overflow:hidden;
}
li
{
float:left;
}
a:link,a:visited
{
display:block;
width:120px;
font-weight:bold;
color:#FFFFFF;
background-color:#bebebe;
text-align:center;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}
a:hover,a:active
{
background-color:#cc0000;
}
studentInfo.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="bean.StudentInfoBean"%>
<!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>
<style type="text/css">
tr {
text-align: center;
}
</style>
</head>
<body>
<div style="margin: 0 auto; margin-top: 40px;">
<form action="addStuInfoReq?reqType=ADD_STU_INFO" method="POST">
<center>
<table width="40%" ; border="1" ; bgcolor="#FFFFC6">
<caption style="margin-bottom: 20px">新增學生資訊</caption>
<tr>
<td>學生學號:</td>
<td> <input type="text" name="stuSno"></td>
</tr>
<tr>
<td>學生姓名:</td>
<td><input type="text" name="stuSname"></td>
</tr>
<tr>
<td>學生性別:</td>
<td><select width="300px" name="stuSsex">
<option>男</option>
<option>女</option>
</select></td>
</tr>
<tr>
<td>學生年齡:</td>
<td><input type="text" name="stuSage"> <input
type="hidden" name="classId"
value="<%=request.getParameter("cid")%>"></td>
</tr>
<tr>
<td><input type="reset" value="重置"></td>
<td><input type="submit" value="確定"></td>
</tr>
</table>
</center>
</form>
</div>
<div style="margin: 0 auto; margin-top: 40px;">
<center>
<table width="80%" ; border="1" ; bgcolor="#FFFFC6">
<caption style="margin-bottom: 20px">學生資訊列表</caption>
<tr>
<th>所屬班級</th>
<th>學生學號</th>
<th>學生姓名</th>
<th>學生性別</th>