MySQL + Java(Username and Password Validation) Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: MySQL + Java(Username and Password Validation) Help

  1. #1
    Join Date
    Mar 2011
    Posts
    6

    MySQL + Java(Username and Password Validation) Help

    I am trying to validate a username and password to my system using AES_ENCRYPT and AES_DECRYPT, however when logging in I get a error

    Code:
    ERROR: Column Index out of range, 3 > 2.
    Login Code
    Code:
     try {
                con = DriverManager.getConnection(url+dbName,userName,password);
                System.out.println("Connected to the database");
                Statement st = con.createStatement();
                ResultSet rt = st.executeQuery("SELECT AES_DECRYPT(`userPassword`, 'enigma') AS password, accessLevel FROM accounts WHERE username=('"+userNameE+"')");
               while(rt.next()){
                 passwordCheck = rt.getString(3);
                 userNameCheck = rt.getString(1);
                 accessLevel = rt.getInt(2);
               if (userNameCheck.equals(userNameE) && passwordCheck.equals(passwordE)){
                if(accessLevel == 1){
                       mu.setVisible(true);
                       mu.jButton1.setEnabled(false);
                        mu.jButton2.setEnabled(false);
                       loginFail = false;
                       con.close();}
                if(accessLevel == 2){
                      mu.setVisible(true);
                      mu.jButton5.setEnabled(false);
                      loginFail = false;
                       con.close();
                }
                System.out.println("Disconnected from database");
                   this.dispose();
                }
                  
            }
    Also for creating a user, is this correct, because when I was created a user before, it would validate it, but when I created a user using the MySQL command client, it seemed to validate that instead of the java version.

    Code:
    public void createUser(){
    
             if(jTextField1.getText().isEmpty() == false && jPasswordField2.getPassword().toString().isEmpty() == false &&
               jPasswordField1.getPassword().toString().isEmpty())
             if(!jPasswordField2.getPassword().equals(jPasswordField1.getPassword())){
                             new Menu().setVisible(true);
             }
             
             {
                 try{
                   con = DriverManager.getConnection(url+dbName,userName,password);
                      System.out.println("Connected to the database");
                   PreparedStatement pst = con.prepareStatement("INSERT INTO accounts (username, accessLevel, userPassword) VALUES (?,?,AES_ENCRYPT('"+jPasswordField1.getPassword()+"', 'enigma')");
                   pst.setString(1, jTextField1.getText());
                   pst.setInt(2, accessLevel);
                   pst.executeUpdate();
                            con.close();
                              System.out.println("Disconnected from database");
                                    new Menu().setVisible(true);
                                    this.dispose();
                 } catch (SQLException e) {System.err.println("ERROR: " + e.getMessage()); }
             }
    
       
        }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: MySQL + Java(Username and Password Validation) Help

    I am trying to validate a username and password to my system using AES_ENCRYPT and AES_DECRYPT, however when logging in I get a error
    In future please post the full error message and stack trace.

    I suspect the problem is your sql statement is getting the password and access level (ie 2 columns) but you are trying to access the 3rd column of the result set. The result set will only contain 2 columns (1=password, 2=access level) and hence it is throwing an exception.

    Also for creating a user, is this correct, because when I was created a user before, it would validate it, but when I created a user using the MySQL command client, it seemed to validate that instead of the java version.
    Sorry but I've no idea what you mean by this.
    As for whether the code is correct or not why not run it and then use the command line tool to check the database to see if a new record has been added and that it contains the correct values.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center