www.webdeveloper.com
Results 1 to 7 of 7

Thread: Form entry with one or two fields entered

  1. #1
    Join Date
    Nov 2002
    Posts
    333

    Form entry with one or two fields entered

    I have a form that works where someone puts in a last name AND first name to query an Access 2000 database. Now I want it to work where it takes either a last name or a first name and query the database. My form:
    Code:
    <form action="actionpage.jsp" method="post">
    Last name:
    <input type="Text" name="Name">
    First Name
    <input type="Text" name="F_Name">
    <input type="Submit" value="Submit">
    </form>
    Action page:
    Code:
    <%@ page import="java.sql.*" %>
    <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
    
    
    <body>
    <% Connection connection = DriverManager.getConnection("jdbc:odbc:myDatabaseName", "", "");
    Statement statement = connection.createStatement();
    String name = request.getParameter("Name");
    String name2 = request.getParameter("F_Name");
    
    if((request.getParameter("Name") != null) && (request.getParameter("F_name") != null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where F_Name = '" + name2 + "' and Name = '" + name + "'"); 
    }
    else if((request.getParameter("Name") == null) && (request.getParameter("F_name") != null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where F_Name = '" + name2 + "'");
    }
    else if((request.getParameter("Name") != null) && (request.getParameter("F_name") == null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where Name = '" + name + "'");
    }
    
    <% while(resultset.next())
       { %>
       <%= resultset.getString(1) %>
       <%= resultset.getString(2) %>
      Results
       <% } %>
    Please advise because it is not working.

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Please define "not working."

  3. #3
    Join Date
    Nov 2002
    Posts
    333
    After I put in a first name or a last name or even both a first name and a last name it gives me error messages:


    The below action page I used is called dbs8.jsp
    Code:
    Generated servlet error:
        [javac] Compiling 1 source file
    
    C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\colors\dbs8_jsp.java:72: cannot resolve symbol
    symbol  : variable resultset 
    location: class org.apache.jsp.dbs8_jsp
     while(resultset.next())
           ^
    
    
    
    An error occurred at line: 38 in the jsp file: /jsp/colors/dbs8.jsp
    
    Generated servlet error:
    C:\jakarta-tomcat-4.1.27\work\Standalone\localhost\examples\jsp\colors\dbs8_jsp.java:75: cannot resolve symbol
    symbol  : variable resultset 
    location: class org.apache.jsp.dbs8_jsp
          out.print( resultset.getString(1) );
                     ^
    It seems to error on the resultset object. Please advise.

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Thats because your trying to access the variable out of scope. It only exists within the block in which it was created. (in this case, the if block)

    Define the variable outside of trhe if statement and try again.

    Regards.

  5. #5
    Join Date
    Nov 2002
    Posts
    333
    Thanks! It works now.

    Is it okay to repeat like this or is there a shorter way to do what I am doing?

    Code:
    if((request.getParameter("Name") != null) && (request.getParameter("F_name") != null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where F_Name = '" + name2 + "' and Name = '" + name + "'"); 
         while(resultset.next())
         {
             <%= resultset.getString(1) %>
             <%= resultset.getString(2) %>
         } 
    }
    else if((request.getParameter("Name") == null) && (request.getParameter("F_name") != null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where F_Name = '" + name2 + "'");
         while(resultset.next())
         {
             <%= resultset.getString(1) %>
             <%= resultset.getString(2) %>
         } 
    }
    else if((request.getParameter("Name") != null) && (request.getParameter("F_name") == null))
    {
        ResultSet resultset = statement.executeQuery(
        "select * from poc where Name = '" + name + "'");
         while(resultset.next())
         {
             <%= resultset.getString(1) %>
             <%= resultset.getString(2) %>
         } 
    }
    %>

  6. #6
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Here it is (untested) refactored to eliminate the repetition.
    Code:
    boolean needAnd = false;
    StringBuffer query = new StringBuffer("select * from poc where ");
    
    if (request.getParameter("Name") != null)
    {
        query.append("Name = '" + name + "'");
        needAnd = true;
    }
    if (request.getParameter("F_name") != null)
    {
        if (needAnd) query.append(" and ");
        query.append("F_Name = '" + name2 + "'");
    }
    
    ResultSet resultset = statement.executeQuery(query.toString());
    while(resultset.next())
    {
     <%= resultset.getString(1) %>
     <%= resultset.getString(2) %>
    }

  7. #7
    Join Date
    Nov 2002
    Posts
    333
    Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles