Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Username ,Password Validation

Username ,Password Validation

Scheduled Pinned Locked Moved Java
databaseregexhelp
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    I want to validate Username and Password matching from my sql Database,but it didn't work.Even if the text fields are blank it does not give any error.

    private static void selectfromdb() throws SQLException
    {
    Statement stmt = Connectionstring().createStatement();
    ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");
    while (rs.next())
    {
    String x = tf_Fname.getText();
    String pass = new String(tf_Lname.getPassword());
    if (x.equals(rs.getString("Username")))
    {
    if (pass.equals(rs.getString("Password")))
    {
    JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING_MESSAGE);
    //System.out.println("Logged in!");
    }
    else
    {
    JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING_MESSAGE);
    // System.out.println("Password did not match username!");
    }
    }
    }

    	}
    
    B L J 3 Replies Last reply
    0
    • C chdboy

      I want to validate Username and Password matching from my sql Database,but it didn't work.Even if the text fields are blank it does not give any error.

      private static void selectfromdb() throws SQLException
      {
      Statement stmt = Connectionstring().createStatement();
      ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");
      while (rs.next())
      {
      String x = tf_Fname.getText();
      String pass = new String(tf_Lname.getPassword());
      if (x.equals(rs.getString("Username")))
      {
      if (pass.equals(rs.getString("Password")))
      {
      JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING_MESSAGE);
      //System.out.println("Logged in!");
      }
      else
      {
      JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING_MESSAGE);
      // System.out.println("Password did not match username!");
      }
      }
      }

      	}
      
      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Try this

      private static void selectfromdb() throws SQLException
      {
      Statement stmt = Connectionstring().createStatement();
      ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");

       rs.last();
      int rowcount = rs.getRow();
      if(rowcount ==0){
      JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING\_MESSAGE);
      }
      else{
      	JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING\_MESSAGE);
      }
      

      }


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

      L C 2 Replies Last reply
      0
      • B Blue_Boy

        Try this

        private static void selectfromdb() throws SQLException
        {
        Statement stmt = Connectionstring().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");

         rs.last();
        int rowcount = rs.getRow();
        if(rowcount ==0){
        JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING\_MESSAGE);
        }
        else{
        	JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING\_MESSAGE);
        }
        

        }


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Blue_Boy wrote:

          ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf\_Fname+"' AND Password ='"+tf\_Lname+"'");
        

        It is really bad form to give sample code that can lead to SQL injection[^].

        Use the best guess

        B 1 Reply Last reply
        0
        • L Lost User

          Blue_Boy wrote:

            ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf\_Fname+"' AND Password ='"+tf\_Lname+"'");
          

          It is really bad form to give sample code that can lead to SQL injection[^].

          Use the best guess

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          I didn't gave example to use query which is able to inject, the user used by him/herself, i just gave solution how to manage login case.


          I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

          1 Reply Last reply
          0
          • B Blue_Boy

            Try this

            private static void selectfromdb() throws SQLException
            {
            Statement stmt = Connectionstring().createStatement();
            ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");

             rs.last();
            int rowcount = rs.getRow();
            if(rowcount ==0){
            JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING\_MESSAGE);
            }
            else{
            	JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING\_MESSAGE);
            }
            

            }


            I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

            C Offline
            C Offline
            chdboy
            wrote on last edited by
            #5

            In both the cases even if it's right password it shows "Login UNSuccessful"

            1 Reply Last reply
            0
            • C chdboy

              I want to validate Username and Password matching from my sql Database,but it didn't work.Even if the text fields are blank it does not give any error.

              private static void selectfromdb() throws SQLException
              {
              Statement stmt = Connectionstring().createStatement();
              ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");
              while (rs.next())
              {
              String x = tf_Fname.getText();
              String pass = new String(tf_Lname.getPassword());
              if (x.equals(rs.getString("Username")))
              {
              if (pass.equals(rs.getString("Password")))
              {
              JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING_MESSAGE);
              //System.out.println("Logged in!");
              }
              else
              {
              JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING_MESSAGE);
              // System.out.println("Password did not match username!");
              }
              }
              }

              	}
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Use your debugger to step through the code and see what values your variables contain at each stage.

              Use the best guess

              C 1 Reply Last reply
              0
              • C chdboy

                I want to validate Username and Password matching from my sql Database,but it didn't work.Even if the text fields are blank it does not give any error.

                private static void selectfromdb() throws SQLException
                {
                Statement stmt = Connectionstring().createStatement();
                ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails where Username='"+tf_Fname+"' AND Password ='"+tf_Lname+"'");
                while (rs.next())
                {
                String x = tf_Fname.getText();
                String pass = new String(tf_Lname.getPassword());
                if (x.equals(rs.getString("Username")))
                {
                if (pass.equals(rs.getString("Password")))
                {
                JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.WARNING_MESSAGE);
                //System.out.println("Logged in!");
                }
                else
                {
                JOptionPane.showMessageDialog(null,"Login UNSuccessful! ","UNSuccessful",JOptionPane.WARNING_MESSAGE);
                // System.out.println("Password did not match username!");
                }
                }
                }

                	}
                
                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                So the code is telling you the values are not equal. Figure out what the values are and you will see the difference. Either a debugger or a print them out. But as a guess there is a case difference. The query doesn't care but the comparison does.

                C 1 Reply Last reply
                0
                • L Lost User

                  Use your debugger to step through the code and see what values your variables contain at each stage.

                  Use the best guess

                  C Offline
                  C Offline
                  chdboy
                  wrote on last edited by
                  #8

                  The row count is 0 when I debug it....but I'm using the same username and password what's in the database,why is it not accepting it?

                  L 1 Reply Last reply
                  0
                  • C chdboy

                    The row count is 0 when I debug it....but I'm using the same username and password what's in the database,why is it not accepting it?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    chdboy wrote:

                    The row count is 0 when I debug it....but I'm using the same username and password what's in the database,why is it not accepting it?

                    I have no idea since I have no access to your system. You need to investigate further to discover what is happening.

                    Use the best guess

                    C 1 Reply Last reply
                    0
                    • J jschell

                      So the code is telling you the values are not equal. Figure out what the values are and you will see the difference. Either a debugger or a print them out. But as a guess there is a case difference. The query doesn't care but the comparison does.

                      C Offline
                      C Offline
                      chdboy
                      wrote on last edited by
                      #10

                      com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.

                      But when I see it in the Database there are plenty of rows. I don't understand why it is not picking up the rows ?

                      1 Reply Last reply
                      0
                      • L Lost User

                        chdboy wrote:

                        The row count is 0 when I debug it....but I'm using the same username and password what's in the database,why is it not accepting it?

                        I have no idea since I have no access to your system. You need to investigate further to discover what is happening.

                        Use the best guess

                        C Offline
                        C Offline
                        chdboy
                        wrote on last edited by
                        #11

                        Now I have changed the code

                        PreparedStatement preparedStatement = Connectionstring().prepareStatement(
                        "Select Username from dbo.LoginDetails where Username = ? and Password =?");
                        String User = tf_Fname.getText();
                        String _Pass = new String(tf_Lname.getPassword());
                        preparedStatement.setString(1, User);
                        preparedStatement.setString(2, _Pass);
                        ResultSet resultSet = preparedStatement.executeQuery();
                        while (resultSet.next())
                        {
                        System.out.println("Username is "+ resultSet.getString(1)+"Password is "+resultSet.getString(2));
                        }

                        But now it's picking up the right Username from the table ,if I don't use

                        +"Password is "+resultSet.getString(2)

                        in the Print line,and if I use that I get an error

                        com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.

                        L 1 Reply Last reply
                        0
                        • C chdboy

                          Now I have changed the code

                          PreparedStatement preparedStatement = Connectionstring().prepareStatement(
                          "Select Username from dbo.LoginDetails where Username = ? and Password =?");
                          String User = tf_Fname.getText();
                          String _Pass = new String(tf_Lname.getPassword());
                          preparedStatement.setString(1, User);
                          preparedStatement.setString(2, _Pass);
                          ResultSet resultSet = preparedStatement.executeQuery();
                          while (resultSet.next())
                          {
                          System.out.println("Username is "+ resultSet.getString(1)+"Password is "+resultSet.getString(2));
                          }

                          But now it's picking up the right Username from the table ,if I don't use

                          +"Password is "+resultSet.getString(2)

                          in the Print line,and if I use that I get an error

                          com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I guess that you need to look more closely at the contents of your ResultSet and its individual property items. Check with your debugger and also the documentation for the classes that you are working with.

                          Use the best guess

                          C 1 Reply Last reply
                          0
                          • L Lost User

                            I guess that you need to look more closely at the contents of your ResultSet and its individual property items. Check with your debugger and also the documentation for the classes that you are working with.

                            Use the best guess

                            C Offline
                            C Offline
                            chdboy
                            wrote on last edited by
                            #13

                            I think I got what is wrong with it.....I'm missing Select Username,Password ...hehe thanks

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups