Skip to content
Advertisement

Can’t extract string from database sql

My aim is to test if the user and password inserted, existed in Table1. However, if I typed (pink,floyd) which exists in the database count still null and it appears the message “user doesn’t exist”.

  b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String user=f_user.getText().trim();
                String pass=f_pass.getText().trim();
                String sql="select   user, pass from "Table1" where user='"+user+"' and pass='"+pass+"'";
                rs=stat.executeQuery(sql);
                int count=0;
                while(rs.next()){
                    count++;
                }
                if (count==0) JOptionPane.showMessageDialog(null, "user doesn't exist");
                else JOptionPane.showMessageDialog(null, "acces permitted");
            } catch ( Exception ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }

Here’s my database :

enter image description here

Advertisement

Answer

It turns that the stocked user and pass parameters in Table1 hasn’t the same length of input parameters . So , we need to change :

String sql="select   user, pass from "Table1" where user='"+user+"' and pass='"+pass+"'";

to

String sql="select * from "Table1" where trim("user")= '"+user+"' and trim("pass")= '"+pass+"'";
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement