我的servlet在从我的JSP打开时遇到问题,即ShowPurchasingItems.jsp,它不会转到下一个JSP。
这是我的ShowPurchasingItems.jsp http://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(“这里有错误”);而且我被困在这里我不知道我的计划还有什么问题希望有人可以帮助我。
答案 0 :(得分:2)
你通过吞下异常从而失去所有可能帮助你解决问题的信息而犯下了一个主要的罪行!
您应该更改异常的处理方式,至少应该转储堆栈跟踪:
catch(Exception e) {
out.println("There is an error here");
e.printStackTrace();
}
一旦掌握了堆栈跟踪,在诊断问题(或提出更具体的问题)时,您将处于更好的状态!
修改 - 根据评论中发布的例外:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery()
因为您使用update
方法执行query
而被抛出。您应该将代码更改为:
int updateCount = stmt.executeUpdate(query);
if(updateCount > 0) {
String encodedURL = response.encodeRedirectURL("ShowInventoryList.jsp");
response.sendRedirect(encodedURL);
}
答案 1 :(得分:1)
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/