Solved

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

Posted on 2004-10-20
4
231 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now