无法删除记录

时间:2016-03-06 14:21:23

标签: java mysql jsp

<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Level"%>
<%@page language="java" import="java.util.*"%>
<%@page language="java" import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
     //String id4 = request.getParameter("id3");
     //int id5=Integer.ParseInt("id4");
    String id4 = request.getParameter("id3");

    if (id4 != null) {
        // int  id4 = Integer.parseInt(request.getParameter("id3.trim()"));
        Connection con = null;
        int id5 = Integer.parseInt("id4");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/login?       user=root&password=root");
            Statement stmt = con.createStatement();
            String sql = "delete from login2 where id=" + id5;
            int i = stmt.executeUpdate(sql);
            if (i > 0) {
                response.sendRedirect("index.jsp");
            }
            out.println("failed");
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>

我无法删除记录... id4正在取值null.how来解决它..

String id = request.getParameter("id");
int id1 = Integer.parseInt(id);
final String email = request.getParameter("email");
final String password = request.getParameter("pass");
Connection con = null;

try {
    Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://localhost/login? user=root&password=root");

    Statement stmt = con.createStatement();
    HttpSession session = request.getSession();
    String idd = (String) session.getAttribute("id1");
    String sql = "INSERT INTO login2(email,pass)" + " VALUES('" + email + "','" + password + "')";
    stmt.execute(sql);
    PrintWriter out = response.getWriter();

    out.println("successfully registered");
    ResultSet rs = stmt.executeQuery("select * from login2");
    response.setContentType("text/html");
    out.println("<html><body>");
    out.println("<table border=1 width=50% height=50%>");
    out.println("<tr><th>id</th><th>email</th><th>pasword</th>   <th>action</th><tr>");

    while (rs.next()) {
        int id2 = rs.getInt("id");
        String id9 = request.getParameter("id");
        int id0 = Integer.parseInt(id9);
        String email1 = rs.getString("email");
        String pass1 = rs.getString("pass");
        //int s = rs.getInt("sal"); 
        out.println("<tr>  <td>" + id2 + "</td> <td>" + email1 + "</td><td>" + pass1 + "</td>   </tr>");
        out.println("<tr><td><a href=  delete.jsp? id3=<%=rs.getInt(\"id\")%   >delete</a></td></tr>");
        //out.println("<tr><td><a HREF=\"delete.jsp\">link</a></td></tr>");
    }

    out.println("</table>");
    out.println("</html></body>");
}

这是我用于显示记录的servlet页面。

1 个答案:

答案 0 :(得分:0)

您的删除记录jsp必须抛出NumberFormatException,因为您正在尝试将无效字符串转换为数字

int id5 = Integer.parseInt("id4");在id4周围删除",然后id4字符串将转换为int。

如果您在表达式null中获得id4String id4 = request.getParameter("id3");

更新:

试试这个:out.println("<tr><td><a href= \"delete.jsp?id3=\""+ rs.getInt("id") +"\">delete</a></td></tr>");

取代out.println("<tr><td><a href= delete.jsp? id3=<%=rs.getInt(\"id\")% >delete</a></td></tr>");