使用Servlet从数据库中删除行

时间:2017-05-23 18:37:12

标签: java mysql database servlets

我的servlet中有一个关于从数据库中删除记录的问题。请查看我的servlet代码,请纠正我。

ViewBooksServlet.java servlet显示数据库中的记录以及每行中的“删除”超链接。但问题是,每当我尝试按下删除按钮.. elseif循环运行,它显示从DeleteBooksServlet.java servlet删除但数据不会从数据库中删除..请指导我如何从数据库中删除数据库中的记录或者来自book_name。

ViewBooksServlet.java

package books;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/viewbook")
public class ViewBooksServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException 
{
    PrintWriter out=response.getWriter();
    Connection con=null;
    try {
        Class.forName("com.mysql.jdbc.Driver"); 
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/books_online","root","");
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("Select * from books");
        out.println("<html><table width='50' border='1'>");
        out.println("<tr><td>Id</td><td>Name</td><td>Author</td><td>Price</td></tr>");

        while(rs.next())
        {
            out.println("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td><a href='deletebook'>Delete</a></td></tr>");
        }
        out.write("</table></html>");

    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

DeleteBooksServlet.java

package books;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/deletebook")
public class DeleteBooksServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{       
    PrintWriter out=response.getWriter();   
    Connection con=null;
    try {

        String id=request.getParameter("id");
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/books_online","root","root");
        PreparedStatement ps=con.prepareStatement("delete from books where id=?");
        ps.setString(1, id);
        int i=ps.executeUpdate();
        if(i!=0)
        {
            out.println("Deleting rows");
        }
        else if(i==0)
        {
            out.print("deleted");
        }
    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:1)

这可能是因为以下几行:

PreparedStatement ps=con.prepareStatement("delete from books where id=?");
ps.setString(1, id);

ViewBooksServlet的代码看,id似乎是int而不是String。我会使用setInt方法代替setString来删除id,例如:

PreparedStatement ps=con.prepareStatement("delete from books where id=?");
ps.setInt(1, Integer.parseInt(id));