sql 异常 不能为带有 SELECT 查询的 SELECT 发出 executeUpdate
Posted
技术标签:
【中文标题】sql 异常 不能为带有 SELECT 查询的 SELECT 发出 executeUpdate【英文标题】:sql exception Can not issue executeUpdate for SELECTs with SELECT query 【发布时间】:2017-11-12 07:50:46 【问题描述】:我是 Java 领域的初学者,我正在一家项目书店工作。我创建了一个购买链接,该链接被重定向到购买 servlet,并且在购买 servlet 中我有我的 sql 查询,通过它我可以获取所选书籍的书籍详细信息,但是 sql 抛出异常,无法为 SELECT 发出 executeUpdate。 我在这里先向您的帮助表示感谢 这是我的jsp代码,我有购买链接
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link href="templatemo_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
Connection con=null;
try
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql:/
/localhost:3306jsp","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from books where
book_type='Thriller Book'");
out.print("<table width='80%'>");
out.print("<tr><th>ID</th><th>Book Name</th><th>Book Author</th><th>Book
Price</th><th>Book Category</th><th></th></tr>");
while(rs.next())
String str1=rs.getString("id");
String str2=rs.getString("book_name");
String str3=rs.getString("book_author");
String str4=rs.getString("book_price");
String str5=rs.getString("book_type");
out.println("<tr><td align='center'>"+str1+"</td><td
align='center'>"+str2+"</td><td align='center'>"+str3+"</td><td
align='center'>"+str4+"</td><td align='center'>"+str5+"</td><td
align='center'><a href=\"./purchase?id="+str1+"\">Purchase</a>
</td></tr>");
out.print("</table>");
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
%>
</body>
</html>
这是我的购买 servlet,我有我的 sql 代码
package purchase;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/purchase")
public class Purchase extends HttpServlet
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
PrintWriter out=response.getWriter();
Connection con=null;
try
String str1=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306
/jsp","root","root");
PreparedStatement ps=con.prepareStatement("select
book_name,book_author,book_price from books where id=?");
ps.setString(1,str1);
int i=ps.executeUpdate();
if(i!=0)
out.println("Details");
else if(i==0)
out.println("<table>");
out.print("<tr><td>Book Name</td><td>Book Author</td><td>Book
Price</td><td>Purchase</td></tr>");
out.println("</table>");
catch (ClassNotFoundException | SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
【问题讨论】:
这个错误肯定会让您指出executeUpdate
并不是真正用于SELECT 语句的事实,请尝试使用executeQuery
【参考方案1】:
您正在拨打SELECT
,所以您应该拨打executeQuery()
,而不是executeUpdate()
:
String sql = "SELECT book_name, book_author, book_price FROM books WHERE id=?";
Statement ps = con.createStatement();
ps.setString(1, str1);
ResultSet rs = ps.executeQuery();
while (rs.next())
String bookName = rs.getString("book_name");
// etc.
【讨论】:
以上是关于sql 异常 不能为带有 SELECT 查询的 SELECT 发出 executeUpdate的主要内容,如果未能解决你的问题,请参考以下文章
带有 Select2 未捕获异常的 FormValidation:未为 Select2 定义查询函数