Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

831 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