Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to solve this issue

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to solve this issue

    I have a question - not sure where to post it, but i'll try here.

    So I have the following piece of code:

        public boolean checkUser(String user, String password){
            boolean connected = false;
            try{
               Class.forName("com.mysql.jdbc.Driver").newInstance();
               Connection con = DriverManager.getConnection("jdbc:mysql://localhost/finalprojects", "root", "password");
               Statement st = con.createStatement();
               ResultSet rs = st.executeQuery("select * from users where user ='" + user + "' and password ='" + password +"';");
               connected = true;
               rs.close();
               st.close();
               return connected;
            }catch(Exception e){
                e.printStackTrace();
            }
     
            return connected;
        }

    Now what is suppose to happen is you enter (into a form) a user name of say Jake and a password of Jake. this will query the database based on that information and return true IF the user exists with the username and password you entered.

    what I need to know is how to say:

    Ok so username Jake and password Jake doesn't exist then return false - which then gets passed up to the page and the user is alerted that hey you don't exist. (the passing to the page is done).

    What are the steps I need to take to query the database to check for username and password and see if they match that which the user has entered or not.

    Because right now if you exist - great - return true. if not - return false.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to solve this issue

    Hello and welcome adrienk.
    For future reference, such a question could have gone to the JDBC & Databases section, but nevermind for now
    Assuming the user name and password will be unique, from a basic point of view, all you need to do is check if a result was found.
    To do this, check out the next() or hasNext() method in the ResultSet API.
    ResultSet (Java 2 Platform SE v1.4.2)

    There are quite a few other methods that could achieve the same thing aswell, but any of those two should be sufficient.
    Check back if you have any further issues.
    Last edited by newbie; April 11th, 2011 at 04:54 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 04:26 PM
  2. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 01:45 PM
  3. Plz solve the problem
    By rasheedmgs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 14th, 2010, 11:59 AM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM