ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
用jsp、javabean做了一个学生信息管理系统,虽然j2ee的框架很多,但是基础仍然很重要。麻雀虽小五脏俱全,希望本博客对j2ee初学者有帮助,也是对自己知识的复习和整合。 ### 系统预览: ### 管理员登录界面 ![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdd7d6d2f.jpg "") ### 录入信息界面 ![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdd803cf8.jpg "") ### 管理界面 ![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdd82fa07.jpg "") ### 修改学生信息 ![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdd85d066.jpg "") 系统比较简单,有增删改查四个基本功能。 源码下载地址:[学生信息管理系统源码] ([http://download.csdn.net/detail/napoay/9411126](http://download.csdn.net/detail/napoay/9411126)) 线上访问地址:[http://120.27.46.201:8080/studentmanager](http://120.27.46.201:8080/studentmanager)(用户名:admin,密码:123456) ### 核心代码 ### jdbc连接数据库 ~~~ package cn.ac.ucas.conn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Conn { public Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://127.0.0.1/student?useUicode=true&characterEncoding=utf-8"; String user="root"; String password="123456"; conn=DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return conn; } } ~~~ ### 管理员登陆 在数据库中有一张user表,有用户名和密码两个字段,登录时将用户输入的用户名和密码和数据库中的管理员毕竟,用户名和密码输入正确登录成功。 #### login.jsp ~~~ <jsp:useBean id="user" class="cn.ac.ucas.model.User"></jsp:useBean> <jsp:useBean id="userservice" class="cn.ac.ucas.service.UserService"></jsp:useBean> <jsp:setProperty property="*" name="user" /> <% if (userservice.validUser(user)) { session.setAttribute("user", user); %> <jsp:forward page="main.jsp"></jsp:forward> <% } else { %> <jsp:forward page="index.jsp"></jsp:forward> <% } %> ~~~ #### 验证用户名和密码 ~~~ public boolean validUser(User user) { try { pstmt = conn.prepareStatement("select * from user where username=?and password=?"); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); ResultSet rst = pstmt.executeQuery(); if (rst.next()) { return true; } else { return false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } ~~~ ### 使用session 用户名和密码输入正确的时候把user放到seesion中: ~~~ session.setAttribute("user", user); ~~~ 用户名和密码输入不正确不能之间访问登录后的页面,不然用户直接输入地址就可以访问到管理页面,登录和不登录没有区别。下面试checklogin.jsp,需要登录后访问的页面使用jsp:include包含在内,这样如果session中没有user信息返回到首页,不能直接查看。 ~~~ <%@page import="cn.ac.ucas.model.User"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% User user= (User)session.getAttribute("user"); if(user==null){ %> <jsp:forward page="index.jsp"></jsp:forward> <% } %> ~~~ 退出时移出session ~~~ session.removeAttribute("user"); ~~~ ### 增 ~~~ public boolean addStu(Student student) { try { pstmt = conn.prepareStatement( "insert into Student(nickname,name,gender,birth,majority,course,interest,otherinfo)" + "values(?,?,?,?,?,?,?,?)"); pstmt.setString(1, student.getNickname()); pstmt.setString(2, student.getName()); pstmt.setByte(3, student.getGender()); pstmt.setString(4, student.getBirth()); pstmt.setString(5, student.getMajority()); pstmt.setString(6, student.getCourses()); pstmt.setString(7, student.getInterests()); pstmt.setString(8, student.getOtherinfo()); pstmt.executeUpdate(); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } ~~~ ### 删 ~~~ public boolean deleteStu(int id) { try { pstmt = conn.prepareStatement("delete from Student where id=?"); pstmt.setInt(1, id); pstmt.executeUpdate(); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } ~~~ ### 改 ~~~ public boolean updateStu(Student student){ try { pstmt=conn.prepareStatement("update Student set nickname=?,name=?,gender=?,birth=?,majority=?,course=?,interest=?,otherinfo=? where id=?"); pstmt.setString(1, student.getNickname()); pstmt.setString(2, student.getName()); pstmt.setByte(3, student.getGender()); pstmt.setString(4, student.getBirth()); pstmt.setString(5, student.getMajority()); pstmt.setString(6, student.getCourses()); pstmt.setString(7, student.getInterests()); pstmt.setString(8, student.getOtherinfo()); pstmt.setInt(9, student.getId()); pstmt.executeUpdate(); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } ~~~ ### 查 #### 查询所有学生信息 ~~~ public List getAllStudent() { List stuList = new ArrayList(); try { pstmt = conn.prepareStatement("select * from Student"); ResultSet rst = pstmt.executeQuery(); while (rst.next()) { Student stu = new Student(); stu.setId(rst.getInt(1)); stu.setNickname(rst.getString(2)); stu.setName(rst.getString(3)); stu.setGender(rst.getByte(4)); stu.setBirth(rst.getString(5)); stu.setMajority(rst.getString(6)); if (rst.getString(7) != null) { stu.setCourse(rst.getString(7).split("&&")); } if (rst.getString(8) != null) { stu.setInterest(rst.getString(8).split("&&")); } stu.setOtherinfo(rst.getString(9)); stuList.add(stu); } return stuList; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } ~~~ #### 查询单个学生信息 ~~~ public Student getStuById(int id) { try { pstmt = conn.prepareStatement("select * from Student where id = ?"); pstmt.setInt(1, id); ResultSet rst = pstmt.executeQuery(); if(rst.next()){ Student stu=new Student(); stu.setId(rst.getInt(1)); stu.setNickname(rst.getString(2)); stu.setName(rst.getString(3)); stu.setGender(rst.getByte(4)); stu.setBirth(rst.getString(5)); stu.setMajority(rst.getString(6)); if (rst.getString(7) != null) { stu.setCourse(rst.getString(7).split("&&")); } if (rst.getString(8) != null) { stu.setInterest(rst.getString(8).split("&&")); } stu.setOtherinfo(rst.getString(9)); return stu; } return null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } ~~~