This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "search" Watch "search" New topic
Author

search

jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
There is a searching facility iam trying to implement in my jsp application. It has 2 fields that is the firstname and lastname and on that basis a page will be displayed containing the matching names and on clicking on the particular name one can see the home page of the person.
can anyone give me some idea about the same..
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
hai,
I understood what you want to do.But inorder to help you, you should clearly
specify in what way you are doing and where you are getting the problem.
Jyothsna
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
hey!! same names .. good coincidence..
iam trying to display names in my page.. like if the user enters anoo in the name field ..then all the names whihc have 'anoo' in the beginning or in the middle or end will be displayed.
in my sql i write query as select * from table where fname like '%anoo%' and iam able to get the results but in my jsp page iam not able to
i have written the query like
rs = stmt.executeQuery("select fname,lname from login where fname like %'"+fname+"'% or lname like %'"+lname+"'%");
iam getting a null pointer exception in this..
can u help me out in this..
and have a great life ahead!!
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
hi,
in my sql i write query as select * from table where fname like '%anoo%' and iam able to get the results but in my jsp page iam not able to

so you are getting the results....
rs = stmt.executeQuery("select fname,lname from login where fname like %'"+fname+"'% or lname like %'"+lname+"'%");
iam getting a null pointer exception in this..

so you are getting the exception.
i am not understanding you help for the query or getting the results to jsp page.
Jyothsna
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
in the sql query analyzer the records are displayed . but when i write the same query in my jsp page..iam not getting any records in my next page. and the tomcat window gives a null pointer exception..
where does the error lie?? iam tired of debugging!
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
here is my code:

<%!
Connection con;
java.sql.Statement stmt;
ResultSet rs;
%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:mech_iit");
stmt = con.createStatement();
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
System.out.println(con);
System.out.println(stmt);

System.out.println("select * from login where fname like %'"+fname+"'% or lname like %'"+lname+"'%");
if(rs.next())
{
rs.close();

rs = stmt.executeQuery("select fname,lname from login where fname like %'"+fname+"'% or lname like %'"+lname+"'%");

while(rs.next())
{
fname = rs.getString("fname") ;
lname = rs.getString("lname");
%>
<table border="1" width="400">
<tr><td><%=fname %></td>
<td><%=lname %></td>
</tr>
<%
}
}
else
{
%>
<b>Sorry there is no any match.<b><br>

<%
}
}
catch(Exception e)
{
System.out.println(e);
}
%>
</table>
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 640
Hi,
can you tell,why have you written these lines

This line will always throw nullpointer exception as rs is null before execution of this statement.
removing if statement will solve your problem......
Njoy Serching...
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
try Praful's solution.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Moving to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: search