|
Javaxue习者论坛
import java.io.*;
import javax.Servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Sales extends HttpServlet
{
Connection dbCon;
public void init() throws ServletException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon=DriverManager.getConnection("jdbcdbc:pfstest");
}
catch(ClassNotFoundException e)
{
System.out.println("Sales.init():");
System.out.println(e.toString());
}
catch(SQLException e)
{
System.out.println("Sales.init()");
System.out.println(e.toString());
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
String sales[][]=new String[5][3];
sales[0][0]="North";
sales[0][1]=req.getParameter("txtQtyN");
sales[0][2]=req.getParameter("txtRevN");
sales[1][0]="South";
sales[1][1]=req.getParameter("txtQtyS");
sales[1][2]=req.getParameter("txtRevS");
sales[2][0]="East";
sales[2][1]=req.getParameter("txtQtyE");
sales[2][2]=req.getParameter("txtRevE");
sales[3][0]="West";
sales[3][1]=req.getParameter("txtQtyW");
sales[3][2]=req.getParameter("txtRevW");
sales[4][0]="Center";
sales[4][1]=req.getParameter("txtQtyC");
sales[4][2]=req.getParameter("txtRevC");
System.out.println("2");
try
{
StringBuffer query=new StringBuffer();
query.append("insert into java_sales_t (region,date,qtysold,revenue)");
query.append("values(?,?,?,?)");
PreparedStatement stmt=dbCon.prepareStatement(query.toString());
for(int i=0;i<5;i++)
{
stmt.clearParameters();
stmt.setString(1,sales[0]);
stmt.setDate(2,new java.sql.Date(System.currentTimeMillis()));
stmt.setInt(3,Integer.parseInt(sales[1]));
stmt.setDouble(4,Double.parseDouble(sales[2]));
stmt.executeUpdate();
}
getServletContext().getRequestDispatcher("/Result82.jsp").forward(req,res);
}
catch (SQLException e)
{
res.sendError(res.SC_ACCEPTED,
"The request has been accepted,but an error occurred due to the database");
}
}
public void destroy()
{
try
{
dbCon.close();
}
catch(Exception e)
{
System.out.println("Salesatabase close failed");
System.out.println(e.toString());
}
}
}
这是我的代码,为什么该servlet不能處理請求?謝谢!
歡迎来到Java学习者论坛,转载请注明地址:http://www.javaxxz.com. |
|