JSP ile Veri Tabanından Kayıt Düzeltmek, silmek

-------------------------------------------------
Cihazlar.jsp
-------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:include page="header.jsp" />
<form method="post" action="cihazara">
<table border="1px">
<tr>
<td>SERİ NO:<input type="text" name="cihazSeriNo">
<td><input type="submit" value="ARA"></td>
<td><a href="cihazekle.jsp">Yeni Cihaz Ekle</a></td>
</tr>

</table>
<br>
</form>
<table border="1">
<tr>
<tr><td colspan="6" style="text-align: center; color: red " > KAYITLI CİHAZLAR </td></tr>
<td bgcolor="yellow"><h5>MARKA</h5></td>
<td bgcolor="yellow"><h5>MODEL</h5></td>
<td bgcolor="yellow"><h5>SERİ NUMARASI</h5></td>
<td bgcolor="yellow"><h5>AÇIKLAMA</h5></td>
<td bgcolor="yellow"><h5>DURUM</h5></td>
<td bgcolor="yellow"><h5>İŞLEMLER</h5></td>
<c:forEach items="${cihazlar}" var="cihazlar">

<tr>
<td>${cihazlar.cihazMarka}</td>
<td>${cihazlar.cihazModel}</td>
<td>${cihazlar.cihazSeriNo}</td>
<td>${cihazlar.cihazAciklama}</td>
<td style="color: gray"><c:choose>
<c:when test="${cihazlar.cihazDurum == 1}">
      "ZİMMETLİ"
    </c:when>
<c:otherwise>
        DEPODA
    </c:otherwise>
</c:choose></td>
</tr>
</tr>
</c:forEach>
<c:forEach items="${cihazSeri}" var="cihazlar">
<tr>
<input type="hidden" value="${cihazlar.cihazID}" name="cihazID">
<td>${cihazlar.cihazMarka}</td>
<td>${cihazlar.cihazModel}</td>
<td>${cihazlar.cihazSeriNo}</td>
<td>${cihazlar.cihazAciklama}</td>
<td style="color: gray"><c:choose>
<c:when test="${cihazlar.cihazDurum == 1}">
      "ZİMMETLİ"
    </c:when>
<c:otherwise>
        DEPODA
    </c:otherwise>
</c:choose></td>
<td>
<button onclick="window.location.href='cihazdurum?action=duzelt&cihazID=${cihazlar.cihazID}'">Düzelt</button>
<button onclick="window.location.href='cihazdurum?action=sil&cihazID=${cihazlar.cihazID}'">Sil</button>
<button onclick="window.location.href='cihazdurum?action=zimmet&cihazID=${cihazlar.cihazID}'">Zimmet Göster</button>

</td>
</tr>
</c:forEach>


</table>

<jsp:include page="footer.jsp" />

--------------------------------------------
cihazdurum.java (Servletimiz);
---------------------------------------------

package com.servlets;

import java.io.IOException;
import java.util.ArrayList;

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.dao.cihazlarDAO;
import com.dao.personelDAO;
import com.entity.cihazlar;
import com.entity.personel;


@WebServlet("/cihazdurum")
public class CihazDurumServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


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");
if (request.getParameter("action") != null)
        {
       
            String id = request.getParameter("cihazID");
            String action = request.getParameter("action");

            if (action.equals("duzelt"))
            {
             int cihazID =Integer.parseInt(request.getParameter("cihazID"));
             ArrayList cihazDegistir =new cihazlarDAO().cihazDegistir(cihazID);
         request.setAttribute("cihazDegistir", cihazDegistir);
         request.getRequestDispatcher("cihazduzelt.jsp").forward(request, response);
            
            }
            else if (action.equals("sil"))
            {
             int cihazID =Integer.parseInt(request.getParameter("cihazID"));
             cihazlarDAO cihazsill = new cihazlarDAO();
             cihazsill.cihazSil(cihazID);
             request.getRequestDispatcher("cihazlar.jsp").forward(request, response);
            }
            else if (action.equals("zimmet"))
            {
             int personelID =Integer.parseInt(request.getParameter("cihazID"));
            
            
            }
}
}
}

--------------------------------
cihazlarDAO.java
---------------------------------
public void cihazSil(int cihazID){

try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = ConnectionManager.getConnection();
String sql = "delete from cihazlar where cihazID = ?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, cihazID);
psmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

public void cihazlDuzelt (String cihazMarka, String cihazModel,
String cihazSeriNo, String cihazAciklama,int cihazID) {

try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = ConnectionManager.getConnection();
String sql = "update cihazlar set cihazMarka=?,cihazModel=?,cihazSeriNo=?,"
+ "cihazAciklama=? where cihazID = ?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, cihazMarka);
psmt.setString(2, cihazModel);
psmt.setString(3, cihazSeriNo);
psmt.setString(4, cihazAciklama);
psmt.setInt(5, cihazID);

psmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public ArrayList cihazDegistir(int cihazID){

ArrayList cihazlar = new ArrayList();
String sorgu = "select * from cihazlar where cihazID = ?;";
try {

Connection conn = ConnectionManager.getConnection();
PreparedStatement psmt = conn.prepareStatement(sorgu);
psmt.setInt(1,cihazID);

ResultSet rs = psmt.executeQuery();

while (rs.next()) {
cihazlar  cihaz = new cihazlar(rs.getInt("cihazID"),
rs.getString("cihazMarka"),
rs.getString("cihazModel"),
rs.getString("cihazSeriNo"),
rs.getString("cihazAciklama"),
rs.getString("cihazDurum"));
cihazlar.add(cihaz);

}
} catch (Exception e) {
e.printStackTrace();
}


-----------------------------------
cihazlar.java(Entitiy için);
-----------------------------------
package com.entity;

public class cihazlar {
private int cihazID;
private String cihazMarka;
private String cihazModel;
private String cihazSeriNo;
private String cihazAciklama;
private String cihazDurum;




public cihazlar(int cihazID, String cihazMarka, String cihazModel,
String cihazSeriNo, String cihazAciklama, String cihazDurum) {
super();
this.cihazID = cihazID;
this.cihazMarka = cihazMarka;
this.cihazModel = cihazModel;
this.cihazSeriNo = cihazSeriNo;
this.cihazAciklama = cihazAciklama;
this.cihazDurum = cihazDurum;
}




public String getCihazDurum() {
return cihazDurum;
}




public void setCihazDurum(String cihazDurum) {
this.cihazDurum = cihazDurum;
}




public int getCihazID() {
return cihazID;
}




public void setCihazID(int cihazID) {
this.cihazID = cihazID;
}




public String getCihazMarka() {
return cihazMarka;
}




public void setCihazMarka(String cihazMarka) {
this.cihazMarka = cihazMarka;
}




public String getCihazModel() {
return cihazModel;
}




public void setCihazModel(String cihazModel) {
this.cihazModel = cihazModel;
}




public String getCihazSeriNo() {
return cihazSeriNo;
}




public void setCihazSeriNo(String cihazSeriNo) {
this.cihazSeriNo = cihazSeriNo;
}




public String getCihazAciklama() {
return cihazAciklama;
}




public void setCihazAciklama(String cihazAciklama) {
this.cihazAciklama = cihazAciklama;
}




public cihazlar() {
// TODO Auto-generated constructor stub
}

 @Override
public String toString() {
// TODO Auto-generated method stub
return this.cihazID+" "+ this.cihazMarka +" "+this.cihazModel +
" "+this.cihazSeriNo+" "+this.cihazAciklama+" ";
}
}

---------------------------------
cihazduzelt.java(Servlet için).
----------------------------------
package com.servlets;

import java.io.IOException;

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.dao.cihazlarDAO;


@WebServlet("/cihazduzelt")
public class CihazDuzeltServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


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");
String cihazMarka = request.getParameter("cihazMarka");
String cihazModel = request.getParameter("cihazModel");
String cihazSeriNo = request.getParameter("cihazSeriNo");
String cihazAciklama = request.getParameter("cihazAciklama");

int cihazID =Integer.parseInt(request.getParameter("cihazID"));
    cihazlarDAO cihazDuzelt= new cihazlarDAO();
    cihazDuzelt.cihazlDuzelt(cihazMarka, cihazModel, cihazSeriNo, cihazAciklama, cihazID);
    request.getRequestDispatcher("cihazlar.jsp").forward(request,response);

}

}

------------------------------
cihazdüzelt.jsp
------------------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<jsp:include page="header.jsp" />

<form method="post" action="cihazduzelt">
<table border="1px solid gray">
<c:forEach items="${cihazDegistir}" var="cihazlar">

<tr>   <td>MARKA</td><td><input type="text" value= "${cihazlar.cihazMarka}" name="cihazMarka" > </td></tr>
<tr> <td>MODEL</td><td><input type="text" value= "${cihazlar.cihazModel}" name="cihazModel" ></td></tr>
<tr> <td>SERİ NO</td><td><input type="text" value= "${cihazlar.cihazSeriNo}" name="cihazSeriNo" ></td></tr>
<tr> <td>AÇIKLAMA</td><td><input type="text" value= "${cihazlar.cihazAciklama}" name="cihazAciklama" ></td></tr>

<input type="hidden" value="${cihazlar.cihazID}" name="cihazID">
<td colspan="2"><input type="submit" value="Değiştir"> </td>



</c:forEach>
</table>
</form>

Yorumlar