Results 1 to 5 of 5
  1. #1
    sanser is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default unable to validate

    Hi everyone,

    I am trying to write a simple jsp and Servlets program.I am trying to validate the columns but it is not working.Please can any one suggest me.If i am giving empty columns and hit the enter then it is not giving alert box and it is giving the error like "The server encountered an internal error (java.sql.SQLException: ORA-01400: cannot insert NULL into ("SCOTT"."QINFO"."FNAME") ) that prevented it from fulfilling this request."

    Bellow are my programs:
    my JSP file:

    <%@ page language="java" %>
    <HTML>
    <HEAD>
    <TITLE> User Personal Information </TITLE>
    <script language="Javascript">
    function Validate(objForm){
    if (objForm.Fname.value==0)
    {
    alert("please enter Fname");
    objForm.Fname.focus();
    return false;
    }
    if (objForm.value.Lname==0){
    alert("please enter Lname");
    objForm.Lname.focus();
    return else;
    }
    if (objForm.value.ssn==0)
    {
    alert("Please enter ssn");
    objForm.ssn.focus();
    return false;
    }
    if (objForm.GMontlyIncome=0)
    {
    alert("plase enter Gincome");
    objForm.GMonthlyIncome.focus();
    return false;
    }
    if (objForm.value.NMonthlyIncome==0)
    {
    alert("please enter NM income');
    objForm.NMonthlyIncome.focus();
    return false;
    }
    return true;
    }
    </script>


    </HEAD>

    <BODY>
    <BODY bgcolor="orange">WELCOME TO OUR SITE<br><br><br>
    Please Fill The Bellow Form:
    <form action="/validation/Qinfo" method="post" onSubmit="return Validate(this)">


    <table border="1" align="center"><br><br>
    First Name<input type="text" name="Fname" ></br></br>
    Last Name<input type="text" name="Lname" ></br></br>
    SSN
    <input type="text" name="ssn"></br></br>
    Gross Monthly income<input type="text" name="GMonthlyIncome"></br></br>
    Net Monthly Income<input type="text" name="NMonthlyIncome"></br></br>
    Indicator: <select name="Indicator">
    <option>1</option>
    <option>2</option>
    </select >
    <input type="submit" value="submit"></br></br>
    </form>
    </BODY>
    </HTML>

    My Servlet is:
    import java.io.*;
    import java.util.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Qinfo extends HttpServlet
    {

    private ServletConfig config;
    public void init(ServletConfig config)
    throws ServletException{
    this.config=config;
    }
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    Connection con=null;
    ResultSet rs;
    String Fname=new String();
    Fname=req.getParameter("Fname");
    String Lname=new String();
    Lname=req.getParameter("Lname");
    String ssn=new String();
    ssn=req.getParameter("ssn");
    String GMincome=new String();
    GMincome=req.getParameter("GMonthlyIncome");
    String NMincome=new String();
    NMincome=req.getParameter("NMonthlyIncome");
    String Indicator=new String();
    Indicator=req.getParameter("Indicator");
    out.println("<html><body bgcolor=orange>");

    out.println("<br>WELCOME:"+Fname);

    out.println("</body></html>");
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin: @****:***","***","***");
    PreparedStatement stmt=con.prepareStatement("insert into qinfo values(?,?,?,?,?,?)");
    stmt.setString(1,Fname);
    stmt.setString(2,Lname);
    stmt.setString(3,ssn);
    stmt.setString(4,GMincome);
    stmt.setString(5,NMincome);
    stmt.setString(6,Indicator);
    stmt.executeUpdate();
    con.close();
    out.println("<br><h4> Thank you for the information<br>");
    }
    catch(Exception e)
    {
    res.sendError(500,e.toString());
    }
    }
    }
    And i created my table columns with not null.Please help me.Thanks in advance....

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't test if the value is 0. Test if the value's length is 0.

    P.S Do you see that your connection is not closed since an exception was thrown? That's why you should always close them in a finally block.

  3. #3
    sanser is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default Thanks r035198x

    Thank you for the response.I changed my jsp now with value.length in if condition and i close the connection in java file,then also i am getting the error same.Please can you help me.
    Thank you.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Put alerts in your Javascript validations to see if the javascript is getting called at all.
    It's as simple as
    alert("Look Ma, the function has been called!");
    And you can go fancy and output the values of the form controls from those alerts as well to see what you are really testing against.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    u check like this for not having empty values

    if((objForm.Fname.value == null) ||(obj.Fname.value =="")||(obj.Fname.length() == 0 ))
    Ramya:cool:

Similar Threads

  1. validate between int and double
    By heartysnowy in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 07:15 PM
  2. How to validate date using SampleDateFormat
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-04-2008, 02:38 PM
  3. repaint validate doLayout
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-27-2007, 06:53 PM
  4. How to validate date using SampleDateFormat
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:31 PM
  5. validate hex character
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •