Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

20 Experts available now in Live!

Get 1:1 Help Now