Results 1 to 8 of 8
  1. #1
    efozdel is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default mysql (Basic opening page operation using database)

    Hi everybody lots of thanks for your previous helps,

    my question is about how to build a check "username/password". I cant figure out it.

    String username = t1.getText();
    String passwords = t2.getText();
    Statement st;
    Statement st2 = null;

    try {
    st = openConnection();
    res = st.executeQuery("SELECT * FROM giris WHERE=" + username);
    res2 = st2.executeQuery("SELECT * FROM giris WHERE=" + password);

    } catch (Exception ex) {

    JOptionPane.showConfirmDialog(null, "Bağlantı Başarısız", "MySQL Bağlantısı", JOptionPane.PLAIN_MESSAGE);
    }

    I want to build a classic username/password page. if username and password are true, then pass to rest part of the program.

    Thanks in advance to everybody.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What's the structure of your table, can you show it here?

    Basically what you have to do is, write a single query and check the combination of user name and the password. The you can check the effective number of rows, should be only one. Or else return the table index. It depends on the design you've.

  3. #3
    efozdel is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What's the structure of your table, can you show it here?
    in my table, there are three rows and two columns at the beginning.However, row number will increase when users add new users.

    if(!res.isFirst())
    res.first();

    do
    {

    if (t1.getText().equalsIgnoreCase(res.getString(1)) && t2.getText().equalsIgnoreCase(res.getString(2))) {

    Anasayfa yeni = new Anasayfa();
    giris eski = new giris();

    yeni.setVisible(true);
    dispose();
    res.close();
    }
    }while(res.next());


    if(!res.isClosed())
    {
    JOptionPane.showMessageDialog(null,"Kullanıcı Adı / Şifre Yanlış");
    t1.setText("");
    t2.setText("");
    res.close();
    }
    } catch (SQLException ex) {
    // Logger.getLogger(giris.class.getName()).log(Level. SEVERE, null, ex);
    }


    this is working now but I dont think so it is so reliable..:)

    your corrections will be appreciated ..

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes, by adding new users always the number of rows are increasing. Without that how you can added new users. But the point is two or more users cannot be there with the same user name. So always your query returns one entry.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    So long as your user table has a unique key (in the form of the username in your case), you need just the single query (as already mentioned). That query will return either one row or none, so a check of
    Java Code:
    if (rs.next()) {
        // Successfully logged in!
    }
    will do.

    If you find yourself getting data from a db and then iterating over the resultset to find a subset of what's returned then you have probably got the query wrong.

  6. #6
    efozdel is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Thanks for your helps Eranga and Tolls..

    with the help of your helps , I got where my problem is.. Previously, I tried to query a table and check that result set in my program. This causes a problem in which Username or Password is wrong message box .anyway.. Thanks .. :)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Adding more, in most of the designs successful queried returns a session key for the user, which use in the rest of the process.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  2. database with mysql using the netbeans 6.0
    By kwesiaryee in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 04:27 AM
  3. MySQL Database and Java
    By shaggymac in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 09:01 PM
  4. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 01:35 AM
  5. Opening frame/page options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-10-2008, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •