0
votes

This is my code to delete data from mysql database, and I'm getting this error.

<%@page import='java.sql.*'%>    
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
         <%! 
        public class student{
        String URL="jdbc:mysql://localhost:3306/project";
       String USERNAME="root";
       String PASSWORD="xxxx";
       Connection con=null;
       PreparedStatement select,deleteA=null;
       ResultSet resultSet=null;

       public student(){               
           try{
           con=DriverManager.getConnection(URL,USERNAME,PASSWORD);           
           select=con.prepareStatement("select id,first_name,last_name from student");
            deleteA=con.prepareStatement("delete from student where id = ? ");             
           }
           catch(SQLException e)
           {
               e.printStackTrace();               
           }                         
       }
       public ResultSet getstudent(){
       try{           
       resultSet=select.executeQuery();           
       }catch(SQLException e)
       {
           e.printStackTrace();
       }
       return resultSet;
       }  

       public int deleteA(Integer id)
        {
        int result=0;
        try{
       deleteA.setInt(1, id);
        result=deleteA.executeUpdate();            
    }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        return result;
        }
        }  
        %>
        <%
        int result=0;
        student std=new student();
        ResultSet stds=std.getstudent();
        Integer sid=new Integer(0);
        if(request.getParameter("submit")!=null){
        sid=Integer.parseInt(request.getParameter("std"));
        result=std.deleteA(sid);            
        }            
        %>
        <form name="myform" action="delete.jsp" method="POST">
            <table border="0">                    
               <tbody>
                    <tr>
                        <td>name:</td>
                        <td><select name="student">
                                <% while(stds.next()){%>
                                <option value="<%= stds.getInt("id")%>"> <%= stds.getString("first_name")%> <%= stds.getString("last_name")%></option>
                            <% } %>
                            </select></td>
                    </tr>
                </tbody>
            </table>

            <input type="submit" value="submit" name="submit" />                
        </form>
    </body>
</html>

after i select name and try to submit..it gives me this error

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: null root cause

java.lang.NumberFormatException: null note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.

1

1 Answers

0
votes

You are parsing the std parameter as an integer but probably it is null. You should check if this parameter is null before using it, and not only the submit parameter.