neks
asked on
SQL Query the "AND" Statement - urgent
Hello :)
I have been struggling for the past 3 days to get the "and" statement to work, There is no errors in the actual program, but it is not returning the record/records. And I have made sure that the fields i am calling are present in the Access database. Where am I going wrong? Do I need an IF Statement. when i use the OR statement it works, but i want the "hobbyname", "sex" and "coursename" values to met entirely. Please, please can you help :(
<%
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.Statement statement = connection.createStatement ();
java.sql.ResultSet RS = statement.executeQuery
("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE HobbyName=? AND Sex=? AND CourseName=?");
%>
I have been struggling for the past 3 days to get the "and" statement to work, There is no errors in the actual program, but it is not returning the record/records. And I have made sure that the fields i am calling are present in the Access database. Where am I going wrong? Do I need an IF Statement. when i use the OR statement it works, but i want the "hobbyname", "sex" and "coursename" values to met entirely. Please, please can you help :(
<%
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.Statement statement = connection.createStatement
java.sql.ResultSet RS = statement.executeQuery
("SELECT FirstName,LastName,HobbyNa
%>
ASKER
Thanx for replying,
I have actually tried both uppercase and lowercase, and it is coming up with 3 errors saying......."
"HobbyNameValue" is either a misplaced package name or a non-existent entity. etc.
It might be me, maybe im coding it in the wrong place. I placed it like this...
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.Statement statement = connection.createStatement ();
statement.setString(1,Hobb yNameValue .toUpperCa se());
statement.setString(2,SexV alue.toUpp erCase());
statement.setString(3,Cour seNameValu e.toUpperC ase());
java.sql.ResultSet RS = statement.executeQuery
("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
I have actually tried both uppercase and lowercase, and it is coming up with 3 errors saying......."
"HobbyNameValue" is either a misplaced package name or a non-existent entity. etc.
It might be me, maybe im coding it in the wrong place. I placed it like this...
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.Statement statement = connection.createStatement
statement.setString(1,Hobb
statement.setString(2,SexV
statement.setString(3,Cour
java.sql.ResultSet RS = statement.executeQuery
("SELECT FirstName,LastName,HobbyNa
You are not using a prepared statement (in fact I'm not sure how the above code even compiles). Try this:
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
statement.setString(1,Hobb yNameValue .toUpperCa se());
statement.setString(2,SexV alue.toUpp erCase());
statement.setString(3,Cour seNameValu e.toUpperC ase());
java.sql.ResultSet RS = statement.execute();
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
statement.setString(1,Hobb
statement.setString(2,SexV
statement.setString(3,Cour
java.sql.ResultSet RS = statement.execute();
ASKER
I pasted the wrong code previously, I did use the Prepared Statement, everything seems right in the sql query, but an error comes up now saying.......
Syntax error (missing operator) in query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
This is how my code stands at the moment;
<%
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE Upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
statement.setString(1,"Hob byNameValu e".toUpper Case());
statement.setString(2,"Sex Value".toU pperCase() );
statement.setString(3,"Cou rseNameVal ue".toUppe rCase());
java.sql.ResultSet RS = statement.executeQuery();
so, again where is the problem? Searchinng a database is supposed to be one of the easiest things to do, but this doesnt seem to be the case:(
P.S The last line thats executes it needs to contain the word query or that comes up with an error. Please help!!!
Syntax error (missing operator) in query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
This is how my code stands at the moment;
<%
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
statement.setString(1,"Hob
statement.setString(2,"Sex
statement.setString(3,"Cou
java.sql.ResultSet RS = statement.executeQuery();
so, again where is the problem? Searchinng a database is supposed to be one of the easiest things to do, but this doesnt seem to be the case:(
P.S The last line thats executes it needs to contain the word query or that comes up with an error. Please help!!!
should be something like this:
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE HobbyName=? AND Sex=? AND CourseName=?");
statement.setString(1,hobb y);
statement.setString(2,sex) ;
statement.setString(3,cour se);
java.sql.ResultSet RS = statement.executeQuery();
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
statement.setString(1,hobb
statement.setString(2,sex)
statement.setString(3,cour
java.sql.ResultSet RS = statement.executeQuery();
Little modification , since you are comparing with upper case values.
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb y.toUpperC ase());
statement.setString(2,sex. UpperCase( ));
statement.setString(3,cour se.toUpper Case());
java.sql.ResultSet RS = statement.executeQuery();
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb
statement.setString(2,sex.
statement.setString(3,cour
java.sql.ResultSet RS = statement.executeQuery();
Little modification , since you are comparing with upper case values.
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb y.toUpperC ase());
statement.setString(2,sex. toUpperCas e());
statement.setString(3,cour se.toUpper Case());
java.sql.ResultSet RS = statement.executeQuery();
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb
statement.setString(2,sex.
statement.setString(3,cour
java.sql.ResultSet RS = statement.executeQuery();
have you run the query in Access to confirm that some records actually match your query?
ASKER
I pasted the wrong code previously, I did use the Prepared Statement, everything seems right in the sql query, but an error comes up now saying.......
Syntax error (missing operator) in query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
This is how my code stands at the moment;
<%
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE Upper HobbyName=? AND upper Sex=? AND upper CourseName=?");
statement.setString(1,"Hob byNameValu e".toUpper Case());
statement.setString(2,"Sex Value".toU pperCase() );
statement.setString(3,"Cou rseNameVal ue".toUppe rCase());
java.sql.ResultSet RS = statement.executeQuery();
so, again where is the problem? Searchinng a database is supposed to be one of the easiest things to do, but this doesnt seem to be the case:(
P.S The last line thats executes it needs to contain the word query or that comes up with an error. Please help!!!
Syntax error (missing operator) in query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
This is how my code stands at the moment;
<%
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
statement.setString(1,"Hob
statement.setString(2,"Sex
statement.setString(3,"Cou
java.sql.ResultSet RS = statement.executeQuery();
so, again where is the problem? Searchinng a database is supposed to be one of the easiest things to do, but this doesnt seem to be the case:(
P.S The last line thats executes it needs to contain the word query or that comes up with an error. Please help!!!
Access does not distinguish between upper and lower case .
So remove Upper from the code.
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE HobbyName=? AND Sex=? AND CourseName=?");
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb y);
statement.setString(2,sex) ;
statement.setString(3,cour se);
java.sql.ResultSet RS = statement.executeQuery();
So remove Upper from the code.
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
// here hobby,sex,coures are Java String variables.
statement.setString(1,hobb
statement.setString(2,sex)
statement.setString(3,cour
java.sql.ResultSet RS = statement.executeQuery();
> So remove Upper from the code.
Didn't you just tell neks to add it :)
That code is also identical to what I already posted.
Didn't you just tell neks to add it :)
That code is also identical to what I already posted.
I just came to know that Access does not distinguish between upper and lower case ,when neks posted Syntax error (missing operator) in query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
Yes, i did to help him. I could have also said look to for the posting of objets. My mistake Mr. objects.
Yes, i did to help him. I could have also said look to for the posting of objets. My mistake Mr. objects.
> when neks posted Syntax error (missing operator) in
> query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
He actually posted that before your comment, but u may have missed it :)
Comment from neks 03/15/2003 04:56PM PST
> query expression 'Upper HobbyName=? AND upper Sex=? AND upper CourseName=?'.
He actually posted that before your comment, but u may have missed it :)
Comment from neks 03/15/2003 04:56PM PST
Yes, you are right, I missed "Comment from neks 03/15/2003 04:56PM PST".
ASKER
yes I have checked numerous amounts of time that the 3 searches that i use are being satisfied in the Access database. None of the suggessted codes work
Firstly; none of the codes posted use( request.getParameter), so I am baffled has in to how the program is supposed to bring up the fields requested. It does not recognise
1)statement.setString(1,ho bby);
neither does it recognise.... this method
statement.setString(1,hobb y.toUpperC ase());
(they both come with errors specifying that the package/method is unknown)
it however recognises the way i implement it and runs...
statement.setString(1,requ est.getPar ameter("Ho bbyName")) ;
however the specfied record does not show up, as i said when I use the OR statement it works... how weird!!do you suggest that I use an IF statement?!?
Firstly; none of the codes posted use( request.getParameter), so I am baffled has in to how the program is supposed to bring up the fields requested. It does not recognise
1)statement.setString(1,ho
neither does it recognise.... this method
statement.setString(1,hobb
(they both come with errors specifying that the package/method is unknown)
it however recognises the way i implement it and runs...
statement.setString(1,requ
however the specfied record does not show up, as i said when I use the OR statement it works... how weird!!do you suggest that I use an IF statement?!?
ASKER
dear objects and allahabad,
I am actually female ;)
I am actually female ;)
ASKER
dear objects and allahabad,
I am actually female ;)
I am actually female ;)
ASKER
dear objects and allahabad,
I am actually female ;)
I am actually female ;)
> I am actually female ;)
My apologies, i generally try not to be gender specific but occasionally I slip up. Again please accept my apologies.
> none of the codes posted use( request.getParameter),
Well u never stated where the query parameters were coming from so we could only post generic code :)
> 1)statement.setString(1,ho bby);
Here hobby is assumed to be a string variable containing the value of your request parameter.
So yes you should replace it with request.getParameter("Hobb yName"). Same for the other query parameters.
> however the specfied record does not show up
Check the values of your request parameter.
Also what are the types of the columns in the database?
My apologies, i generally try not to be gender specific but occasionally I slip up. Again please accept my apologies.
> none of the codes posted use( request.getParameter),
Well u never stated where the query parameters were coming from so we could only post generic code :)
> 1)statement.setString(1,ho
Here hobby is assumed to be a string variable containing the value of your request parameter.
So yes you should replace it with request.getParameter("Hobb
> however the specfied record does not show up
Check the values of your request parameter.
Also what are the types of the columns in the database?
ASKER
sorry for repeating myself 3 times, I am a new user and not very familiar with the way this site works.
i shall check for any new code posted tomorrow, as it is 3am here in London.
Thank you..
neks
i shall check for any new code posted tomorrow, as it is 3am here in London.
Thank you..
neks
ASKER
In the Access database the 3 fields are all declared as text, thus I used "String", could it be the seetings in my design view in access? Bcos where it says "required data entry in the field", they are all set to "no", but I dont think that is the problem.
No, that shouldn't create a problem.
Perhaps try trimming the parameters:
System.out.println(request .getParame ter("Hobby Name"));
System.out.println(request .getParame ter("Sex") );
System.out.println(request .getParame ter("Cours eName"));
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
java.sql.PreparedStatement statement = connection.prepareStatemen t ("SELECT FirstName,LastName,HobbyNa me,Sex,Cou rseName FROM Student WHERE HobbyName=? AND Sex=? AND CourseName=?");
statement.setString(1,requ est.getPar ameter("Ho bbyName"). trim());
statement.setString(2,requ est.getPar ameter("Se x").trim() );
statement.setString(3,requ est.getPar ameter("Co urseName") .trim());
java.sql.ResultSet RS = statement.executeQuery();
Perhaps try trimming the parameters:
System.out.println(request
System.out.println(request
System.out.println(request
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.PreparedStatement
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
java.sql.ResultSet RS = statement.executeQuery();
ASKER
Nope,
trimming the parameters brings up an error saying null pointer exception.
any more suggestions...
trimming the parameters brings up an error saying null pointer exception.
any more suggestions...
ASKER
hi,
this is how my code stands right now.... and it still isnt running.
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setString(1,requ est.getPar ameter("Fi rstName")) ;
statement.setString(2,requ est.getPar ameter("La stName"));
statement.setString(3,requ est.getPar ameter("Ag e"));
statement.setString(4,requ est.getPar ameter("Co urseName") );
statement.setString(5,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
while(RS.next()) {
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
}
RS.close();
connection.close();
this is how my code stands right now.... and it still isnt running.
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
statement.setString(4,requ
statement.setString(5,requ
ResultSet RS = statement.executeQuery();
while(RS.next()) {
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
}
RS.close();
connection.close();
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setString(3,requ est.getPar ameter("Ag e"));
statement.setString(4,requ est.getPar ameter("Co urseName") );
statement.setString(5,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(2);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
RS.close();
connection.close();
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement
statement.setString(3,requ
statement.setString(4,requ
statement.setString(5,requ
ResultSet RS = statement.executeQuery();
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(2);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
RS.close();
connection.close();
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
There was little mistake in setString.
try this:
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setString(1,requ est.getPar ameter("Ag e"));
statement.setString(2,requ est.getPar ameter("Co urseName") );
statement.setString(3,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
RS.close();
connection.close();
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
try this:
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
ResultSet RS = statement.executeQuery();
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<table BORDER WIDTH="100%" >
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
RS.close();
connection.close();
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
<%
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setString(1,requ est.getPar ameter("Ag e"));
statement.setString(2,requ est.getPar ameter("Co urseName") );
statement.setString(3,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
%>
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
If this does not work, could you post us error message of the page.
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
%>
Make sure your request Paremter name (Age,CourseName,Sex) are correct
in case also.
If this does not work, could you post us error message of the page.
<%@ page import="java.sql.*" %>
// make sure imported required classes
<%
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setString(1,requ est.getPar ameter("Ag e"));
statement.setString(2,requ est.getPar ameter("Co urseName") );
statement.setString(3,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
}
catch (Exception e){
out.println(e.getMessage() );
}
%>
// make sure imported required classes
<%
try {
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student"
+ " WHERE Age = ?"
+ " OR CourseName = ?"
+ " OR Sex = ?"
;
java.sql.PreparedStatement
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
}
catch (Exception e){
out.println(e.getMessage()
}
%>
> trimming the parameters brings up an error saying null pointer exception.
Then the problem is that one of the request parameters is not set. ie. it is null.
Check that you arfe passing the request parameters correctly.
Then the problem is that one of the request parameters is not set. ie. it is null.
Check that you arfe passing the request parameters correctly.
insted of using Statement use preparedStatement and set the value for each "?" using setInt(), setString()..related functions.
ASKER
To allahabad, the code seems fine, no errors, but does not bring up the record. If I change the "AND" to "OR", it then lists all the females or males in the database. depending on which gender I have specfied. Obviously this is not what I want to achieve, thus there cant be anything wrong with the actual Access database. I have run out of ideas on what could be wrong with it.
To Objects, the request parameters are right, I have checked them over several times :(
To umangioshi, would you like to elaborate on what your saying, by using my code that I have pasted thanx :)
To Objects, the request parameters are right, I have checked them over several times :(
To umangioshi, would you like to elaborate on what your saying, by using my code that I have pasted thanx :)
i feel Age field could be giving problem in AND condition. So pass Age as Int .
<%@ page import="java.sql.*" %>
// make sure imported required classes
<%
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("jdbc:odb c:Student_ db","","") ;
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student "
+ " WHERE Age = ?"
+ " AND CourseName = ?"
+ " AND Sex = ?"
;
java.sql.PreparedStatement statement = connection.prepareStatemen t(sql);
statement.setInt(1,Integer .parseInt( request.ge tParameter ("Age")));
statement.setString(2,requ est.getPar ameter("Co urseName") );
statement.setString(3,requ est.getPar ameter("Se x"));
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
}
catch (Exception e){
out.println(e.getMessage() );
}
%>
<%@ page import="java.sql.*" %>
// make sure imported required classes
<%
try {
Class.forName("sun.jdbc.od
java.sql.Connection connection = java.sql.DriverManager.get
String sql = ""
+ " SELECT FirstName, LastName, Age, CourseName, Sex"
+ " FROM Student "
+ " WHERE Age = ?"
+ " AND CourseName = ?"
+ " AND Sex = ?"
;
java.sql.PreparedStatement
statement.setInt(1,Integer
statement.setString(2,requ
statement.setString(3,requ
ResultSet RS = statement.executeQuery();
%>
<table BORDER WIDTH="100%" >
<tr>
<td><b>First Name</b></td>
<td><b>Surname</b></td>
<td><b>Age</b></td>
<td><b>Course</b></td>
<td><b>Gender</b></td>
</tr>
<%
while(RS.next()) {
String firstName = RS.getString(1);
String lastName = RS.getString(1);
String Age = RS.getString(3);
String CourseName = RS.getString(4);
String Sex = RS.getString(5);
%>
<tr>
<td><%= firstName %></td>
<td><%= lastName %></td>
<td><%= Age %></td>
<td><%= CourseName %></td>
<td><%= Sex %></td>
</tr>
<%
}
%>
</table>
<%
RS.close();
connection.close();
}
catch (Exception e){
out.println(e.getMessage()
}
%>
> To Objects, the request parameters are right, I have checked them over several times :(
How have you checked them?
If one of the following lines is causing an NPE, then the request parameter must be null.
statement.setString(1,requ est.getPar ameter("Ho bbyName"). trim());
statement.setString(2,requ est.getPar ameter("Se x").trim() );
statement.setString(3,requ est.getPar ameter("Co urseName") .trim());
How have you checked them?
If one of the following lines is causing an NPE, then the request parameter must be null.
statement.setString(1,requ
statement.setString(2,requ
statement.setString(3,requ
I agree with 'objects'. Please check all your request parameters values and their length; this will help you to debug your problem, since you are very near to it.
Put these lines just below the try block and before JDBC class loading lines to see actually what are you getting from calling jsp.
out.println("Value of Age " +request.getParameter("Age "));
out.println("Length of Age " +request.getParameter("Age ").length( ));
out.println("Value of CourseName " +request.getParameter("Cou rseName")) ;
out.println("Length of CourseName " +request.getParameter("Cou rseName"). length());
// looks like you are geting value of 'Sex' correct, but still just print it for your information.
out.println("Value of Sex " +request.getParameter(Sex" ));
out.println("Length of Sex " +request.getParameter("Sex ").length( ));
If you are getting additional black spaces(in start or last), you can use 'objects' posting to trim those blank spaces, before passing those to sql where condition.
Put these lines just below the try block and before JDBC class loading lines to see actually what are you getting from calling jsp.
out.println("Value of Age " +request.getParameter("Age
out.println("Length of Age " +request.getParameter("Age
out.println("Value of CourseName " +request.getParameter("Cou
out.println("Length of CourseName " +request.getParameter("Cou
// looks like you are geting value of 'Sex' correct, but still just print it for your information.
out.println("Value of Sex " +request.getParameter(Sex"
out.println("Length of Sex " +request.getParameter("Sex
If you are getting additional black spaces(in start or last), you can use 'objects' posting to trim those blank spaces, before passing those to sql where condition.
Yes, I suggested earlier printing out the request values :)
ASKER
yes,
there are null values present. In Age, and in HobbyName.
In HobbyName when the program runs it just says.....
"Value of HobbyName null null", i do not know much jsp, so how do i rectify this problem... I have no idea.
there are null values present. In Age, and in HobbyName.
In HobbyName when the program runs it just says.....
"Value of HobbyName null null", i do not know much jsp, so how do i rectify this problem... I have no idea.
Check the page that is calling it is setting/passing the parameters correctly.
ASKER
Guess what? I think I know where my problem is. I didnt actually show any of you (allahabad and objects), where I was calling the page from. I havent set the Parameters on this page to retreive "HobbyName", "CourseName", and "Sex", this is the file (below). I am not sure how to either, so I'll experiment, with it whilst I wait for your professional advice :)
This is where I search from, and it calls the page that we have been working on... I hope this helps to find out where Ive been going wrong.
<font size="5" color="#003366"><b>Student - Search </b></font>
<form name="HobbySearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
<tr>
<td width="25%"><b>Search by Hobby:</b></td>
<td>
<select name="HobbyName">
<option> </option>
<option>Basketball</option >
<option>Cooking</option>
<option>Dancing</option>
<option>Football</option>
<option>Golf</option>
<option>Horse riding</option>
<option>Music</option>
<option>NetBall</option>
<option>StampCollecting</o ption>
<option>Swimming</option>
</select>
</td>
</tr>
</table>
</form>
<form name="CourseSearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
<tr>
<td width="25%"><b>Search by Course:</b></td>
<td>
<select name="CourseName">
<option> </option>
<option>Aerospace</option>
<option>Architecture</opti on>
<option>Art</option>
<option>Applied Science</option>
<option>Biology</option>
<option>Business</option>
<option>Chemistry</option>
<option>Computer Science</option>
<option>Digital Media Studies</option>
<option>English</option>
<option>Economics</option>
<option>French</option>
<option>Geography</option>
<option>GIS</option>
<option>History</option>
<option>Information Technology</option>
<option>Law</option>
<option>Mathematics</optio n>
<option>Music</option>
<option>Pharamacutical Science</option>
<option>Religious Studies</option>
<option>Sociology</option>
<option>Spanish</option>
<option>Sport Science</option>
</select>
</td>
</tr>
</table>
</form>
<form name="SexSearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
</tr>
<td width="28%">Sex:</td>
<td width="72%">
<input type="radio" name="Sex" value="Female" checked>
Female
<input type="radio" name="Sex" value="Male">
Male</td>
</tr>
<tr>
</td>
</tr>
</table>
<input type="submit" name="Search" value="Search">
</form>
This is where I search from, and it calls the page that we have been working on... I hope this helps to find out where Ive been going wrong.
<font size="5" color="#003366"><b>Student
<form name="HobbySearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
<tr>
<td width="25%"><b>Search by Hobby:</b></td>
<td>
<select name="HobbyName">
<option> </option>
<option>Basketball</option
<option>Cooking</option>
<option>Dancing</option>
<option>Football</option>
<option>Golf</option>
<option>Horse riding</option>
<option>Music</option>
<option>NetBall</option>
<option>StampCollecting</o
<option>Swimming</option>
</select>
</td>
</tr>
</table>
</form>
<form name="CourseSearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
<tr>
<td width="25%"><b>Search by Course:</b></td>
<td>
<select name="CourseName">
<option> </option>
<option>Aerospace</option>
<option>Architecture</opti
<option>Art</option>
<option>Applied Science</option>
<option>Biology</option>
<option>Business</option>
<option>Chemistry</option>
<option>Computer Science</option>
<option>Digital Media Studies</option>
<option>English</option>
<option>Economics</option>
<option>French</option>
<option>Geography</option>
<option>GIS</option>
<option>History</option>
<option>Information Technology</option>
<option>Law</option>
<option>Mathematics</optio
<option>Music</option>
<option>Pharamacutical Science</option>
<option>Religious Studies</option>
<option>Sociology</option>
<option>Spanish</option>
<option>Sport Science</option>
</select>
</td>
</tr>
</table>
</form>
<form name="SexSearch" method="Post" action="Three2.jsp">
<table border="0" cols="2" width="75%" >
</tr>
<td width="28%">Sex:</td>
<td width="72%">
<input type="radio" name="Sex" value="Female" checked>
Female
<input type="radio" name="Sex" value="Male">
Male</td>
</tr>
<tr>
</td>
</tr>
</table>
<input type="submit" name="Search" value="Search">
</form>
ASKER
Thanx objects, Ive only seen your reply now :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ITS WORKED!!!!
THANK YOU SO MUCH. YOU HAVE MADE MY DAY ALLAHABD :) THANK YOU A MILLION.
P.S HOW DO I GRADE YOU
P.S I HAVE TO ALSO PERSONALLY THANK OBJECTS. YOU WERE SUCH A GREAT HELP TOO
THANK YOU BOTH, FOR GOING OUT YOUR WAY TO HELP ME. PLEASE LET ME KNOW HOW TO GRADE YOU BOTH.
THANK YOU SO MUCH. YOU HAVE MADE MY DAY ALLAHABD :) THANK YOU A MILLION.
P.S HOW DO I GRADE YOU
P.S I HAVE TO ALSO PERSONALLY THANK OBJECTS. YOU WERE SUCH A GREAT HELP TOO
THANK YOU BOTH, FOR GOING OUT YOUR WAY TO HELP ME. PLEASE LET ME KNOW HOW TO GRADE YOU BOTH.
ASKER
As i said previously you were great. you responded to all my problems, and if i had used my brain, i would have sent my other page sooner, in order for you to have sussed it out.again Thank you so much, I am truly grateful.
neks xxx
neks xxx
You are welcome. I think you can post it 'Community Service' section to give points for 'objects'.
ASKER
> To objects
please let me know how to give you points even though i posted allahabad's comment as an answer, how can I award you points and grade you as well. I have 25 points left in my account so if i need to give you those cos Ive already processed my question i truly dont mind. Please please let me know
neksxx
please let me know how to give you points even though i posted allahabad's comment as an answer, how can I award you points and grade you as well. I have 25 points left in my account so if i need to give you those cos Ive already processed my question i truly dont mind. Please please let me know
neksxx
That's ok, keep your points for another question :-)
For ex. if you want to compare with lower case.
statement.setString(1,hobb
statement.setString(2,sexV
statement.setString(3,cour
java.sql.ResultSet RS = statement.executeQuery
("SELECT FirstName,LastName,HobbyNa