我是java领域的初学者,我正在开发一个项目书店。我创建了一个购买链接,重定向到购买servlet,在购买servlet中,我有我的SQL查询,我通过它查询所选书籍的书籍详细信息,但是sql抛出异常不能为SELECTs发出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();
}
}
}
答案 0 :(得分: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.
}