当前位置:网站首页>Student addition / deletion gaih
Student addition / deletion gaih
2022-06-29 09:49:00 【lion tow】
Today I would like to share a simple web Small projects , If you don't read the nonsense, let's go straight to work :
Catalog
One 、 Idea diagram

Realize the idea :
Here we are using Mysql database Establish the four tables required for this project Student list 、 Teachers list 、 Class table 、 And hobby list
The development tools used this time eclipse Import the required jar Package and project package :

Two 、 Coding work
1.util package DBHelper class :

package com.zq.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 myClose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(ps!=null) {
ps.close();
}
if(con!=null&&!con.isClosed()) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
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. Entity class :

clss:
package com.zq.entity;
import java.io.Serializable;
/**
* Class
* @author Administrator
*
*/
public class Class implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5145856861606131091L;
private int cid;
private String cname;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Class() {
// TODO Auto-generated constructor stub
}
public Class(int cid, String cname) {
super();
this.cid = cid;
this.cname = cname;
}
@Override
public String toString() {
return "Class [cid=" + cid + ", cname=" + cname + "]";
}
}
hobby class :
package com.zq.entity;
import java.io.Serializable;
/**
* Hobbies
* @author Administrator
*
*/
public class Hobby implements Serializable{
/**
*
*/
private static final long serialVersionUID = -9120395110708701114L;
private String hid;
private String hname;
public String getHid() {
return hid;
}
public void setHid(String hid) {
this.hid = hid;
}
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
public Hobby() {
// TODO Auto-generated constructor stub
}
public Hobby(String hid, String hname) {
super();
this.hid = hid;
this.hname = hname;
}
@Override
public String toString() {
return "Hobby [hid=" + hid + ", hname=" + hname + "]";
}
}
Student class :
package com.zq.entity;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable{
private static final long serialVersionUID = 7154831938131519366L;
private int sid;
private String sname;
private Teacher t;
private Class c;
// private Hobby h;
private String ss;// To increase xiugai
private List<Hobby> ls;// Used to bind values
// Define a
public int getSid() {
return sid;
}
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Teacher getT() {
return t;
}
public void setT(Teacher t) {
this.t = t;
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
public List<Hobby> getLs() {
return ls;
}
public void setLs(List<Hobby> ls) {
this.ls = ls;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, Teacher t, Class c, List<Hobby> ls) {
super();
this.sid = sid;
this.sname = sname;
this.t = t;
this.c = c;
this.ls = ls;
}
public Student(String sname, Teacher t, Class c, String ss) {
this.sname = sname;
this.t = t;
this.c = c;
this.ss = ss;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", t=" + t + ", c=" + c + ", ls=" + ls + "]";
}
}
teacher class :
package com.zq.entity;
import java.io.Serializable;
/**
* Teachers
* @author Administrator
*
*/
public class Teacher implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4984197172186331612L;
private int tid;
private String tname;
private Class c;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(int tid, String tname, Class c) {
this.tid = tid;
this.tname = tname;
this.c = c;
}
@Override
public String toString() {
return "Teacher [tid=" + tid + ", tname=" + tname + ", c=" + c + "]";
}
}
3.dao class
calssDao:
package com.zq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zq.entity.Class;
import com.zq.util.DBHelper;
public class ClssDao implements IClassDao{
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
@Override
public List<Class> getAll() {
List<Class> ls=new ArrayList<Class>();
try {
con=DBHelper.getConnection();
String sql="select * from tb_class";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Class c=new Class();
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
ls.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public Class getOne(int cid) {
Class c=new Class();
try {
con=DBHelper.getConnection();
String sql="select * from tb_class where cid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, cid);
rs=ps.executeQuery();
if(rs.next()) {
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return c;
}
}
IcalssDao:
package com.zq.dao;
import java.util.List;
import com.zq.entity.Class;
public interface IClassDao {
public List<com.zq.entity.Class> getAll();
public Class getOne(int cid);
}
hobbyDao:
package com.zq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zq.entity.Hobby;
import com.zq.util.DBHelper;
public class HobbyDao implements IHobbyDao{
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
@Override
public List<Hobby> getAll() {
List<Hobby> ls=new ArrayList<Hobby>();
try {
con=DBHelper.getConnection();
String sql="select * from tb_hobby";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Hobby h=new Hobby();
h.setHid(rs.getString(1));
h.setHname(rs.getString(2));
ls.add(h);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public Hobby getHobby(String hid) {
Hobby h=new Hobby();
try {
con=DBHelper.getConnection();
String sql="select * from tb_hobby where hid=?";
ps=con.prepareStatement(sql);
ps.setString(1, hid);
rs=ps.executeQuery();
if(rs.next()) {
h.setHid(rs.getString(1));
h.setHname(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return h;
}
}
IhobbyDao:
package com.zq.dao;
import java.util.List;
import com.zq.entity.Hobby;
public interface IHobbyDao {
public List<Hobby> getAll();
public Hobby getHobby(String hid);
}
StudentDao:
package com.zq.dao;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Student;
import com.zq.entity.Teacher;
import com.zq.util.DBHelper;
public class StudentDao implements IStudentDao{
// Three brothers
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
// Call hobby dao layer
IHobbyDao ihd=new HobbyDao();
@Override
public List<Student> getAll() {
List<Student> ls=new ArrayList<Student>();
try {
con=DBHelper.getConnection();
String sql="select * from tb_student";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
// Instantiate a hobby array
List<Hobby> lss=new ArrayList<>();
Student stu=new Student();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setT(new BjJyAhDao().getTeacher(rs.getInt(3)));
stu.setC(new BjJyAhDao().getOne(rs.getInt(4)));
String sa = rs.getString(5);
// Split with a comma
String[] io = sa.split(",");
for (String kk : io) {
// Call to query a single method
Hobby h = ihd.getHobby(kk);
lss.add(h);
}
stu.setLs(lss);
ls.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public int addStu(Student stu) {
int n=0;
try {
con=DBHelper.getConnection();
String sql="insert into tb_student(sname,tid,cid,hid) values(?,?,?,?)";
ps=con.prepareStatement(sql);
ps.setString(1, stu.getSname());
ps.setInt(2, stu.getT().getTid());
ps.setInt(3, stu.getC().getCid());
ps.setString(4, stu.getSs());
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
@Override
public int delStu(int sid) {
int n=0;
try {
con=DBHelper.getConnection();
String sql="delete from tb_student where sid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, sid);
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
@Override
public int updStu(int sid, Student stu) {
int n=0;
try {
con=DBHelper.getConnection();
String sql="update tb_student set sname=?,tid=?,cid=?,hid=? where sid=?";
ps=con.prepareStatement(sql);
ps.setString(1, stu.getSname());
ps.setInt(2, stu.getT().getTid());
ps.setInt(3, stu.getC().getCid());
// ps.setString(4, stu.getH().getHid());
ps.setString(4, stu.getSs());
ps.setInt(5, sid);
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
@Override
public Student getStu(int sid) {
Student stu=new Student();
try {
con=DBHelper.getConnection();
String sql="select * from tb_student where sid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, sid);
rs=ps.executeQuery();
if(rs.next()) {
// Instantiate a hobby array
List<Hobby> lss=new ArrayList<>();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setT(new BjJyAhDao().getTeacher(rs.getInt(3)));
stu.setC(new BjJyAhDao().getOne(rs.getInt(4)));
String sa = rs.getString(5);
// Split with a comma
String[] io = sa.split(",");
for (String kk : io) {
// Call to query a single method
Hobby h = ihd.getHobby(kk);
lss.add(h);
}
stu.setLs(lss);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return stu;
}
@Override
public List<Student> getMH(String ctr, String htr, String str, int pageInde, int pageSize) {
List<Student> ls=new ArrayList<Student>();
int a=(pageInde-1)*pageSize;
try {
con=DBHelper.getConnection();
String sql="select * from tb_student where hid like '%"+htr+"%' and tid like '%"+str+"%' and cid like '%"+ctr+"%' limit ?,?";
ps=con.prepareStatement(sql);
ps.setInt(1, a);
ps.setInt(2, pageSize);
rs=ps.executeQuery();
while(rs.next()) {
// Instantiate a hobby array
List<Hobby> lss=new ArrayList<>();
Student stu=new Student();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setT(new BjJyAhDao().getTeacher(rs.getInt(3)));
stu.setC(new BjJyAhDao().getOne(rs.getInt(4)));
String sa = rs.getString(5);
// Split with a comma
String[] io = sa.split(",");
for (String kk : io) {
// Call to query a single method
Hobby h = ihd.getHobby(kk);
lss.add(h);
}
stu.setLs(lss);
ls.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public int getRows(String str) {
int n=0;
try {
con=DBHelper.getConnection();
String sql="select count(*) from "+str;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
}
IStudentDao:
package com.zq.dao;
import java.util.List;
import com.zq.entity.Student;
public interface IStudentDao {
public List<Student> getAll();
public int addStu(Student stu);
public int delStu(int sid);
public int updStu(int sid,Student stu);
public Student getStu(int sid);
public List<Student> getMH(String ctr,String htr,String str,int pageInde,int pageSize);
public int getRows(String str);
}
TeacherDao:
package com.zq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zq.entity.Teacher;
import com.zq.util.DBHelper;
public class TeacherDao implements ITeacherDao{
// Three brothers
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
@Override
public List<Teacher> getAll() {
List<Teacher> ls=new ArrayList<Teacher>();
try {
con=DBHelper.getConnection();
String sql="select * from tb_teacher";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Teacher t=new Teacher();
t.setTid(rs.getInt(1));
t.setTname(rs.getString(2));
ls.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public Teacher getTeacher(int tid) {
Teacher t=new Teacher();
BjJyAhDao bjJyAhDao = new BjJyAhDao();
try {
con=DBHelper.getConnection();
String sql="select * from tb_teacher where tid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, tid);
rs=ps.executeQuery();
if(rs.next()) {
t.setTid(rs.getInt(1));
t.setTname(rs.getString(2));
t.setC(bjJyAhDao.getOne(rs.getInt(3)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return t;
}
}
ITeacherDao:
package com.zq.dao;
import java.util.List;
import com.zq.entity.Teacher;
public interface ITeacherDao {
public List<Teacher> getAll();
public Teacher getTeacher(int tid);
}
4.biz layer :
Here is an example :
StudentBiz:
package com.zq.biz;
import java.util.List;
import com.zq.dao.IStudentDao;
import com.zq.dao.StudentDao;
import com.zq.entity.Student;
public class StudentBiz implements IStudentBiz{
IStudentDao isd=new StudentDao();
@Override
public List<Student> getAll() {
return isd.getAll();
}
@Override
public int addStu(Student stu) {
return isd.addStu(stu);
}
@Override
public int delStu(int sid) {
return isd.delStu(sid);
}
@Override
public int updStu(int sid, Student stu) {
return isd.updStu(sid, stu);
}
@Override
public Student getStu(int sid) {
return isd.getStu(sid);
}
@Override
public List<Student> getMH(String ctr, String htr, String str, int pageInde, int pageSize) {
return isd.getMH(ctr, htr, str, pageInde, pageSize);
}
@Override
public int getRows(String str) {
return isd.getRows(str);
}
}
IStudentBiz:
package com.zq.biz;
import java.util.List;
import com.zq.entity.Student;
public interface IStudentBiz {
public List<Student> getAll();
public int addStu(Student stu);
public int delStu(int sid);
public int updStu(int sid,Student stu);
public Student getStu(int sid);
public List<Student> getMH(String ctr, String htr, String str, int pageInde, int pageSize);
public int getRows(String str);
}
5.servlet class :
1.AddServlet
package com.zq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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.zq.biz.ClassBiz;
import com.zq.biz.HobbyBiz;
import com.zq.biz.IClassBiz;
import com.zq.biz.IHobbyBiz;
import com.zq.biz.IStudentBiz;
import com.zq.biz.ITeacherBiz;
import com.zq.biz.StudentBiz;
import com.zq.biz.TeacherBiz;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Student;
import com.zq.entity.Teacher;
@WebServlet("/AddServlet")
public class AddServlet 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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
// take out
PrintWriter out = response.getWriter();
// Receive form value transfer
String sname = request.getParameter("sname");
String tid = request.getParameter("teacher");
String cid = request.getParameter("class");
String[] hobby = request.getParameterValues("aa");
String ss="";
for (String string : hobby) {
ss+=string;
}
// Call the business logic layer
IStudentBiz isb=new StudentBiz();
ITeacherBiz itb=new TeacherBiz();
Teacher t = itb.getTeacher(Integer.parseInt(tid));
IClassBiz icb=new ClassBiz();
Class c = icb.getOne(Integer.parseInt(cid));
Student stu=new Student(sname, t, c, ss);
int n = isb.addStu(stu);
if(n>0) {// Join the success
out.print("<script>alert(' Increase success ');location.href='index.jsp';</script>");
}
else {// Join the failure
out.print("<script>alert(' Add failure ');location.href='add.jsp';</script>");
}
}
}
2.DeleteServlet
package com.zq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.zq.biz.IStudentBiz;
import com.zq.biz.StudentBiz;
@WebServlet("/DeleteServlet")
public class DeleteServlet 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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
// Receive form value transfer
String sid = request.getParameter("sid");
// Call the business logic layer
IStudentBiz isb=new StudentBiz();
int n = isb.delStu(Integer.parseInt(sid));
if(n>0) {
out.print("<script>alert(' Delete successful ');location.href='index.jsp';</script>");
}
else {
out.print("<script>alert(' Delete failed ');location.href='index.jsp';</script>");
}
}
}
3.IndexServlet
package com.zq.servlet;
import java.io.IOException;
import java.util.ArrayList;
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.zq.biz.ClassBiz;
import com.zq.biz.HobbyBiz;
import com.zq.biz.IClassBiz;
import com.zq.biz.IHobbyBiz;
import com.zq.biz.IStudentBiz;
import com.zq.biz.ITeacherBiz;
import com.zq.biz.StudentBiz;
import com.zq.biz.TeacherBiz;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Student;
import com.zq.entity.Teacher;
@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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
// Define page number
int pageInde=1;
int pageSize=4;
// Receive form value transfer
String ctr = request.getParameter("ctr");// class
String htr = request.getParameter("htr");// hobby
String str = request.getParameter("str");// teacher
if(str==null) {
str="";
}
if(htr==null) {
htr="";
}
if(ctr==null) {
ctr="";
}
// Check all classes
IClassBiz icb=new ClassBiz();
List<Class> lc = icb.getAll();
// Check all the teachers
ITeacherBiz itb=new TeacherBiz();
List<Teacher> lt = itb.getAll();
// Query all students
IStudentBiz isb=new StudentBiz();
// List<Student> ls = isb.getAll();
String pid=request.getParameter("pid");
if(pid!=null) {
pageInde=Integer.parseInt(pid);
}
int rows=isb.getRows("tb_student");
int max=rows/pageSize;
if(rows%pageSize!=0) {
max++;
}
if(max==0) {
max=1;
}
// Paging with fuzzy queries
List<Student> ls = isb.getMH(ctr, htr, str, pageInde, pageSize);
// Check out all your hobbies
IHobbyBiz ihb=new HobbyBiz();
List<Hobby> lh = ihb.getAll();
if(ls.size()!=0&<.size()!=0&&lc.size()!=0&&lh.size()!=0) {
request.setAttribute("pageIndex", pageInde);
request.setAttribute("ls", ls);
request.setAttribute("la", lt);
request.setAttribute("lc", lc);
request.setAttribute("lh", lh);
request.setAttribute("max", max);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else {
System.out.println(" Collection is empty ");
}
}
}
4. PreAddServlet
package com.zq.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.zq.biz.ClassBiz;
import com.zq.biz.HobbyBiz;
import com.zq.biz.IClassBiz;
import com.zq.biz.IHobbyBiz;
import com.zq.biz.ITeacherBiz;
import com.zq.biz.TeacherBiz;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Teacher;
@WebServlet("/PreAddServlet")
public class PreAddServlet 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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
// Call the business logic layer
IHobbyBiz ihb=new HobbyBiz();
IClassBiz icb=new ClassBiz();
ITeacherBiz itb=new TeacherBiz();
List<Hobby> lh = ihb.getAll();
List<Class> lc = icb.getAll();
List<Teacher> lt = itb.getAll();
if(lh.size()!=0&&lc.size()!=0&<.size()!=0) {
request.setAttribute("lh", lh);
request.setAttribute("lc", lc);
request.setAttribute("la", lt);
request.getRequestDispatcher("add.jsp").forward(request, response);
}
else {
System.out.println(" Collection is empty ");
}
}
}
5.PreUpdateServlet
package com.zq.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.zq.biz.ClassBiz;
import com.zq.biz.HobbyBiz;
import com.zq.biz.IClassBiz;
import com.zq.biz.IHobbyBiz;
import com.zq.biz.IStudentBiz;
import com.zq.biz.ITeacherBiz;
import com.zq.biz.StudentBiz;
import com.zq.biz.TeacherBiz;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Student;
import com.zq.entity.Teacher;
@WebServlet("/PreUpdateServlet")
public class PreUpdateServlet 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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
// receive
String sid = request.getParameter("sid");
// Call the business logic layer
IStudentBiz isb=new StudentBiz();
Student stu = isb.getStu(Integer.parseInt(sid));
IHobbyBiz ihb=new HobbyBiz();
IClassBiz icb=new ClassBiz();
ITeacherBiz itb=new TeacherBiz();
List<Hobby> lh = ihb.getAll();
List<Class> lc = icb.getAll();
List<Teacher> lt = itb.getAll();
request.setAttribute("lh", lh);
request.setAttribute("lc", lc);
request.setAttribute("la", lt);
request.setAttribute("stu", stu);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
}
6. PreUpdateServlet1
package com.zq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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.zq.biz.ClassBiz;
import com.zq.biz.HobbyBiz;
import com.zq.biz.IClassBiz;
import com.zq.biz.IHobbyBiz;
import com.zq.biz.IStudentBiz;
import com.zq.biz.ITeacherBiz;
import com.zq.biz.StudentBiz;
import com.zq.biz.TeacherBiz;
import com.zq.entity.Class;
import com.zq.entity.Hobby;
import com.zq.entity.Student;
import com.zq.entity.Teacher;
@WebServlet("/PreUpdateServlet1")
public class PreUpdateServlet1 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 {
// Set the encoding mode
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
// Receive form value transfer
String sid = request.getParameter("sid");
String sname = request.getParameter("sname");
String tid = request.getParameter("teacher");
String cid = request.getParameter("class");
String[] hobby = request.getParameterValues("aa");
String ss="";
for (String string : hobby) {
ss+=string;
}
// Call the business logic layer
IStudentBiz isb=new StudentBiz();
ITeacherBiz itb=new TeacherBiz();
Teacher t = itb.getTeacher(Integer.parseInt(tid));
IClassBiz icb=new ClassBiz();
Class c = icb.getOne(Integer.parseInt(cid));
Student stu=new Student(sname, t, c, ss);
int n = isb.updStu(Integer.parseInt(sid),stu);
if(n>0) {// Join the success
out.print("<script>alert(' Modification successful ');location.href='index.jsp';</script>");
}
else {// Join the failure
out.print("<script>alert(' Modification successful ');location.href='PreUpdateServlet?sid="+sid+"';</script>");
}
}
}
3、 ... and 、jsp page
① main interface :
Code :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<center>
<form method="post" action="IndexServlet">
<c:if test="${empty la}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty la}">
<select name="str">
<c:forEach var="t" items="${la}">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</c:if>
<c:if test="${empty lc}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lc}">
<select name="ctr">
<c:forEach var="c" items="${lc}">
<option value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
</c:if>
<c:if test="${empty lh}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lh}">
<c:forEach items="${lh}" var="h">
<input type="checkbox" name="htr" value="${h.hid},"/>${h.hname}
</c:forEach>
</c:if>
<input type="submit" value=" Inquire about "/>
</form>
<c:if test="${empty ls}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty ls}">
<table border="1px">
<tr>
<td> Student number </td>
<td> The student's name </td>
<td> Students' teachers </td>
<td> The class the students are in </td>
<td> Students' hobbies </td>
<td> operation <a href="add.jsp"> increase </a></td>
</tr>
<c:forEach items="${ls}" var="s">
<tr>
<td>${s.sid}</td>
<td>${s.sname}</td>
<td>${s.t.tname}</td>
<td>${s.c.cname}</td>
<td>
<c:forEach items="${s.ls}" var="sd">
${sd.hname}
</c:forEach>
</td>
<td>
<a onclick="return confirm(' Are you sure you want to delete ?');" href="DeleteServlet?sid=${s.sid}"> Delete </a>
<a href="PreUpdateServlet?sid=${s.sid}"> modify </a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="6">
<a href="IndexServlet?pid=1"> home page </a>
<a href="IndexServlet?pid=${pageIndex>1?pageIndex-1:1}"> The previous page </a>
[${pageIndex}/${max}]
<a href="IndexServlet?pid=${pageIndex<max?pageIndex+1:max}"> The next page </a>
<a href="IndexServlet?pid=${max}"> Tail page </a>
</td>
</tr>
</table>
</c:if>
</center>
</body>
</html>② Add interface :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
<script type="text/javascript">
function myf(){
location.href="index.jsp";
}
</script>
</head>
<body>
<center>
<form action="AddServlet" method="post">
<table border="1px">
<tr>
<td> The student's name </td>
<td><input type="text" name="sname"/></td>
</tr>
<tr>
<td> teacher </td>
<td>
<c:if test="${empty la}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty la}">
<select name="teacher">
<c:forEach var="t" items="${la}">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</c:if>
</td>
</tr>
<tr>
<td> class </td>
<td>
<c:if test="${empty lc}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lc}">
<select name="class">
<c:forEach var="c" items="${lc}">
<option value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
</c:if>
</td>
</tr>
<tr>
<td> hobby </td>
<td>
<c:if test="${empty lh}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lh}">
<c:forEach items="${lh}" var="h">
<input type="checkbox" name="aa" value="${h.hid},"/>${h.hname}
</c:forEach>
</c:if>
</td>
</tr>
</table>
<input type="submit" value=" increase "/>
<input type="button" value=" Empty " onclick="myf()"/>
</form>
</center>
</body>
</html>③ Modify the interface :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<center>
<form action="PreUpdateServlet1?sid=${stu.sid}" method="post">
<table border="1px">
<tr>
<td> The student's name </td>
<td><input type="text" name="sname" value="${stu.sname}"/></td>
</tr>
<tr>
<td> teacher </td>
<td>
<select name="teacher">
<c:forEach var="t" items="${la}">
<option <c:if test="${t.tid==stu.t.tid}"> selected="selected" </c:if> value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td> class </td>
<td>
<select name="class">
<c:forEach var="c" items="${lc}">
<option <c:if test="${c.cid==stu.c.cid}"> selected="selected" </c:if> value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td> hobby </td>
<td>
<c:forEach items="${lh}" var="h">
<input <c:forEach items="${stu.ls}" var="oo"> <c:if test="${h.hname==oo.hname}"> checked="checked" </c:if> </c:forEach> type="checkbox" name="aa" value="${h.hid},"/>${h.hname}
</c:forEach>
</td>
</tr>
</table>
<input type="submit" value=" modify "/>
</form>
</center>
</body>
</html>边栏推荐
- JS获取手机型号和系统版本
- Gd32f4xx Ethernet chip (ENC28J60) driver migration
- Pytorch Summary - Automatic gradient
- Implementation of multi key state machine based on STM32 standard library
- Idea auto completion
- kdevelop新建工程
- InvalidConnectionAttributeException异常处理
- How to set Google Chrome as the default browser
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- 股票炒股账号开户安全吗?是靠谱的吗?
猜你喜欢

Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks

Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear

A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h

UE4 material UV texture does not stretch with model scale

UE4 remove the mask transparent white edge in the material

Yotact real-time instance segmentation

# 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)

Gd32f4xx Ethernet chip (ENC28J60) driver migration

Self cultivation (XXI) servlet life cycle, service method source code analysis, thread safety issues

Have you done the network security "physical examination" this year?
随机推荐
UE4 animation redirection
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
Go deep into RC, RS, daemonset and statefulset (VII)
Segmentation of Head and Neck Tumours Using Modified U-net
UE4 材质UV纹理不随模型缩放拉伸
长安链GO语言智能合约环境搭建及使用
UE4 blueprint modify get a copy in array to reference
Segmentation of Head and Neck Tumours Using Modified U-net
云管理平台:9大开源云管理平台(CMP)
zabbix4.4配置监控服务器指标,以及图形页乱码解决
367. 有效的完全平方数-二分法
MATLAB小技巧(21)矩阵分析--偏最小二乘回归
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
UE4 编译单个文件(VS与编辑器分别启动)
Pytorch summary learning series - data manipulation
微信小程序实现store功能
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
Pytorch Summary - Automatic gradient
五心公益红红娘团队
遍历vector容器中的对象的方式