当前位置:网站首页>Simple student management

Simple student management

2022-06-23 08:57:00 An Li Jiu Ge

Catalog

One 、 Demand analysis

Two 、 database

3、 ... and 、 Entity class

Four 、mvc Pattern

5、 ... and 、 Interface


One 、 Demand analysis

1) Build table :

        You need to create four tables , Class table 、 Student list 、 Teachers list 、 Hobby list

2) Inquire about

You can query by multiple conditions : According to the class 、 Teachers' 、 Like to query

3) newly added

4) modify

Pay attention to echo when modifying

5) Pagination

Show the page , A few pages in total , How many pieces of data are recorded in total , You can select a page to jump to

6) Delete

Two 、 database

1) Create a new database and table

        

2) add value , To test

3、 ... and 、 Entity class

        1) Establish the required entity classes according to the tables in the database

  Class name Clazz In order to prevent with keyword Class repeat

        2)  Define variables and required construction methods for entity classes

for example :

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 + "]";
	}
	
}

 

Four 、mvc Pattern

1) Define database auxiliary classes

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;

/**
 *  Provides a set of methods to get or close database objects 
 * 
 */
public class DBHelper {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {//  The static block executes once , load   Drive once 
		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);
		}
	}

	/**
	 *  Get the data connection object 
	 * 
	 * @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(" Database connection ( close ) success ");
	}
}

 

 2) Define the methods required for the function

for example : 

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(" Temporarily no data ");
		}
		
		
	}

}

5、 ... and 、 Interface

1) main interface

2) Add interface

  The result after adding

 

3) Modify the interface

After modification  

 

 4) Delete

 

  After deleting

 5) Inquire about

  After query

 

原网站

版权声明
本文为[An Li Jiu Ge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230839534083.html