当前位置:网站首页>简易学生管理
简易学生管理
2022-06-23 08:40:00 【安离九歌】
目录
一、 需求分析
1)建表:
需要建立四张表,班级表、学生表、教师表、爱好表
2)查询
可以多条件查询:根据班级、教师、爱好进行查询
3)新增
4)修改
修改要注意值得回显
5)分页
显示第几页,共几页,总记录多少条数据,可以选定页面跳转
6)删除
二、数据库
1)新建数据库以及表

2)添加值,以便测试
三、实体类
1)根据数据库的表建立所需实体类

班级叫Clazz为了防止与关键词Class重复
2) 给实体类定义变量以及所需构造方法
例如:
package com.zhw.entity; import java.util.List; public class Student { private int sid; private String sname; private Clazz cid; private Teacher tid; private List<Hobby> hid; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Clazz getCid() { return cid; } public void setCid(Clazz cid) { this.cid = cid; } public Teacher getTid() { return tid; } public void setTid(Teacher tid) { this.tid = tid; } public List<Hobby> getHid() { return hid; } public void setHid(List<Hobby> hid) { this.hid = hid; } public Student() { // TODO Auto-generated constructor stub } public Student(int sid, String sname, Clazz cid, Teacher tid, List<Hobby> hid) { super(); this.sid = sid; this.sname = sname; this.cid = cid; this.tid = tid; this.hid = hid; } public Student(String sname, Clazz cid, Teacher tid, List<Hobby> hid) { super(); this.sname = sname; this.cid = cid; this.tid = tid; this.hid = hid; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", cid=" + cid + ", tid=" + tid + ", hid=" + hid + "]"; } }
四、mvc模式
1)定义数据库辅助类

package com.zhw.util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 提供了一组获得或关闭数据库对象的方法 * */ public class DBHelper { private static String driver; private static String url; private static String user; private static String password; static {// 静态块执行一次,加载 驱动一次 try { InputStream is = DBHelper.class .getResourceAsStream("config.properties"); Properties properties = new Properties(); properties.load(is); driver = properties.getProperty("driver"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("pwd"); Class.forName(driver); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * 获得数据连接对象 * * @return */ public static Connection getConnection() { try { Connection conn = DriverManager.getConnection(url, user, password); return conn; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void close(ResultSet rs) { if (null != rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Statement stmt) { if (null != stmt) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Connection conn) { if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Connection conn, Statement stmt, ResultSet rs) { close(rs); close(stmt); close(conn); } public static boolean isOracle() { return "oracle.jdbc.driver.OracleDriver".equals(driver); } public static boolean isSQLServer() { return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver); } public static boolean isMysql() { return "com.mysql.jdbc.Driver".equals(driver); } public static void main(String[] args) { Connection conn = DBHelper.getConnection(); DBHelper.close(conn); System.out.println("isOracle:" + isOracle()); System.out.println("isSQLServer:" + isSQLServer()); System.out.println("isMysql:" + isMysql()); System.out.println("数据库连接(关闭)成功"); } }
2)定义功能所需的方法

例如:
ClazzDaoImpl
package com.zhw.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.zhw.entity.Clazz; import com.zhw.entity.Hobby; import com.zhw.util.DBHelper; public class ClazzDaoImpl implements IClazzDao{ Connection conn = null; Statement stmt = null; ResultSet rs = null; @Override public List<Clazz> getAll() { List<Clazz> ls = new ArrayList<Clazz>(); try { conn = DBHelper.getConnection(); String sql = "select * from tb_class"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(sql); while(rs.next()) { Clazz cl = new Clazz(rs.getInt(1),rs.getString(2)); ls.add(cl); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.close(conn, stmt, rs); } return ls; } @Override public Clazz getdg(int cid) { Clazz cl = new Clazz(); try { conn = DBHelper.getConnection(); String sql = "select * from tb_class where cid="+cid+""; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(sql); while(rs.next()) { cl = new Clazz(rs.getInt(1),rs.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.close(conn, stmt, rs); } return cl; } public static void main(String[] args) { System.out.println(new ClazzDaoImpl().getdg(1)); } }IClazzDao
package com.zhw.dao; import java.util.List; import com.zhw.entity.Clazz; public interface IClazzDao { public List<Clazz> getAll(); public Clazz getdg(int cid); }IndexServlet
package com.zhw.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhw.biz.ClazzBizImpl; import com.zhw.biz.HyBizImpl; import com.zhw.biz.IClazzBiz; import com.zhw.biz.IHyBiz; import com.zhw.biz.IStuBiz; import com.zhw.biz.ITeacherBiz; import com.zhw.biz.StuBizImpl; import com.zhw.biz.TeacherBizImpl; import com.zhw.entity.Clazz; import com.zhw.entity.Hobby; import com.zhw.entity.Student; import com.zhw.entity.Teacher; /** * Servlet implementation class IndexServlet */ @WebServlet("/IndexServlet") public class IndexServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=UTF-8"); int pageIndex = 1; int pageSize = 2; String cid = request.getParameter("cid"); if(cid==null) { cid=""; } String tid= request.getParameter("tid"); if(tid==null) { tid=""; } String[] hids = request.getParameterValues("hid"); String hid = ""; String hidd =""; if(hids==null) { hidd=" "; hid=""; } else { for (String str : hids) { hidd+=str+" "; hid= " where hid like '%"+hidd+"%' "; } } String pid = request.getParameter("pid"); if(pid!=null){ pageIndex = Integer.parseInt(pid); } String gid = request.getParameter("gid"); // System.out.println("ska:"+gid); if(gid==null) { gid=""; } else if(gid=="") { gid=""; } else{ pageIndex = Integer.parseInt(gid); } IStuBiz isb = new StuBizImpl(); IHyBiz ihb = new HyBizImpl(); ITeacherBiz itb = new TeacherBizImpl(); IClazzBiz icb = new ClazzBizImpl(); List<Student> stu = isb.getAll(cid, tid,hid, pageIndex, pageSize); List<Hobby> hobby1 = ihb.getAll(); List<Teacher> teacher1 = itb.getAll(); List<Clazz> clas1 = icb.getAll(); int count = isb.count(cid, tid, hid); int pagecount=0; if(count%pageSize==1) { pagecount=count/pageSize+1; } else { pagecount=count/pageSize; } if(stu.size()!=0) { request.setAttribute("stu", stu); request.setAttribute("hobby1", hobby1); request.setAttribute("teacher1", teacher1); request.setAttribute("clas1", clas1); request.setAttribute("count", count); request.setAttribute("pagecount", pagecount); request.setAttribute("pageIndex", pageIndex); request.getRequestDispatcher("index.jsp").forward(request, response); } else { System.out.println("暂无数据"); } } }
五、界面
1)主界面

2)增加界面

增加后结果

3)修改界面

修改后

4)删除

删除后

5)查询

