Help With Debugging

[nobr]im doing a shopping Cart with an MS ACCESS DB...

Im trying to get it to input the details of the order the code is as follows:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.text.SimpleDateFormat,java.sql.*,java.util.*,basket.ShoppingBasket, basket.Product" errorPage="" %>

<html>

<head>

<title>West Wrights Oatcakes - Process Order</title>

</head>

<body>

<jsp:useBean id="basket" class="basket.ShoppingBasket" scope="session"/>

<%

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/ROOT/Assignment/Db1.mdb;");

Statement statement = conn.createStatement();

Enumeration parameters = request.getParameterNames();

SimpleDateFormat dbtimedatestampformat =new SimpleDateFormat("MM/dd/yyyy");

java.util.Date date =new java.util.Date();

String dbtimedatestamp = (dbtimedatestampformat.format(date));

String emailParam = request.getParameter("email");

String postParam = request.getParameter("radiobutton");

Double total = basket.getTotal();

String query ="INSERT INTO TblTransaction VALUES (#" + dbtimedatestamp +"#,'',?,'"+emailParam+"')";

java.sql.PreparedStatement stment = conn.prepareStatement(query);

if(parameters.hasMoreElements())

{

statement.executeUpdate("INSERT INTO TblOrder (\"Email\", \"Date\", \"Total\", \"Post\") VALUES ('"+emailParam+"',#" + dbtimedatestamp +"#,'"+total+"','"+postParam+"')");

out.print("mooooo");

Enumeration products = basket.getProducts();

Product product = (Product)products.nextElement();

while(products.hasMoreElements())

{

//Product product = (Product)products.nextElement();

stment = conn.prepareStatement(query);

stment.setString(2,product.getId());

out.print("dad");

stment.setInt(3,product.getQuantity());

out.print("mum");

stment.executeUpdate();

}

}

statement.close();

stment.close();

conn.close();

basket.emptyBasket();

}

catch (Exception e){out.print(e);

}

%>

Thank youfor your order.

The order will be processed within the next 10 minutes <br>

Please note during busy periods orders may take longer to process<br>

<br>

<a href="<%=response.encodeURL("shop-products.jsp")%> ">Return to shop</a>

</body>

</html>

The error i am getting is this:

java.lang.ArrayIndexOutOfBoundsException: 1

can anyone help?

Many thanks in advance[/nobr]

[3993 byte] By [Jan3ya] at [2007-10-2 14:42:20]
# 1

Can you show the full stack trace? I can't see any use of arrays on your page. The best guess would be:

Enumeration products = basket.getProducts();

Product product = (Product)products.nextElement();

You are not checking that there ARE any products before you get the nextElement.

Suggestions

- This sort of code belongs in a servlet rather than a JSP.

- Use prepared statements for all parameters - you are leaving yourself open to SQL Injection.

- Always specify all the columns and the order when inserting records. It saves confusion later.

- I'm not sure about your use of quotes around the column names in the SQL statement. They shouldn't be necessary

String sql = "INSERT INTO TblOrder (Email, Date, Total, Post) VALUES (?,?,?,?,?)";

PreparedStatement stmt2 = conn.prepareStatement(sql);

stmt2.setString(1, emailParam);

stmt2.setDate(2, new java.sql.Date()); // current date/time

stmt2.setDouble(3, basket.getTotal());

stmt2.setString(4, postParam);

stmt2.execute();

evnafetsa at 2007-7-13 13:12:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I'm not sure if this is what is causing your ArrayIndexOutOfBoundsException but your query:

String query = "INSERT INTO TblTransaction VALUES (#" + dbtimedatestamp + "#,'',?,'"+emailParam+"')";

only has one ? but you are trying to set 3 parameters:

stment = conn.prepareStatement(query);

stment.setString(2,product.getId());

out.print("dad");

stment.setInt(3,product.getQuantity());

out.print("mum");

Actually you are trying to set 2 parameters but they are parameters 2 and 3. What line are you getting the exception on?

Gita_Weinera at 2007-7-13 13:12:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...