当前位置: 技术问答>java相关
谁能帮我看看下面这段程序错在何处,100分重谢
来源: 互联网 发布时间:2015-04-08
本文导语: //import ShoppingCart; import dbSql; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.lang.Float; import java.sql.*; public class AddToCart extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletRespo...
//import ShoppingCart;
import dbSql;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Float;
import java.sql.*;
public class AddToCart extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
{
try{
String id=request.getParameter("productid");
dbSql dbc = new dbSql();
String sql = "select * from product where productid="+id;
ResultSet products = dbc.executeQuery(sql);
if(products.next())
{
String desc=products.getString("disc");
float price=products.getFloat("price");
String name=products.getString("productname");
int quantity=1;
products.close();
dbc.closeStmt();
dbc.closeConn();
String[] item={id,name,desc,Float.toString(price),Integer.toString(quantity)};
HttpSession session=request.getSession(true);
Vector cart=(Vector)session.getAttribute("cart");
if(cart==null)
{
cart=new Vector();
}
cart.addElement(item);
session.setAttribute("cart",cart);
getServletConfig().getServletContext().getRequestDispatcher("/listshoppingcart.jsp").forward(request,response);
}
else{
products.close();
dbc.closeStmt();
dbc.closeConn();
getServletConfig().getServletContext().getRequestDispatcher("/login.jsp").forward(request,response);
}
}
catch(SQLException se) {
System.err.println("aq.executeQuery: " + se.getMessage());
}
}
}
编译时就出现下面的错误:
E:javaworkmyjspweb-infclasses>javac AddToCart.java
AddToCart.java:46: unreported exception javax.servlet.ServletException; must be
caught or declared to be thrown
getServletConfig().getServletContext().getRequestDispatcher("/listshop
ingcart.jsp").forward(request,response);
^
AddToCart.java:55: unreported exception javax.servlet.ServletException; must be
caught or declared to be thrown
getServletConfig().getServletContext().getRequestDispatcher("/login.jsp").
orward(request,response);
^
import dbSql;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Float;
import java.sql.*;
public class AddToCart extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
{
try{
String id=request.getParameter("productid");
dbSql dbc = new dbSql();
String sql = "select * from product where productid="+id;
ResultSet products = dbc.executeQuery(sql);
if(products.next())
{
String desc=products.getString("disc");
float price=products.getFloat("price");
String name=products.getString("productname");
int quantity=1;
products.close();
dbc.closeStmt();
dbc.closeConn();
String[] item={id,name,desc,Float.toString(price),Integer.toString(quantity)};
HttpSession session=request.getSession(true);
Vector cart=(Vector)session.getAttribute("cart");
if(cart==null)
{
cart=new Vector();
}
cart.addElement(item);
session.setAttribute("cart",cart);
getServletConfig().getServletContext().getRequestDispatcher("/listshoppingcart.jsp").forward(request,response);
}
else{
products.close();
dbc.closeStmt();
dbc.closeConn();
getServletConfig().getServletContext().getRequestDispatcher("/login.jsp").forward(request,response);
}
}
catch(SQLException se) {
System.err.println("aq.executeQuery: " + se.getMessage());
}
}
}
编译时就出现下面的错误:
E:javaworkmyjspweb-infclasses>javac AddToCart.java
AddToCart.java:46: unreported exception javax.servlet.ServletException; must be
caught or declared to be thrown
getServletConfig().getServletContext().getRequestDispatcher("/listshop
ingcart.jsp").forward(request,response);
^
AddToCart.java:55: unreported exception javax.servlet.ServletException; must be
caught or declared to be thrown
getServletConfig().getServletContext().getRequestDispatcher("/login.jsp").
orward(request,response);
^
|
只要将catch(SQLException se)改成catch(Exception se)就可以了!
至少编译可以通过,但你的程序的具体运行可以再自己调试一下!
至少编译可以通过,但你的程序的具体运行可以再自己调试一下!