Problem med kode Missing Return statement
Her er min kode, den bliver ved med at sige at der mangler et return statement ved ikke hvad der er galt. Det er efter jeg indsatte try catch for at opfange sql exceptions, kan godt være jeg har lavet noget forkert der. Hele koden er indsatimport java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import shopping.Book;
import test.dbConnect;
import java.net.*;
import java.sql.*;
import java.util.ArrayList;
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
res.sendRedirect("http://localhost:8080/error.html");
}
Vector buylist = (Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
Book aBook = getBook(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector(); //first order
buylist.addElement(aBook);
} else { // not first buy
for (int i=0; i< buylist.size(); i++) {
Book book = (Book) buylist.elementAt(i);
if (book.getIsbn().equals(aBook.getIsbn())) {
book.setAntal(book.getAntal()+aBook.getAntal());
buylist.setElementAt(book,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(aBook);
}
}
session.putValue("shopping.shoppingcart", buylist);
String url="shopping/showBookSpec.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
float total =0;
for (int i=0; i< buylist.size();i++) {
Book anOrder = (Book) buylist.elementAt(i);
float pris= anOrder.getPris();
int antal = anOrder.getAntal();
total += (pris * antal);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf('.');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="/shopping/Checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private Book getBook(HttpServletRequest req) {
try {
dbConnect dbc = new dbConnect();
Connection con = dbc.getConnection();
Statement stmt = con.createStatement();
String myBook = req.getParameter("isbn");
String antal = req.getParameter("antal");
ResultSet rs = stmt.executeQuery("SELECT isbn, forfatter, titel, pris FROM boeger WHERE isbn = '" + myBook + "'");
Book book = new Book();
book.setForfatter(rs.getString(2));
book.setTitel(rs.getString(3));
book.setIsbn(rs.getString(1));
book.setPris((new Float(rs.getFloat(4))).floatValue());
book.setAntal((new Integer(antal)).intValue());
return book;
} catch (Exception e) {
}
}
}
