Cách thêm sửa xóa lập trình hướng đối tượng | Đại học Kinh tế Kỹ thuật Công nghiệp

 Dưới đây là hướng dẫn cơ bản về cách thực hiện các thao tác thêm, sửa và xóa đối tượng trong lập trình hướng đối tượng (OOP) bằng ngôn ngữ Python. Mình sẽ sử dụng lớp Sinh viên từ mẫu trước đó làm ví dụ Để thêm sinh viên vào danh sách, bạn có thể tạo một lớp quản lý sinh viên (ví dụ: StudentManager) để lưu trữ các đối tượng sinh viên.

Themsuaxoa
public static Connection kncsdl() throws SQLException
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection c =
DriverManager.getConnection("jdbc:mysql://localhost:3306/bai1_qlsach","root","");
return c;
} catch (ClassNotFoundException ex) {
Logger.getLogger(ketnoi.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public ResultSet getData(String sql) throws SQLException
{
ResultSet rs;
Statement st = kncsdl().createStatement();
rs = st.executeQuery(sql);
return rs;
}
public void edit(String sql) throws SQLException
{
Statement st = kncsdl().createStatement();
st.executeUpdate(sql);
}
login
String user = request.getParameter("user");
String pass = request.getParameter("pass");
if(user.equals("admin@gmail.com")&&pass.equals("123"))
{
HttpSession Session = request.getSession();
Session.setAttribute("ktra", true);
response.sendRedirect("Welcome");
}
else{
response.sendRedirect("login.jsp");
}
<form action="LoginServlet" method="post">
<table>
<tr>
<th>Email:</th>
<td><input type="text" name="user"></td>
</tr>
<tr>
<th>Pass:</th>
<td><input type="text" name="pass"></td>
</tr>
<tr>
<th></th>
<td><input type="submit" value="Dang nhap">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
Welcome
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession ss = request.getSession();
Boolean ktra = (Boolean)ss.getAttribute("ktra");
if (ktra!=null&&ktra) {
List<sach> list = new ArrayList<>();
ketnoi kn = new ketnoi();
String seasrch = request.getParameter("search");
String getid = request.getParameter("getid");
try {
String sql = "select * from qlsach";
if (seasrch != null && !seasrch.isEmpty()) {
sql += " where id = '" + seasrch + "'";
}
if(getid!=null&&!getid.isEmpty())
{
String sql1 = "Delete from qlsach where id = '"+getid+"'";
kn.edit(sql1);
}
ResultSet rs = kn.getData(sql);
while (rs.next()) {
list.add(new sach(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
} catch (SQLException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex);
}
request.setAttribute("tam", seasrch);
request.setAttribute("list", list);
request.getRequestDispatcher("display.jsp").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
Display
<div>
<form action="Welcome" method="get">
Nhap ID: <input type="search" name="search" value="${tam}">
<input type="submit" value="Tim">
</form>
</div>
<div>
<table border="1px">
<tr>
<th>ID</th>
<th>Ten Sach</th>
<th>Nha xuat ban</th>
<th>Nam XB</th>
</tr>
<%
List<sach> list =(List<sach>) request.getAttribute("list");
if(list!=null){
for(sach a : list)
{
%>
<tr>
<td><%= a.getId()%></td>
<td><%= a.getTen()%></td>
<td><%= a.getNxb()%></td>
<td><%= a.getNamxb()%></td>
<td><a href="update?getid=<%= a.getId()%>"><input type="submit"
value="UPDATE"></a>
<a href="Welcome?getid=<%= a.getId()%>"><input type="submit"
value="DELETE"></a>
</td>
</tr>
<%
}
}
%>
</table>
</div>
<div>
<a href="them"><input type="submit" value="ADD"></a>
</div>
Theem protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("them.jsp");
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String ten = request.getParameter("ten");
String nxb = request.getParameter("nxb");
String namxb = request.getParameter("namxb");
ketnoi kn = new ketnoi();
String sql = "insert into qlsach values('"+id+"','"+ten+"','"+nxb+"','"+namxb+"')";
try {
kn.edit(sql);
} catch (SQLException ex) {
Logger.getLogger(them.class.getName()).log(Level.SEVERE, null, ex);
}
response.sendRedirect("Welcome");
}
<form action="them" method="post">
<table border="1px">
<tr>
<th>ID</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>Ten</th>
<td><input type="text" name="ten"></td>
</tr>
<tr>
<th>Nha xuat ban</th>
<td><input type="text" name="nxb"></td>
</tr>
<tr>
<th>Nam xuat ban</th>
<td><input type="text" name="namxb"></td>
</tr>
<tr>
<th></th>
<td><input type="submit" value="THEM"></td>
</tr>
</table>
</form>
Update
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("getid");
ketnoi kn = new ketnoi();
String seasrch = request.getParameter("search");
try {
String sql = "select * from qlsach";
ResultSet rs = kn.getData(sql);
while (rs.next()) {
if(id.equals(rs.getString(1)))
{
request.setAttribute("id", rs.getString(1));
request.setAttribute("ten", rs.getString(2));
request.setAttribute("nxb", rs.getString(3));
request.setAttribute("namxb", rs.getString(4));
}
}
} catch (SQLException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex);
}
request.getRequestDispatcher("update.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String ten = request.getParameter("ten");
String nxb = request.getParameter("nxb");
String namxb = request.getParameter("namxb");
ketnoi kn = new ketnoi();
String sql = "update qlsach set ten = '"+ten+"',nxb = '"+nxb+"',namxb = '"+namxb+"'where
id = '"+id+"'";
try {
kn.edit(sql);
} catch (SQLException ex) {
Logger.getLogger(them.class.getName()).log(Level.SEVERE, null, ex);
}
response.sendRedirect("Welcome");
}
<form action="update" method="post">
<table border="1px">
<tr>
<th>ID</th>
<td><input type="text" name="id" value="${id}" readonly="false"></td>
</tr>
<tr>
<th>Ten</th>
<td><input type="text" name="ten" value="${ten}"></td>
</tr>
<tr>
<th>Nha xuat ban</th>
<td><input type="text" name="nxb" value="${nxb}"></td>
</tr>
<tr>
<th>Nam xuat ban</th>
<td><input type="text" name="namxb" value="${namxb}"></td>
</tr>
<tr>
<th></th>
<td><input type="submit" value="UPDATE"></td>
</tr>
| 1/6

Preview text:

Themsuaxoa
public static Connection kncsdl() throws SQLException { try {
Class.forName("com.mysql.cj.jdbc.Driver"); Connection c =
DriverManager.getConnection("jdbc:mysql://localhost:3306/bai1_qlsach","root",""); return c;
} catch (ClassNotFoundException ex) {
Logger.getLogger(ketnoi.class.getName()).log(Level.SEVERE, null, ex); } return null; }
public ResultSet getData(String sql) throws SQLException { ResultSet rs;
Statement st = kncsdl().createStatement(); rs = st.executeQuery(sql); return rs; }
public void edit(String sql) throws SQLException {
Statement st = kncsdl().createStatement(); st.executeUpdate(sql); } login
String user = request.getParameter("user");
String pass = request.getParameter("pass");
if(user.equals("admin@gmail.com")&&pass.equals("123")) {
HttpSession Session = request.getSession();
Session.setAttribute("ktra", true);
response.sendRedirect("Welcome"); } else{
response.sendRedirect("login.jsp"); }

Email:
Pass:

Welcome
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession ss = request.getSession();
Boolean ktra = (Boolean)ss.getAttribute("ktra");
if (ktra!=null&&ktra) {
List list = new ArrayList<>(); ketnoi kn = new ketnoi();
String seasrch = request.getParameter("search");
String getid = request.getParameter("getid"); try {
String sql = "select * from qlsach";
if (seasrch != null && !seasrch.isEmpty()) {
sql += " where id = '" + seasrch + "'"; }
if(getid!=null&&!getid.isEmpty()) {
String sql1 = "Delete from qlsach where id = '"+getid+"'"; kn.edit(sql1); }
ResultSet rs = kn.getData(sql); while (rs.next()) {
list.add(new sach(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4))); } } catch (SQLException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex); }
request.setAttribute("tam", seasrch);
request.setAttribute("list", list);
request.getRequestDispatcher("display.jsp").forward(request, response); } else {
response.sendRedirect("login.jsp"); } Display Nhap ID:

IDTen SachNha xuat banNam XB
<%= a.getId()%><%= a.getTen()%><%= a.getNxb()%><%= a.getNamxb()%>value="UPDATE"> value="DELETE">


Theem protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("them.jsp"); } /**
* Handles the HTTP POST method. *
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs */ @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String ten = request.getParameter("ten");
String nxb = request.getParameter("nxb");
String namxb = request.getParameter("namxb"); ketnoi kn = new ketnoi();
String sql = "insert into qlsach values('"+id+"','"+ten+"','"+nxb+"','"+namxb+"')"; try { kn.edit(sql); } catch (SQLException ex) {
Logger.getLogger(them.class.getName()).log(Level.SEVERE, null, ex); }
response.sendRedirect("Welcome"); }

ID
Ten
Nha xuat ban
Nam xuat ban

Update */ @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("getid"); ketnoi kn = new ketnoi();
String seasrch = request.getParameter("search"); try {
String sql = "select * from qlsach";
ResultSet rs = kn.getData(sql); while (rs.next()) {
if(id.equals(rs.getString(1))) {
request.setAttribute("id", rs.getString(1));
request.setAttribute("ten", rs.getString(2));
request.setAttribute("nxb", rs.getString(3));
request.setAttribute("namxb", rs.getString(4)); } } } catch (SQLException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex); }
request.getRequestDispatcher("update.jsp").forward(request, response); } @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String ten = request.getParameter("ten");
String nxb = request.getParameter("nxb");
String namxb = request.getParameter("namxb"); ketnoi kn = new ketnoi();
String sql = "update qlsach set ten = '"+ten+"',nxb = '"+nxb+"',namxb = '"+namxb+"'where id = '"+id+"'"; try { kn.edit(sql); } catch (SQLException ex) {
Logger.getLogger(them.class.getName()).log(Level.SEVERE, null, ex); }
response.sendRedirect("Welcome"); }

ID
Ten
Nha xuat ban
Nam xuat ban