查询后

边栏推荐
- C # advanced learning -- virtual method
- 4- draw ellipse, use timer
- 1-gradients, shadows, and text
- How to sort a dictionary by value or key?
- [QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
- Basic use of check boxes and implementation of select all and invert selection functions
- 173. Binary Search Tree Iterator
- Top 25 most popular articles on vivo Internet technology in 2021
- 636. Exclusive Time of Functions
- How to restore visualizations and dashboards after kibana rebuilds the index
猜你喜欢

Qualcomm 9x07 two startup modes

173. Binary Search Tree Iterator

Why use growth neural gas network (GNG)?

6月《中国数据库行业分析报告》发布!智能风起,列存更生

Hongmeng reads the resource file

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“

Point cloud library PCL from introduction to mastery Chapter 10
![Paper reading [quovadis, action recognition? A new model and the dynamics dataset]](/img/3f/449cc91bfa66fcf26bc2cd405fb773.png)
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]

636. Exclusive Time of Functions

Multi-scale feature combination in target detection
随机推荐
Restore the default routing settings of the primary network card
4、 Database design
Go data types (II) overview of data types supported by go and Boolean types
In June, China database industry analysis report was released! Smart wind, train storage and regeneration
Dongyuhui, the "square face teacher", responded that the popularity was declining: do a good job of live broadcasting of agricultural products to benefit farmers and consider supporting education
“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册
How to restore visualizations and dashboards after kibana rebuilds the index
自组织映射神经网络(SOM)
Leetcode topic analysis count primes
社区文章|MOSN 构建 Subset 优化思路分享
Geoserver添加mongoDB数据源
Self organizing map neural network (SOM)
5、 Project management
[qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
Kernel log debugging method
523. Continuous Subarray Sum
Spirit matrix for leetcode topic analysis
Top 25 most popular articles on vivo Internet technology in 2021
Linux MySQL installation
6、 Web Architecture Design