错误表示列数与

时间:2017-02-21 08:06:59

标签: java mysql swing jdbc netbeans

我正试图摆脱这个错误。你介意给我一个参与吗? 以下是我的代码。

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;
}

通过上面的代码,我试图运行名为“userregistry”的用户信息获取表单一个swing应用程序,其中一个名为“eid”的文本字段获取用户名,组合框名称“ulevel”给​​出用户类型(Admin / user)和一个名为“pwd”的密码字段给出密码,另一个名为“cpwd”的密码字段用于确认密码。有4个按钮分别命名为保存,搜索,更新,删除以执行上述操作。(搜索和更新的代码虽然不在这里)。

我使用这种语法

创建了SQL表
mysql> create table user(eid varchar(45) primary key, ulevel int, pwd varchar(45
), cpwd varchar(45));

我的问题是这样的: 当我输入详细信息并单击保存按钮netbeans说

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

这可能是什么原因?

我也用

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

在那个给我信息的特定地方

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

1 个答案:

答案 0 :(得分:3)

您收到此错误的原因是您在查询中使用的列数多于您的表格,您的表格包含4列eid, ulevel, pwd, cpwd

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

您使用的查询需要5列eid.getText(), s, ulevel.getSelectedItem(), pwd.getText(), cpwd.getText()

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

所以解决这个问题,每件事情都会很好。

注意

您的方式可能会出现语法错误或导致SQL Injection ,因此请使用语句 PrepapredStatement ,以获得更全面,更安全的帮助。

祝你好运。