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");
                }

              }

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.