0

I am trying to get out of this error.Would you mind giving me a hand in here? Below is my code.

package Viewer;

import Model.JDBC;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class User extends javax.swing.JFrame 
{
    Model.JDBC j=new JDBC();

    public User() 
    {
        initComponents();
        eid.grabFocus();
    }
    @SuppressWarnings("unchecked")                   

    private void saveActionPerformed(java.awt.event.ActionEvent evt) 
    {                                     
        try 
        {
            ResultSet rs = j.getData("select eid from user where eid='" + eid.getText() + "'");
            if (rs.next()) 
            {
                if (eid.getText().equals(rs.getString(1))) 
                {
                    JOptionPane.showMessageDialog(null, "Existing User Name");
                    pwd.setText("");
                    cpwd.setText("");
                }
            } 
            else 
            {
                if (eid.getText().isEmpty())
                {
                    JOptionPane.showMessageDialog(null,"Enter User Name");
                    eid.grabFocus();
                } 
            else if (pwd.getText().isEmpty()) 
            {
                JOptionPane.showMessageDialog(null,"Enter Password");
            } 
            else if (pwd.getText().isEmpty()) 
            {
                JOptionPane.showMessageDialog(null,"Enter Password");
            } 
            else 
            {
                String pass = new String(pwd.getPassword());
                String pas1 = new String(cpwd.getPassword());

                if (pass.equals(pas1)) 
                {
                    String ps = new String(pwd.getPassword());
                    String s = ps;
                    try 
                    {
                        j.putData("insert into user values ('" + eid.getText() + "','" + s + "','" + ulevel.getSelectedItem() + "','"+pwd.getText()+"','"+cpwd.getText()+"')");
                        JOptionPane.showMessageDialog(null,"User Added Successfully.. ");
                        j.getCon().close();
                        clear();
                    } 
                    catch (Exception e) 
                    {
                        System.out.println(e + "101 User Adding");
                    }
                } 
                else 
                {
                    JOptionPane.showMessageDialog(null,"Password Not Match");

                    pwd.grabFocus();
                    pwd.setText("");
                    cpwd.setText("");
                }
            }
        }
    } 
    catch (Exception e) 
    {
        System.out.println(e);
    }
 }                                    
    private void deleteActionPerformed(java.awt.event.ActionEvent evt) 
    {                                       
        try 
        {
        ResultSet rs = j.getData("select * from user where eid='" + eid.getText() + "'");
            if (rs.next()) 
            {
                if (rs.getString(1).equals(eid.getText())) 
                {             
                    String a = (pwd.getText());
                    String b = (cpwd.getText());
                    if((rs.getString(2).equals(a)) && (rs.getString(2).equals(b)))
                    {
                        int t = JOptionPane.showConfirmDialog(null, "Are You Sure...?");
                        if(t==JOptionPane.YES_OPTION)
                        {
                            j.putData("delete from user where eid='"+eid.getText()+"'");
                            clear();
                        }
                    }
                else if(!rs.getString(2).equals(a))
                {
                    JOptionPane.showMessageDialog(null, "Password Not Match");
                    pwd.setText("");
                    cpwd.setText("");
                }
                else if(!rs.getString(2).equals(b))
                {
                    JOptionPane.showMessageDialog(null, "Password Not Match");
                    pwd.setText("");
                    cpwd.setText("");
                }

            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid User Name");
            clear();
        }

    } 
    catch (Exception ex) 
    {
         ex.printStackTrace();
    }

  }                                      

    private void eidKeyPressed(java.awt.event.KeyEvent evt) 
    {                               
        if (evt.getKeyCode() == 10) 
        {
            if (eid.getText().isEmpty()) 
            {
            //ms.massage("Enter User Name");
                eid.grabFocus();
            }
            ulevel.grabFocus();
        }
    }                              

    private void ulevelKeyPressed(java.awt.event.KeyEvent evt) 
    {                                  
        if (evt.getKeyCode() == 10) 
        {
            pwd.grabFocus();
        }
    }                                 

    private void pwdKeyPressed(java.awt.event.KeyEvent evt) 
    {                               
        if (evt.getKeyCode() == 10) 
        {
            if (pwd.getText().isEmpty()) 
            {
                JOptionPane.showMessageDialog(null,"Enter Password");
            } 
            else 
            {
                cpwd.grabFocus();
            }
        }
    }                              

    private void cpwdKeyPressed(java.awt.event.KeyEvent evt) 
    {                                
        if (evt.getKeyCode() == 10) 
        {
            String pass = new String(pwd.getPassword());
            System.out.println(pass);
            String pas1 = new String(cpwd.getPassword());
            System.out.println(pas1);
            if (pass.equals(pas1)) 
            {
                save.grabFocus();

            } 
            else 
            {
            JOptionPane.showMessageDialog(null,"Password Not Match");

            pwd.grabFocus();
            pwd.setText("");
            cpwd.setText("");
        }
    }
 }                               
    private void pwdActionPerformed(java.awt.event.ActionEvent evt) 
    {}                                   
 public void clear() 
 {
     eid.setText("");
     ulevel.setSelectedItem("Admin");
     pwd.setText("");
     cpwd.setText("");
 }

    public static void main(String args[]) 
    {
        try 
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
            {
                if ("Nimbus".equals(info.getName())) 
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch (ClassNotFoundException ex) 
        {
            java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (InstantiationException ex) 
        {
            java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) 
        {
            java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (javax.swing.UnsupportedLookAndFeelException ex) 
        {
            java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new User().setVisible(true);
            }
        });
    }         
    private javax.swing.JPasswordField cpwd;
    private javax.swing.JButton delete;
    private javax.swing.JTextField eid;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField pwd;
    private javax.swing.JButton save;
    private javax.swing.JButton search;
    private javax.swing.JComboBox<String> ulevel;
    private javax.swing.JButton update;
}

Through the above code, I was trying to run the User Information acquiring form named as "userregistry" a swing application, where a text field named "eid" gets the user name,combo box name "ulevel" gives the user type (Admin/user),and a password field named "pwd" to give the password, another password field named "cpwd" to confirm the password. There are 4 buttons respectively named as save,search,update,delete to perform the mentioned actions.(codes for search and update are not here though).

I have the SQL table created with this syntax

mysql> create table user(eid varchar(45) primary key, ulevel int, pwd varchar(45
), cpwd varchar(45));

My problem is this-: When I enter the details and click the save button netbeans says

java.sql.SQLException: Column count doesn't match value count at row 101 User Adding

What could be the reason for this?

I tried 01 also with

catch (Exception e)
{ 
    System.out.println(e+"01 User adding");
}

in that particular place where it gives me the message

java.sql.SQLException: Column count doesn't match value count at row 101 User Adding
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Jay
  • 43
  • 8

1 Answers1

3

You get this error because the number of column that you use in your query is more that your table, your table contain 4 columns eid, ulevel, pwd, cpwd :

 user(eid varchar(45) primary key, ulevel int, pwd varchar(45), cpwd varchar(45))

And you use a Query that take 5 columns eid.getText(), s, ulevel.getSelectedItem(), pwd.getText(), cpwd.getText():

j.putData("insert into user values ('" + eid.getText() + "','" + s + "','" +  
           ulevel.getSelectedItem() + "','"+pwd.getText()+"','"+cpwd.getText()+"')");

So fix this and every thing will be good.

Note

Your way can get Syntax errors or cause SQL Injection, So instead to use Statement use PrepapredStatement its more help full and more secure.

Good luck.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Thank you very much YCF_L :) Let me try that. Thanks again. Best regards! – Jay Feb 21 '17 at 08:33
  • Done! :) Thanks again buddy... Have a great day ahead! – Jay Feb 21 '17 at 09:18
  • 1
    I am really sorry I forgot that. I think I have accepted the answer by clicking on the upper side arrow next to answer. (I am new to stack overflow community) – Jay Feb 21 '17 at 10:14