Save Data through Bean Class

Bean Class-



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;
}
}

Controller-




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

String name=request.getParameter("name");
String password=request.getParameter("password");

int productid=Integer.parseInt(request.getParameter("productid"));
String productname=request.getParameter("productname");

String category=request.getParameter("category");
String hallmark=request.getParameter("hallmark");

String caret=request.getParameter("caret");
String design=request.getParameter("design");

String weight=request.getParameter("weight");
String quantity=request.getParameter("quantity");

double price=Double.parseDouble(request.getParameter("price"));
double makingcharges=Double.parseDouble(request.getParameter("makingcharges"));

double marketprice=Double.parseDouble(request.getParameter("marketprice"));
String record=request.getParameter("record");

String date=request.getParameter("date");
String tax=request.getParameter("tax");

String vender=request.getParameter("vender");
String country=request.getParameter("country");



LogBean bean=new LogBean(name,password,productid,productname,category,hallmark,caret,design,weight,quantity,price,makingcharges,marketprice,record,date,tax,vender,country);
bean.save();

request.setAttribute("bean",bean);
boolean status=bean.validate();
if(status)
{
request.getRequestDispatcher("log-suc.jsp").include(request,response);
}
else
{
request.getRequestDispatcher("log-err.jsp").include(request,response);
}
}
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}

view-




<html>
<head>
</head>
<body>
<center>
<form action="ControlServlet" method ="post">
<table border='5'>
<tr><td>Name</td><td><input type="text" name="name"></td>                        <td>Password</td><td><input type="password" name="password"></td></tr>
<tr><td>Product Id</td><td><input type="text" name="productid"></td>         <td>Product Name</td><td><input type="text" name="productname"></td></tr>
<tr><td>Category</td><td><input type="text" name="category"></td>              <td>Hallmark</td><td><input type="text" name="hallmark"></td></tr>
<tr><td>Caret</td><td><input type="text" name="caret"></td>                         <td>Design</td><td><input type="text" name="design"></td></tr>
<tr><td>Weight</td><td><input type="text" name="weight"></td>                     <td>Quantity</td><td><input type="text" name="quantity"></td></tr>
<tr><td>Price</td><td><input type="text" name="price"></td>                           <td>Making Charges</td><td><input type="text" name="makingcharges"></td></tr>
<tr><td>Market Price</td><td><input type="text" name="marketprice"></td>
<tr><td>Record</td><td><input type="text" name="record"></td>                     <td>Date</td><td><input type="text" name="date"></td></tr>
<tr><td>Tax</td><td><input type="text" name="tax"></td>                                <td>Vender</td><td><input type="text" name="vender"></td></tr>
<tr><td>Country</td><td><input type="text" name="country"></td>               <td colspan='2'><input type="submit" value="submit"></td></tr>
</table>
</form>
</center>
</body>
</html>



web.xml



<web-app>

<servlet>
<servlet-name>us</servlet-name>
<servlet-class>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>ControlServlet1</servlet-class>
</servlet>

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

</web-app>

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>




Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.