-
PLS CHECK THE CODE
DEAR BADRI,
in the pgm i want to trap the individual cost of each product from the database
and then put in a session...until the rs.next() loop iterates and then finally
add the costs of the products into totalcost and then displsy it on to the
screen..that is the functinality to be achieved. i am getting the individual
costs diplayed on the screen, but i am not able to add them into totalcost
variable and display them...if u understood the problem pls tell me the solution
satish
PLS CHECK THE CODE BELOW AND LET ME KNOW WHERE I AM GOING WRONG
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cart extends HttpServlet
{
Connection con = null;
PreparedStatement pst = null;
ResultSet rs =
null;
public void init(ServletConfig sc) throws ServletException
{
try
{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con = DriverManager.getConnection("jdbc:db2:sample",
"satish", "london");
super.init(sc);
}
catch(Exception e) {e.getMessage() ; }
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession(true);
Object[] items = (Object []) session.getValue("cart.items");
out.println("<html> ");
out.println("<body bgcolor=899699>");
out.println("<H3>you currently have the following
items in your cart:</H3><br>");
out.println("<form> ");
out.println("<table border = 2 cellpadding=2 cellspacing=3
>");
if(items!=null)
{
for (int i = 0 ; i < items.length
; i++ )
{
try
{
String[] cost =
new String[3];
pst = con.prepareStatement("select
* from ITEMTAB where itmname = ? " );
String str1 =
(String) items[ i ] ;
pst.setString(1,str1)
;
rs = pst.executeQuery(
);
while(rs.next())
{
out.println("<tr>
");
out.println("<td> ");
out.println( "The Price of " + " " + items[i] + " is
" + " " + rs.getString("price"));
out.println("</td> ");
out.println("</tr>
");
out.println("</table>
");
out.println("<br>");
cost[ i ] =
rs.getString("price") ;
session.putValue("total.cost",
cost );
}
}
catch(Exception e) { e.getMessage()
; }
}
out.println("<table border
= 1 cellpadding = 2 cellspacing = 3>");
out.println("<tr>
");
out.println("<td>
");
Object[]
totcost = (Object [] ) session.getValue("total.cost") ;
out.println("The Total Cost of all the Products in the Cart is : " +
totcost );
out.println("</td>
");
out.println("</tr>
");
out.println("</table>
");
out.println("</form>
");
}
else
{
out.println("There are no items in the session
value for cart.items" );
}
out.println("</body>");
out.println("</html> ");
}
}
-
Re: PLS CHECK THE CODE
Hi,
I hope two things you should do for this.
1) It is better to read from the database once using RS.getXXX();
Please in the loop, get the price() ( rs.getString("price"))) and assigned
it to a variable ( say curprice).
2) After showing to the page first get the session value session.getValue("total.cost")
and assigned to a variable ( say totprice), and then put the value with (
totprice + curprice ) with float value.
I hope it will work
Nikhil
"SATISH" <SATISH141@ENGLAND.COM> wrote:
>
>
>
>DEAR BADRI,
>
>
>in the pgm i want to trap the individual cost of each product from the database
>and then put in a session...until the rs.next() loop iterates and then finally
>add the costs of the products into totalcost and then displsy it on to
the
>screen..that is the functinality to be achieved. i am getting the individual
>costs diplayed on the screen, but i am not able to add them into totalcost
>variable and display them...if u understood the problem pls tell me the
solution
>
>satish
>
>PLS CHECK THE CODE BELOW AND LET ME KNOW WHERE I AM GOING WRONG
>
>
>
>import java.io.*;
> import java.sql.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
>
>
>
> public class cart extends HttpServlet
> {
>
>
>
> Connection con = null;
> PreparedStatement pst = null;
> ResultSet rs =
> null;
>
>
>
> public void init(ServletConfig sc) throws ServletException
>
> {
>
> try
> {
>
> Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
>
> con = DriverManager.getConnection("jdbc:db2:sample",
>"satish", "london");
>
> super.init(sc);
>
> }
>
> catch(Exception e) {e.getMessage() ; }
>
> }
>
>
>
>
>
>
>
> public void doPost(HttpServletRequest req, HttpServletResponse res)
>throws ServletException, IOException
> {
>
>
>
>
> res.setContentType("text/html");
> PrintWriter out = res.getWriter();
>
> HttpSession session = req.getSession(true);
>
> Object[] items = (Object []) session.getValue("cart.items");
>
>
> out.println("<html> ");
> out.println("<body bgcolor=899699>");
> out.println("<H3>you currently have the following
>items in your cart:</H3><br>");
> out.println("<form> ");
>
> out.println("<table border = 2 cellpadding=2 cellspacing=3
>>");
>
> if(items!=null)
> {
>
> for (int i = 0 ; i < items.length
>; i++ )
> {
>
> try
> {
>
> String[] cost
=
>new String[3];
>
>
> pst = con.prepareStatement("select
> * from ITEMTAB where itmname = ? " );
>
> String str1 =
> (String) items[ i ] ;
>
> pst.setString(1,str1)
>;
> rs = pst.executeQuery(
>);
>
>
>
>
> while(rs.next())
> {
> out.println("<tr>
>");
>
>out.println("<td> ");
>
> out.println( "The Price of " + " " + items[i] + " is
>" + " " + rs.getString("price"));
>
>out.println("</td> ");
> out.println("</tr>
>");
>
>
> out.println("</table>
>");
>
> out.println("<br>");
>
>
>
>
>
> cost[ i ] =
> rs.getString("price") ;
>
>
>
> session.putValue("total.cost",
> cost );
>
>
>
> }
>
>
>
>
>
>
>
>
> }
>
> catch(Exception e) { e.getMessage()
>; }
>
> }
>
>
>
> out.println("<table
border
>= 1 cellpadding = 2 cellspacing = 3>");
> out.println("<tr>
>");
> out.println("<td>
>");
>
> Object[]
> totcost = (Object [] ) session.getValue("total.cost")
;
>
>
> out.println("The Total Cost of all the Products in the Cart is : " +
>totcost );
>
> out.println("</td>
>");
> out.println("</tr>
>");
> out.println("</table>
>");
>
>
>
>
>
>
> out.println("</form>
>");
>
>
>
> }
>
>
>
> else
> {
> out.println("There are no items in the session
>value for cart.items" );
>
> }
>
>
> out.println("</body>");
>
> out.println("</html> ");
>
> }
>
>
> }
>
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks