Noobie need help

L

laik

If anybody can help me with this code. TIA
+++++++ Errors +++++++++++++
CheckoutServlet.java:107: exception java.sql.SQLException is
never thrown in bod
y of corresponding try statement
catch( SQLException se )
^
CheckoutServlet.java:111: exception
java.io.FileNotFoundException has already be
en caught
catch( FileNotFoundException fnfe )
^
CheckoutServlet.java:115: exception java.io.IOException has
already been caught
catch( IOException ie )
^
CheckoutServlet.java:37: unreported exception
java.sql.SQLException; must be cau
ght or declared to be thrown
dbutil.connect( props, "scott", "tiger");
^
CheckoutServlet.java:39: unreported exception
java.sql.SQLException; must be cau
ght or declared to be thrown
dbutil.setPreparedStatement("itemUp", sql);
^
CheckoutServlet.java:41: unreported exception
java.sql.SQLException; must be cau
ght or declared to be thrown
pstmt.setInt( 1, numItems);
^
CheckoutServlet.java:42: unreported exception
java.sql.SQLException; must be cau
ght or declared to be thrown
pstmt.setString( 2, isbn);
^
7 errors

+++++ and code +++++++

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;

public class CheckoutServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

HttpSession session = request.getSession();
StringBuffer html = new StringBuffer( 500 );

ShoppingCart cart;
cart = new ShoppingCart();
session.setAttribute("ShoppingCart", cart);
String isbn = request.getParameter("isbn");
String numItemsString = request.getParameter("numItems");
int numItems;
numItems = Integer.parseInt(numItemsString);
//int numItems = request.getParameter("numItems");
Connection conn = null;
PrintWriter out = null;
PreparedStatement pstmt = null;
String propfile = getServletConfig().getInitParameter(
"oracle" );
log( propfile );
try
{
out = response.getWriter();
Properties props = new Properties();
FileInputStream fis = new FileInputStream( propfile );
props.load( fis );
DbUtil dbutil = new DbUtil();
dbutil.connect( props, "scott", "tiger");
String sql = "UPDATE products set quantityonhand = ? where
isbn = ?" ;
dbutil.setPreparedStatement("itemUp", sql);
pstmt = dbutil.getPreparedStatement("itemUp");
pstmt.setInt( 1, numItems);
pstmt.setString( 2, isbn);

//shipping values from the request
String name = request.getParameter("name");
String address = request.getParameter("address");
String city = request.getParameter("city");
String state = request.getParameter("state");
String postalCode = request.getParameter("postalCode");
String country = request.getParameter("country");
String email = request.getParameter("email");

String nameOnCard = request.getParameter("nameOnCard");
String creditCardType = request.getParameter
("creditCardType");
String creditCardNumber = request.getParameter
("creditCardNumber");
String creditCardExpiration = request.getParameter
("creditCardExpiration");

Shipping shipping = new Shipping(name, address, city, state,
postalCode, country, email);
// Billing values from the request

Billing billing = new Billing(nameOnCard, creditCardType,
creditCardNumber, creditCardExpiration);

//HttpSession session = request.getSession();
// Get the cart

html.append("<HTML>");
html.append("<HEAD><title>CheckOut Servlet</title>");
html.append("<link rel='stylesheet' type='text/css'
HREF='style.css'>");
html.append("<script language='JavaScript'
src='booknook2.js'>");
html.append("</script>");
html.append("</HTML>");
html.append("<BODY>");
html.append("<FORM action = '/booknook2/TestServlet'><input
type='submit' value='Test Servlet'>" );
html.append("<html><body BGCOLOR='#FDF5E6'><h1>Summary of
Your order</h1>");
html.append("<h2>Your personal information</h2>");
html.append("<h3>" + name );
html.append("<br>" + address );
html.append("<br>" + city );
html.append(" " + state );
html.append(" , " + postalCode );
html.append("<br>" + country );
html.append("<br>" + email );
html.append("<pre>");
html.append("</pre>");
html.append("</body>");
html.append("</html>");
out.println( html.toString() );
return;
}
catch( ClassNotFoundException c )
{
log( c.getMessage() );
ServletContext context = getServletContext();
RequestDispatcher rd = context.getRequestDispatcher
("/ErrorServlet" );
try
{
rd.forward( request, response );
}
catch( ServletException se )
{
log( se.getMessage() );
}
catch( IOException ie )
{
log( ie.getMessage() );
}
catch( SQLException se )
{
log( "sql exception" );
}
catch( FileNotFoundException fnfe )
{
log( "file not found" );
}
catch( IOException ie )
{
log( "io exception" );
log( ie.getMessage() );
}
}
}
}
 
R

Richard Wheeldon

laik said:
If anybody can help me with this code. TIA

It looks like a nesting issue. You're catching ClassNotFoundExceptions
then executing a small bit of code ( log(c.getMessage()) ... )
and catching a load of exceptions there. The catch blocks should
probably relate to the main body of the code.

Also you appear to be building a preparedstatement which is never
executed.
+++++++ Errors +++++++++++++
java.io.FileNotFoundException has already been caught

That's because you've already caught IOException.
CheckoutServlet.java:115: exception java.io.IOException has already been caught

This is obvious.
dbutil.connect( props, "scott", "tiger");

This is nesting. See above.
CheckoutServlet.java:39: unreported exception java.sql.SQLException; must be...
CheckoutServlet.java:41: unreported exception java.sql.SQLException; ...
CheckoutServlet.java:42: unreported exception java.sql.SQLException; ...
Ditto.

dbutil.setPreparedStatement("itemUp", sql);
pstmt = dbutil.getPreparedStatement("itemUp");
pstmt.setInt( 1, numItems);
pstmt.setString( 2, isbn);

Where is this executed ?
html.append("</script>");
html.append("</HTML>");
html.append("<BODY>");

{
log( c.getMessage() );
ServletContext context = getServletContext();
RequestDispatcher rd = context.getRequestDispatcher
("/ErrorServlet" );
try
{
rd.forward( request, response );
}
catch( ServletException se )
{
log( se.getMessage() );
}
catch( IOException ie )
{
log( ie.getMessage() );
}
catch( SQLException se )
{
log( "sql exception" );
}
catch( FileNotFoundException fnfe )
{
log( "file not found" );
}
catch( IOException ie )
{
log( "io exception" );
log( ie.getMessage() );
}
}

These exceptions are not thrown in the code left here. See above,

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top