0

I have a swing jframe that has a login screen and I'm trying to execute a phpmyadmin database query. It connects to the database but it doesn't allow me to log in and just infinity says "invalid username or password" Here is the code.

JPasswordField jpfpass;
JLabel jlabuser, jlabpass;

public RychlikSystemversion0(String name) {
    super(name);
    btnlogin = new JButton("Login");
    btenreset = new JButton("Reset");
    btnexit = new JButton("Exit");

    jtfuser = new JTextField();
    jpfpass = new JPasswordField();

    jlabuser = new JLabel("Ussername");
    jlabpass = new JLabel("Password");

    this.setLayout(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    btnlogin.addActionListener(this);
    btenreset.addActionListener(this);
    btnexit.addActionListener(this);

    jlabuser.setBounds(10, 10, 120, 20);
    jlabpass.setBounds(10, 30, 120, 20);
    jtfuser.setBounds(140, 10, 300, 20);
    jpfpass.setBounds(140, 30, 300, 20);

    btnlogin.setBounds(140, 55, 100, 20);
    btenreset.setBounds(240, 55, 100, 20);
    btnexit.setBounds(340, 55, 100, 20);

    this.add(jlabuser);
    this.add(jlabpass);
    this.add(jtfuser);
    this.add(jpfpass);
    this.add(btnlogin);
    this.add(btenreset);
    this.add(btnexit);

    this.setSize(500, 300);
    this.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
    //String USERNAME = "root";
    //String PASSWORD = "root ";
    //String CONN_STRING = "jdbc:mysql://localhost::3307/osdesign";

    Connection connection;
    PreparedStatement ps;
    try {
        String dbName = "osdesign";
        String dbUserName = "root";
        String dbPassword = "";
        //String u = jtfuser.getText();
        //String p =jpfpass.getName();
        String connectionString = "jdbc:mysql://localhost/" + dbName + "?user="
                + dbUserName + "&password=" + dbPassword
                + "&useUnicode=true&characterEncoding=UTF-8";
        connection = DriverManager.getConnection(connectionString);
        JOptionPane.showMessageDialog(null, "Connected!");
        String sql = "SELECT * FROM `userlist` WHERE `UsrName` = ? AND `UsrPass` = ?";
        ps = connection.prepareStatement(sql);

        jtfuser.setText("Test");
        jpfpass.setText("Test");
        ps.setString(1, jtfuser.getText());
        ps.setString(2, jpfpass.getName());
        //JFrame panel = new JFrame();
        ResultSet result = ps.executeQuery();
        btnlogin.equals(result);
        // panel.add(btnlogin);
        //   panel.add(jpfpass.getName(),btnlogin);
        if (result.next()) {
            JOptionPane.showMessageDialog(null, "Login Sucessful");  //Wont execute statement
        } else {
            JOptionPane.showMessageDialog(null,  "Invalid user name or password");     //Always does this
    }
} catch (SQLException ex) {
        Logger.getLogger(RychlikSystemversion0.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void main(String[] args) {
    RychlikSystemversion0 ls = new RychlikSystemversion0("System login");
}

I've tried several things it still doesn't allow me to login. Any help?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • what username and password you try with and exist in database? – Youcef LAIDANI Apr 22 '17 at 19:10
  • You should check your username and password with MySQL, use CMD to connect to mySQL, command -> mysql -u root -p admin – Emad Apr 22 '17 at 19:16
  • Did you search the forum first before posting the question? Like maybe this question: http://stackoverflow.com/questions/15852195/drivermanager-no-suitable-driver-mysql. I would always use the `getConnection(...()` method where you pass the user/password as separate parameters to keep the connection string simpler so you are less likely to make a syntax mistake. – camickr Apr 22 '17 at 19:22
  • @camickr i think the OP use a static username and password `jtfuser.setText("Test"); jpfpass.setText("Test");` – Youcef LAIDANI Apr 22 '17 at 19:28
  • 2
    @YCF_L, that wasn't my point. Look at the OP's "connectionString" and all the string concatenation which could cause a syntax error if not done correctly. instead use: `getConnection(connectionString, user, password)`. With less formatting in the connection string you are less likely to have syntax errors. – camickr Apr 22 '17 at 20:08

1 Answers1

0

The problem is with:

jpfpass = new JPasswordField();
//[..]
ps.setString(2, jpfpass.getName());

The method getName(), does not provide you with the password in the field, instead it gives you the name of the component (which in your code isn't even set).

If you want to obtain the password from the JPasswordField, you need to use getPassword(). This method returns a char[], so you need to change your code to use:

ps.setString(2, new String(jpfpass.getPassword()));

If in the future you are confronted with problems like this, first check your assumptions. Step through your code in a debugger and check if the values are what you expect, or just add some debug System.out.println(..) of the relevant values.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197