Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to search a user-id in the mySQL database table using JAVA (JDBC and JSP)

Posted on 2004-10-20
4
233 Views
Last Modified: 2010-04-17
Hello

I have a form (JSP) which displays a text box.If i enter a user id in the text box and submit the form ,my JSP should search that user id in the mysql database table and should display a message whether the user id is already in the table or not.In case if the user id is not there then it should display a message as it is not there,if it is there it should display a message as it is there in the database.

In my case i could search the user id in the table but the message is displaying many times if the id is not there.It should be displayed only once if the id is not there.

Snippest of code would be helpful.

Please reply,
Thanks
Maruthi.
0
Comment
Question by:ramsin
4 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 63 total points
ID: 12358178
I don't have a snippet for JSP but the way we would do this in VB (syntax may be off)

In the submit click event

dim strSQL as string
dim rs1 as adodb.recordset
dim cn As New ADODB.Connection
Const SecCk = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDatabase;Data Source=MyServer"

    cn.ConnectionString = SecCk
    cn.Open

    strSQL = "SELECT userid FROM MyTable WHERE userid = '" & txtUser.text & "'"
    rs1 .CursorType = adOpenKeyset
    rs1 .CursorLocation = adUseServer
    rs1 .LockType = adLockOptimistic
 
    rs1 .OpenstrSQL, cn

     If rs1.OBF and rs1 .EOF Then
           msgbox "Not found"
     else
           msgbox "Found"
    end if

mlmcc
           



0
 
LVL 1

Assisted Solution

by:crulx
crulx earned 62 total points
ID: 12360462
Java Server Pages have been designed, like all tag based systems, to make this as quick and easy as possible. I will assume for the sake of this short little demo that you have a variable named “found” to indicate if you found the user_id and selected data into user_data.


<%
    if ( found ) {
        %>
        <P> Found User: <%= user_data %>
        <%
    } else {
        %>
        <P>Not Found.
        <%
    }
%>

Now if your code doesn’t look that simple, it’s due to you trying to check for the result you want instead of set in a loop. I would expect that your code has something like the following in it, with a bad SQL query at the head to boot.

Avoid This!
<%
  while(!rs.next()) {
   if( rs.GetString(1) == user_id) {
        %>
        <P> Found User: <%= user_data %>
        <%
        break;
    } else {
        %>
        <P>Not Found.
    }
  }
%>

Try to perform the query first, get the results into a “found” variable then display based on that variable outside of a loop.

Hope this helps.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question