每当我的 JSP 打开 ShowPurchasingItems 时,我的 servlet 就会出现问题.jsp它不会转到下一个 JSP。
这是我的展示购买项目.jsphttp://jsfiddle.net/0g3erumm/
这是我的 Servlet,它不会打开我的下一个 JSP
package connection;
import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
@WebServlet("/CheckOutServlet")
public class CheckOutServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String User = (String) session.getAttribute("username");
String id = (String) session.getAttribute("stockIdToPurchase");
float price = (float) session.getAttribute("UnitPriceToPurchase");
int stock = (int) session.getAttribute("OnStockToPurchase");
int quantityOrdered = (int) session.getAttribute("purchaseQuantity");
float totalPrice = price * quantityOrdered;
int newStock = stock - quantityOrdered;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String url = "jdbc:mysql://localhost:3306/inventory";
String user = "root";
String password = "password";
String query = "INSERT INTO purchases (username,stockId,price,quantityOrdered,totalPrice) VALUES ('"+User+"', '"+id+"', "+price+", "+quantityOrdered+", "+totalPrice+");";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if(rs.next())
{
String encodedURL = response.encodeRedirectURL("ShowInventoryList.jsp");
response.sendRedirect(encodedURL);
}
}
catch(Exception e)
{
out.println("There is an error here");
}
finally
{
out.close();
try
{
rs.close();
stmt.close();
conn.close();
}
catch (Exception e)
{
out.println("There is no error here");
}
}
}
}
它会继续捕获此语句的错误 out.println("这里有一个错误"); 我被困在这里,我不知道我的程序还有什么问题,希望有人可以帮助我。
下异常,从而丢失了所有可以帮助你找到问题根源的信息,这是大罪!
您应该更改异常的处理方式,至少您应该转储堆栈跟踪:
catch(Exception e) {
out.println("There is an error here");
e.printStackTrace();
}
一旦你有了堆栈跟踪,在诊断问题(或提出更具体的问题)时,你的处境就会好得多!
编辑 - 根据评论中发布的异常:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery()
由于您正在使用 query
方法执行update
而引发。 您应该将代码更改为以下内容:
int updateCount = stmt.executeUpdate(query);
if(updateCount > 0) {
String encodedURL = response.encodeRedirectURL("ShowInventoryList.jsp");
response.sendRedirect(encodedURL);
}
executeQuery
执行给定的SQL语句,该语句返回单个ResultSet对象。
您正在制作 INSERT,它不会返回任何内容。我想这就是为什么你会得到一个例外。
我建议使用 PreparedStatement,您可以在其中绑定变量并防止 SQL 注入 + 一些数据库使用预准备语句更快地工作,并且executeUpdate
而不是executeQuery
PreparedStatement stmt = null;
...
String query = "INSERT INTO purchases (username,stockId,price,quantityOrdered,totalPrice) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query);
stmt.setString(1, username);
...
int inserted = stmt.executeUpdate();
if (inserted > 0) {
// there was a successfull insert
...
互联网上有很多例子。例如:http://www.mkyong.com/jdbc/how-to-insert-date-value-in-preparedstatement/