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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Record exist or not

Status
Not open for further replies.

mic12345

Programmer
Jun 19, 2002
14
0
0
US
Hi,

I'm checking for whether record exist in database or not. But it is not working. Though record exist, I never get the message.
***********************************************************************
Here is the table looks like.

EMPLID NAME EMAILID

1 Maruthi mbalmuri@hotmail.com
2 test test@test.com
3 test test@yahoo.com
********************************************************************
Here is the program.

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class updateDatabase extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.println(&quot;<HTML>&quot;);
out.println(&quot;<HEAD><TITLE>Display the records</TITLE></HEAD>&quot;);
out.println(&quot;<BODY>&quot;);

out.print(&quot;<code><pre>&quot;);
out.println(&quot;ID\tFirst Name\tLast Name\n&quot;);

// connect to database and query database
try
{
String myEmplid = req.getParameter(&quot;Emplid&quot;).trim();
String myName = req.getParameter(&quot;Name&quot;).trim();
String myEmail = req.getParameter(&quot;Email&quot;).trim();
boolean proceed = false;

// Using Sybase JCONNECT 5.2
Class.forName(&quot;com.sybase.jdbc2.jdbc.SybDriver&quot;);

Connection con =
DriverManager.getConnection(&quot;jdbc:sybase:Tds:server:port#/dbase&quot;, username, password);
Statement stmt = con.createStatement( );

ResultSet rs = null;
String rsEmplid;
String recexist = &quot;N&quot;;
rs = stmt.executeQuery(&quot;SELECT * FROM PS_PE&quot;);

while(rs.next())
{
rsEmplid = rs.getObject(1).toString().trim();

out.print(&quot;This is rsEmplid : &quot; + rsEmplid + &quot;\n&quot;);
out.print(&quot;This is myEmplid : &quot; + myEmplid + &quot;\n&quot;);

if (rsEmplid == myEmplid)
{
recexist = &quot;Y&quot;;
out.print(&quot;Record Exist\n&quot;);
break;
}
} // end of while
out.print(&quot;Record Exist value &quot; + recexist);
} // end of try

catch ( Exception e )
{
out.println( &quot;An exception occurred.&quot; + e );
}
} // end doGet method

} // end of myConnection class
************************************************

here is the output.

ID First Name Last Name

This is rsEmplid : 1
This is myEmplid : 1
This is rsEmplid : 2
This is myEmplid : 1
This is rsEmplid : 3
This is myEmplid : 1
Record Exist value N

Thanks in advance
 
>> if (rsEmplid == myEmplid)

change to
if( rsEmplid.equals(myEmplid))
or
if( rsEmplid.equalsIgnoreCase(myEmplid))


-pete
 
to understand why what palbano suggested works check out the thread &quot;comparing string literals and string variables...&quot; for an explanation of string equality. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top