Results 1 to 6 of 6
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Question about ResultSetMetadata methods

    If i am writing a program that needs to query a sql database for username and password, can i use resultsetmetadata class to find out if that username and password was locationed in the database and if so how.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Question about ResultSetMetadata methods

    A simple select query should tell you if the username/password combination is within the database. See the API for ResultSetMetaData - it provides meta data associated with a query, you retrieve the results of a query through a ResultSet.

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: Question about ResultSetMetadata methods

    if i were to just use a simple select query what would be returned if the username and password was not found

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Question about ResultSetMetadata methods

    Did you try it? What happened?

  5. #5
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: Question about ResultSetMetadata methods

    what i did was to create two local variables name userName and password. Then i used the userName and password supplied has the criteria for my search. I then store the result of the search in the local username and password variables. then i do a comparision to to find out if the username in the database matched the entered

    Java Code:
     public boolean log ()
    	 {
    	      String userName = "",password = "";
    	      try {
    	      Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/database", "root","");
    	      Statement state = con.createStatement ();
    	      ResultSet rs = state.executeQuery("SELECT userName,password FROM userTable WHERE userName='" + this.userName + "'");
    	      
    	      while (rs.next())
    	      {
    	    	  userName = rs.getString("userName");
    	    	  password = rs.getString("password");
    	      }
    	      catch (SQLException e)
    	      {
    	    	  System.out.println (e.getMessage());
    	      }
    	      
    	      if (this.userName.equal(userName) && this.password.equals (password))
    	    	  return true
    	    			  
    	    	  else return false;
    	}
    what is want to know is if i could have just done the search without creating the local variables and use the resultset metadata to find out if the username and password is in the database

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question about ResultSetMetadata methods

    I'd do something like this: make the db return a single 1 if the username and password match; if they don't match the ResultSet is empty and you know enough; there is no need to transfer a correct password over the wire ...

    Java Code:
    select 1 
    from userTable 
    where username = ? and password = ?
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Question concerning methods
    By jim01 in forum New To Java
    Replies: 6
    Last Post: 05-18-2011, 02:46 PM
  2. 2 methods with same name Question
    By dienesh77 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 01:58 PM
  3. Replies: 3
    Last Post: 10-05-2009, 10:10 AM
  4. resultsetmetadata
    By thamizhisai in forum Advanced Java
    Replies: 1
    Last Post: 05-27-2008, 07:41 AM
  5. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 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
  •