Delete Data through Bean


<web-app>

<servlet>
<servlet-name>us</servlet-name>
<servlet-class>in.google.ControlServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>us</servlet-name>
<url-pattern>/ControlServlet</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>us1</servlet-name>
<servlet-class>in.google.ControlServlet1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>us1</servlet-name>
<url-pattern>/Del</url-pattern>
</servlet-mapping>

</web-app>



Bean-




import java.sql.*;
import java.util.*;
public class LogBean
{

public LogBean()
{
super();
}


 String name;
 String password;
 int productid;
 String productname;
 String category;
 String hallmark;
 String caret;
 String design;
 String weight;
 String quantity;
 double price;
 double makingcharges;
 double marketprice;
 String record;
 String date;
 String tax;
 String vender;
 String country ;




public LogBean(String name,String password,int productid,String productname,String category, String hallmark,String caret,String design,String weight,String quantity,double price,double makingcharges,double marketprice,String record,String date,String tax,String vender,String country)

{
//super();,

this.name = name;
this.password=password;
this.productid = productid;
this.productname=productname;
this.category = category;
this.hallmark = hallmark;
this.caret = caret;
this.design = design;
this.weight=weight;
this.quantity = quantity;
this.price = price;
this.makingcharges=makingcharges;
this.marketprice=marketprice;
this.record=record;
this.date=date;
this.tax=tax;
this.vender=vender;
this.country=country;


}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public int getProductid()
{
return productid;
}
public void setProductid(int productid)
{
this.productid=productid;
}
public String getProductname()
{
return productname;
}
public void setProductname(String productname)
{
this.productname=productname;
}
public String getCategory()
{
return category;
}
public void setCategory(String category)
{
this.category=category;
}
public String getHallmark()
{
return hallmark;
}
public void setHallmark(String hallmark)
{
this.hallmark=hallmark;
}
public String getCaret()
{
return caret;
}
public void setCaret(String caret)
{
this.caret=caret;
}
public String getDesign()
{
return design;
}
public void setDesign(String design)
{
this.design=design;
}
public String getWeight()
{
return weight;
}
public void setWeight(String weight)
{
this.weight=weight;
}
public String getQuantity()
{
return quantity;
}
public void setQuantity(String quantity)
{
this.quantity=quantity;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=price;
}
public double getMakingcharges()
{
return makingcharges;
}
public void setMakingcharges(double makingcharges)
{
this.makingcharges=makingcharges;
}
public double getMarketprice()
{
return marketprice;
}
public void setMarketprice(double marketprice)
{
this.marketprice=marketprice;
}
public String getRecord()
{
return record;
}
public void setRecord(String record)
{
this.record=record;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date=date;
}
public String getTax()
{
return tax;
}
public void setTax(String tax)
{
this.tax=tax;
}
public String getVender()
{
return vender;
}
public void setVendar(String vender)
{
this.vender=vender;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country=country;
}




public boolean validate()
{
if(password.equals("admin"))
{
return true;
}
else
{
return false;
}
}


public void save()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/jewellry","root","root");
java.sql.PreparedStatement pst=con.prepareStatement("insert into product(name,password,productid,productname,category,hallmark,caret,design,weight,quantity,price,makingcharges,marketprice,record,date,tax,vender,country) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

pst.setString(1,name);
pst.setString(2,password);
pst.setInt(3,productid);
pst.setString(4,productname);
pst.setString(5,category);
pst.setString(6, hallmark);
pst.setString(7,caret);
pst.setString(8,design);
pst.setString(9, weight);
pst.setString(10, quantity);
pst.setDouble(11, price);
pst.setDouble(12,makingcharges);
pst.setDouble(13,marketprice);
pst.setString(14,record);
pst.setString(15,date);
pst.setString(16,tax);
pst.setString(17,vender);
pst.setString(18,country);
pst.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}

}
public ArrayList<LogBean> view()
{
ArrayList<LogBean> ar=new ArrayList<LogBean>();
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/jewellry","root","root");
String sql="select*from product";
java.sql.PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery(sql);
while(rs.next())
{

 String name=rs.getString(1);
 String password=rs.getString(2);
 int productid=rs.getInt(3);
 String productname=rs.getString(4);
 String category=rs.getString(5);
 String hallmark=rs.getString(6);
 String caret=rs.getString(7);
 String design=rs.getString(8);
 String weight=rs.getString(9);
 String quantity=rs.getString(10);
 double price=rs.getDouble(11);
 double makingcharges=rs.getDouble(12);
 double marketprice=rs.getDouble(13);
 String record=rs.getString(14);
 String date=rs.getString(15);
 String tax=rs.getString(16);
 String vender=rs.getString(17);
 String country=rs.getString(18);
 ar.add(new LogBean(name,password,productid,productname,category,hallmark,caret,design,weight,quantity,price,makingcharges,marketprice,record,date,tax,vender,country));
}
}

catch(Exception e)
{
System.out.println(e);
}
return ar;
}
public int delete(String hallmark)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/jewellry","root","root");
String sql="delete from product where hallmark=?";
java.sql.PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,hallmark);
pst.executeUpdate();

}

catch(Exception e)
{
System.out.println(e);
}
return 1;
}
}


log-suc.jsp



<%@page import="in.google.LogBean"%>
<%@page import="java.util.*"%>
<jsp:useBean id="log" class="in.google.LogBean"/>
<p>You are successfuly logged in!</p>
<table border="2">
<tr><th>Name</th><th>Password</th><th>ProductId</th><th>Productname</th><th>Category</th><th>Hallmark</th><th>Caret</th><th>Design</th><th>Weight</th><th>Quantity</th><th>Price</th><th>Makingcharges</th><th>Marketprice</th><th>Record</th><th>Date</th><th>Tax</th><th>Vender</th><th>Country</th><th>Delete</th></tr>
<tr>
<%
LogBean bean=(LogBean)request.getAttribute("bean");

ArrayList list=log.view();
Iterator itr=list.iterator();
while(itr.hasNext())
{
LogBean l=(LogBean)itr.next();
%>


<td><%=l.getName()%></td>
<td><%=l.getPassword()%></td>
<td><%=l.getProductid()%></td>
<td><%=l.getProductname()%></td>
<td><%=l.getCategory()%></td>
<td><%=l.getHallmark()%></td>
<td><%=l.getCaret()%></td>
<td><%=l.getDesign()%></td>
<td><%=l.getWeight()%></td>
<td><%=l.getQuantity()%></td>
<td><%=l.getPrice()%></td>
<td><%=l.getMakingcharges()%></td>
<td><%=l.getMarketprice()%></td>
<td><%=l.getRecord()%></td>
<td><%=l.getDate()%></td>
<td><%=l.getTax()%></td>
<td><%=l.getVender()%></td>
<td><%=l.getCountry()%></td>
<td><a href="Del?id=<%=l.getHallmark()%>">Delete</a></td>
</tr>
<%
}
%>
</table>



controller-




import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ControlServlet1 extends HttpServlet
{
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();


String id=request.getParameter("id");



LogBean bean=new LogBean();
int i=bean.delete(id);
if(i>0)
{
//request.getRequestDispatcher("log-suc.jsp").include(request,response);
response.sendRedirect("log-suc.jsp");
}
else
{
request.getRequestDispatcher("log-err.jsp").include(request,response);
}

}
}


Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.