0

Hi Friends I have just started learning Java.I am getting a problem may be a small one but i coundn't able to figure it out.so here is my Servlet code iam trying to insert data into database after submitting form iam getting java.lang.NumberFormatException: For input string: ""
I want to know how to check user entered String instead of Int ??

String Name=request.getParameter("name");
String Password=request.getParameter("pass");
int Mobile= Integer.parseInt(request.getParameter("mobile"));


        try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("Jdbc:Odbc:trans","sa","disil");
        PreparedStatement ps=con.prepareStatement("insert into truck values(?,?,?)");
        ps.setString(1,Name);
        ps.setString(2,Password);
        ps.setInt(3,Mobile);
                               int i=ps.executeUpdate();

                if(Name.equals(""))
                {
                request.setAttribute("ErrorN", "Cannot Leave Empty");
                }
if(Password.equals(""))
{
 request.setAttribute("ErrorP", "Cannot Leave Empty");
}
     if(Mobile==0) 
{
request.setAttribute("ErrorM", "Cannot Leave Empty");
}          


        if(i==1)
                {
        RequestDispatcher d=request.getRequestDispatcher("manager.jsp");
                d.forward(request, response);
                }
                else 
                {
                  request.setAttribute( "truckerr","Please Try Again");
                }

              }
2
Contributors
2
Replies
19
Views
4 Years
Discussion Span
Last Post by zebusman
0

you have to make sure proper data is added.A way to do it is validation.If you are using html5 then there is pattern and required attribute.Pattern will make sure data entered is of particular format and required make sure data is entered

<input type="tel" pattern="\d\d\d\d-\d\d\d\d" required>

If you are not using javascript,then in that case you can use javascipt validations.

0

My above code not checking error why ?? After setting attribute here i have get it in my jsp page using EL ${ErrorM}

One more Question iam trying to insert USER birthday into database using dropdown like facebook (Day/Month/Year), but iam not getting date into one coloum ?? How to bind them into one ??

Thank You for you reply

Edited by zebusman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.