Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form with two entries or one entry 1

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US
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:<br>
<input type="Text" name="Name"><br>
First Name
<input type="Text" name="F_Name"><br>
<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(2) %>
   <%= resultset.getString(3) %>
   <%= resultset.getString(4) %>

   <br>
<%
}
%>


Please advise because it is not working.
 
Because the "Name" and "F_Name" parameters are in the form, they
should not be null, but could be of zero length (ie "")
so you should check for this, and make them null in order for you following
if statements to work properly

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 (name != null && name.length() == 0) {
	name = null;
}
if (name2 != null && name2.length() == 0) {
	name2 = null;
}

if((name != null) && (name2 != null))
{
    ResultSet resultset = statement.executeQuery(
    "select * from poc where F_Name = '" + name2 + "' and Name = '" + name + "'"); 
}
else if((name == null) && (name2 != null))
{
    ResultSet resultset = statement.executeQuery(
    "select * from poc where F_Name = '" + name2 + "'");
}
else if((name != null) && (name2 == null))
{
    ResultSet resultset = statement.executeQuery(
    "select * from poc where Name = '" + name + "'");
}
%>

<% while(resultset.next())
   { %>
   <%= resultset.getString(2) %>
   <%= resultset.getString(3) %>
   <%= resultset.getString(4) %>

   <br>
<%
}
%>

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks I tried as you suggested and got the following errors where it doesnt seem to like the resultset object:

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) );
                 ^

Please advise.
 
Move the resultset var into scope :

Code:
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>


<body>
<% Connection connection = DriverManager.getConnection("jdbc:odbc:myDatabaseName", "", "");
Statement statement = connection.createStatement();
ResultSet resultset = null;
String name = request.getParameter("Name");
String name2 = request.getParameter("F_Name");


if (name != null && name.length() == 0) {
    name = null;
}
if (name2 != null && name2.length() == 0) {
    name2 = null;
}

if((name != null) && (name2 != null))
{
    resultset = statement.executeQuery(
    "select * from poc where F_Name = '" + name2 + "' and Name = '" + name + "'"); 
}
else if((name == null) && (name2 != null))
{
    resultset = statement.executeQuery(
    "select * from poc where F_Name = '" + name2 + "'");
}
else if((name != null) && (name2 == null))
{
    resultset = statement.executeQuery(
    "select * from poc where Name = '" + name + "'");
}
%>

<% while(resultset.next())
   { %>
   <%= resultset.getString(2) %>
   <%= resultset.getString(3) %>
   <%= resultset.getString(4) %>

   <br>
<%
}
%>

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top