javaweb學生管理系統的課程設計
1.設計任務書
1.1 簡單的學生管理體統
本人計劃編寫一個簡單的學生管理系統,主要從兩個身份來說——老師和學生,對於老師來說,帶驗證碼的登入註冊,對學生進行增刪改查,以分頁的形式展示學生的所有資訊;對於學生來說,帶驗證碼的登入註冊,以及查詢自己的資訊,修改自己的資訊,以及查詢其他學生的資訊除密碼以外。具體要求如下:
1、實現資料庫連線,資料庫有兩個表,一個教師表,一個學生表教師表只有簡單的username和password兩個屬性,學生表有sid name password gender birthday address等屬性;
2、學生資訊的增刪改查;
3、寫出老師及學生的登入註冊的程式碼;
4、用程式碼生成一張驗證碼;
5、對學生資訊進行分頁
6、在網頁上具體操作,並顯示結果
1.2 本系統涉及的知識點
函式、資料庫、jsp、servlet、面向物件、動畫
2.功能設計(左對齊,宋體粗四號)
2.1 演算法設計
本系統需要實現的功能要求:
教師登陸成功後會有以下選擇如圖1所示:
(1) 選擇要操作:
(2) 選擇5後,進入到如圖2所示的選單,輸入要查詢學生的學號,SelectServlet拿到相應的資料後,通過建立物件呼叫Student類的select方法進行查詢,然後SelectServlet把查到的資料傳給selectstudent.jsp在網頁上顯示出來如圖3所示的主選單
(3) 根據所選選單編寫相應程式碼:
求查詢函式:
2.2 部分模組流程圖
3.程式程式碼設計
(1) 資料庫連線(需要引入jar包)
public class DBO {
static String url="jdbc:mysql://localhost:3306/dl" ;
tatic String user="root";
static String password="123";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn=null;
try {
conn=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
(2) 老師和學生類
a.老師類
public class Teacher {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
b.學生類
id sid name password gender birthday address屬性以及相應的get set方法(與上面相同)
(3) 學生登入註冊以及老師要操作的增刪改查
3.1.學生相關操作的介面
public interface StudentManager {
//登入時
public Student CheckLogin(String name,String sid,String password);
//新增學生資訊
public boolean addStudent(Student student);
//更新學生資訊
public boolean updateStudent(String sid,Student student);
//刪除學生資訊
public boolean deleteStudent(String sid);
//查詢所有的學生資訊
public List<Student> getAllStudent(String sid);
}
3.2.學生相關操作的介面的實現類
public class StudentManagerImpl implements StudentManager {
public Student CheckLogin(String name, String sid,String password) {
Student s=null;
Connection conn=null;
Statement st=null;
ResultSet rs=null;
String sql="select * from student";
conn=DBO.getConnection();
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) {
String nm=rs.getString("name");
String id=rs.getString("sid");
String psd=rs.getString("password");
if(nm.equals(name) && id.equals(sid) && psd.equals(password)) {
s= new Student();
s.setName(nm);
s.setSid(id);
s.setPassword(psd);
s.setGender(rs.getString("gender"));
s.setAddress(rs.getString("address"));
s.setId(rs.getInt("id"));
s.setBirthday(rs.getString("birthday"));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
public boolean addStudent(Student student) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="insert into student(sid,name,password,gender,birthday,address) values(?,?,?,?,?,?)";
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
pst.setString(1,student.getSid());
pst.setString(2,student.getName());
pst.setString(3, student.getPassword());
pst.setString(4,student.getGender());
pst.setString(5,student.getBirthday());
pst.setString(6,student.getAddress());
pst.executeUpdate();
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public boolean updateStudent(String sid, Student student) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="update student set name=?,password=?,gender=?,birthday=?,address=? where sid='"+sid+"'";
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
pst.setString(1,student.getName());
pst.setString(2,student.getPassword());
pst.setString(3,student.getGender());
pst.setString(4,student.getBirthday());
pst.setString(5,student.getAddress());
pst.executeUpdate();
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public boolean deleteStudent(String sid) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="delete from student where sid='"+sid+"'";
System.out.println(sql);
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
int row=pst.executeUpdate();
if(row>0)
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public List<Student> getAllStudent(String sid) {
// TODO Auto-generated method stub
List<Student> list=new ArrayList<Student>();
String sql="select * from student where sid='"+sid+"'";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
conn=DBO.getConnection();
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) {
Student s=new Student();
s.setSid(sid);
s.setName(rs.getString("name"));
s.setPassword(rs.getString("password"));
s.setGender(rs.getString("gender"));
s.setBirthday(rs.getString("birthday"));
s.setAddress(rs.getString("address"));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
3.3登陸的servlet
public class CheckLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("username");
String sid=request.getParameter("sid");
String password=request.getParameter("password");
String checkcode=request.getParameter("checkcode");
String piccode=(String) request.getSession().getAttribute("piccode");
StudentManager smg = new StudentManagerImpl();
Student s=new Student();
s=smg.CheckLogin(name, sid,password);
HttpSession session=request.getSession();
session.setAttribute("s", s);
if(s!=null && checkcode.equalsIgnoreCase(piccode)) {
response.sendRedirect("person.jsp");
}else{
response.sendRedirect("error.jsp");
}
}
}
3.4註冊和登入的servlet道理相似
(4) 老師的登入註冊與學生的相似(就已不在寫了)
(5) 驗證嗎
public class PicCodeGenerator extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
BufferedImage bi=new BufferedImage(68,22,BufferedImage.TYPE_INT_BGR);
Graphics g=bi.getGraphics();
Color color=new Color(224,205,19);
g.setColor(color);
g.fillRect(0, 0, 68,22);
char [] c="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random random= new Random();
int len=c.length;
int temp;
StringBuffer sb=new StringBuffer(4);
for(int i = 0 ; i<4 ; i++){
char index = c[(int)(0+Math.random()*c.length)];
Font font = new Font("幼圓",Font.BOLD,25);
Color clo = new Color(random.nextInt(255));
g.setFont(font);
g.setColor(clo);
g.drawString(index+"",i*15,18);
g.setColor(new Color(random.nextInt(150),random.nextInt(220),random.nextInt(180)));
sb.append(index);
}
for(int i=0;i<5;i++) {
g.drawLine(random.nextInt(68),random.nextInt(22),random.nextInt(68), random.nextInt(22));
}
request.getSession().setAttribute("piccode", sb.toString());
ImageIO.write(bi, "JPG", response.getOutputStream());
}
}
(6) 分頁
6.1servlet
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
StudentService studentService = new StudentService();
String uri = request.getRequestURI()+"?"+request.getQueryString();
int index=uri.indexOf("StudentServlet");
if(index==-1)
uri="http://localhost:8080/student/StudentServlet?null";
index = uri.lastIndexOf("&pc=");
if(index != -1 )
uri = uri.substring(0,index); //獲取當前頁頁碼
int pc = 1;
String param = request.getParameter("pc");
if(param != null && !param.trim().isEmpty()){
//將string型別強制轉化為Int型別
pc = Integer.parseInt(param);
}
PageBean<Student> pageBean = studentService.findByPageBean(pc);
//pageBean.setPc(pc);
pageBean.setUrl(uri);
//將資料放到Request域
request.setAttribute("pb", pageBean);
//轉發
request.getRequestDispatcher("Partner.jsp").forward(request, response);
}
}
4.介面及執行效果、測試工具與測試效果
4.1身份選擇(碰到教師和學生,這兩個詞會轉動,老師這個小人會左右移動)
4.11選擇教師後,進入老師登入介面,下面有超連結返回和註冊
(1)填入正確的username和password以及驗證碼嗎後進入
(2)對學生進行增刪改查操作
(3)用分頁查詢所有學生(上下一頁可以跳轉)
4.12選擇學生後,進入學生登入介面,下面有超連結返回和註冊
(1) 學生登陸成功的介面
(1) 學生登陸成功的介面
5.設計總結
1.本人在程式設計中遇到了非常多的困難,通過找他人請教,百度,以及翻閱書籍,一點一點解決錯誤,收穫頗多,學到了非常多的知識,以及更加鞏固我以前的知識
2.除錯程式:先判斷是哪一步程式出錯,縮小範圍在具體修改
比如:登陸失敗時
6.致謝
通過這次的實踐,我不僅對學到的東西加以綜合,鞏固,以及學到許多新的知識,收穫頗多,這也是我第一次做的小專案,雖然比較簡單,但感到非常充實,也更加的熟悉如何除錯程式,等等。
參考文獻
《Java基礎教程》
http://www.imooc.com/
http;www.baidu.com/
《傳智播客_2015年java基礎視訊-深入淺出版》
等等