Results 1 to 2 of 2
  1. #1
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default validating JTextField data from result set

    hi guys,


    I am trying to setup a login system where when a user enters his username and password and presses the login button, his username and password are sent in an SQL statements to be matched with a User in the database. if the username and password are correct(if they are matched in the database) a User object is created with all of the users data from the database and the user is brought to the Admin Dashboard

    After doing a lot of research on Result sets etc and going through a lot of code, I still can not make it work in my system. Can anyone show me a good way to do this? Or tell me if there is something obvious wrong with this code?

    At the moment when I enter in a username and password and press login I get an error in the console:
    java.sql.SQLException: Invalid column index at oracle.jdbc.driver.OraclePreparedStatement.setStri ngInternal(OraclePreparedStatement.java:5321) at oracle.jdbc.driver.OraclePreparedStatement.setStri ng(OraclePreparedStatement.java:5309) at oracle.jdbc.driver.OraclePreparedStatementWrapper. setString(OraclePreparedStatementWrapper.java:280) at database.UserOperations.validateLog(UserOperations .java:45) at gui.Login$2.mouseClicked(Login.java:77)

    some of the gui code

    Java Code:
    JButton btnLogin = new JButton("Login");
        btnLogin.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
    
                UserOperations userOperations = new UserOperations();
    
                ResultSet rset = userOperations.validateLog(textFieldUsername.getText(), textFieldPassword.getText());
                User user = null;
                try {
                    if (rset.next()) {
                        user = new User(rset.getInt("userID"), rset.getString("userName"), rset.getString("userPassword"), rset.getString("securityQuestion"),
                                              rset.getString("securityAnswer"),
                            rset.getInt("adminRights"), rset.getString("email"), rset.getString("fName"), rset.getString("phoneNumber"), rset.getString("department"));
    
                        dispose();
                        AdminDashboard admin = new AdminDashboard();
                        admin.setVisible(true);
    
                    }
                    else{
    
                        JOptionPane.showMessageDialog(null, "Login fail!", "Error", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
    
            }
        });
    validateLogin() method

    Java Code:
    public ResultSet validateLog(String username, String userpassword)
    {
        try {
            String sql = "SELECT userid, username, userpassword, securityquestion, securityanswer, adminrights, email, fname, phonenumber, department FROM systeuser
                             WHERE username = ? AND userpassword = ?";
    
            stmt = conn.prepareStatement(sql);
            stmt.setString (2, username);
            stmt.setString (3, userpassword);
            rset = stmt.executeQuery ( );
    
        }
        catch(Exception e){
            e.printStackTrace ( );
            JOptionPane.showMessageDialog (null, "Login Failed", null, JOptionPane.WARNING_MESSAGE);
        }
        return rset;
    
    }
    any help is greatly appreciated
    just a beginner trying to find his way :rolleyes:

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: validating JTextField data from result set

    You only have two parameters, which will be indexed '1' and '2'.
    You've given '2' and '3' for the indexes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Populating a jTextField with data from another class
    By dandapice in forum New To Java
    Replies: 4
    Last Post: 10-11-2012, 04:53 PM
  2. Replies: 12
    Last Post: 06-02-2011, 07:54 PM
  3. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 11:15 AM
  4. Validating the jtable data on cell
    By anilkumar_vist in forum Advanced Java
    Replies: 4
    Last Post: 10-13-2010, 02:31 PM
  5. Replies: 7
    Last Post: 08-29-2010, 11:33 AM

Posting Permissions

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