Solved

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

Posted on 2004-10-20
4
238 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …

738 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