开发者社区> 问答> 正文

Column count doesn't match value错误

public class Regist extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField text_confirm;
private JTextField text_usr;
private JTextField text_pwd;
private JButton btn_save,btn_cancel;
private JLabel user,passwd,confirm;
private Connection conn;
private Statement statement;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Regist frame = new Regist();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Regist() {
    super("注册新用户");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    user = new JLabel("用  户  名");
    user.setBounds(57, 36, 54, 15);
    contentPane.add(user);

    passwd = new JLabel("密       码");
    passwd.setBounds(57, 76, 54, 15);
    contentPane.add(passwd);

    confirm = new JLabel("确认密码");
    confirm.setBounds(57, 121, 54, 15);
    contentPane.add(confirm);

    text_usr = new JTextField();
    text_usr.setBounds(141, 33, 155, 21);
    contentPane.add(text_usr);
    text_usr.setColumns(10);

    text_pwd = new JTextField();
    text_pwd.setBounds(141, 73, 155, 21);
    contentPane.add(text_pwd);
    text_pwd.setColumns(10);

    text_confirm = new JTextField();
    text_confirm.setBounds(141, 118, 155, 21);
    contentPane.add(text_confirm);
    text_confirm.setColumns(10);

    btn_save = new JButton("确定");
    btn_save.setBounds(79, 171, 93, 23);
    contentPane.add(btn_save);
    this.btn_save.addActionListener(this);

    btn_cancel = new JButton("取消");
    btn_cancel.setBounds(256, 171, 93, 23);
    contentPane.add(btn_cancel);
    this.btn_cancel.addActionListener(this);

}
public Connection getConnection() throws SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sign","root","514519");
    return conn;
}
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btn_save){
        if(text_usr.getText().isEmpty()&&text_pwd.getText().isEmpty())
            JOptionPane.showMessageDialog(this, "请填写用户名和密码");
        String sql="insert into signs values ('"+text_usr.getText()+","+text_pwd.getText()+"')";
        try {
               conn=getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            statement=conn.createStatement();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            statement.executeUpdate(sql);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            statement.close();
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }   
    if(e.getSource()==btn_cancel){

    }
}
}

运行的时候说Column count doesn't match value count at row 1
我数据库中的user 和password都是varchar类型
还有就是这行代码中的引号问题 String sql="insert into signs values ('"+text_usr.getText()+","+text_pwd.getText()+"')";

展开
收起
蛮大人123 2016-03-25 16:30:15 3633 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    你可以打印下你的sql,就能发现你输出的sql语句的引号是不全的,例如我都输入a后,打印的sql:insert into signs values ('a,a')
    代码问题,大意导致的引号不全,修正如下:
    String sql = "insert into signs values ('" + text_usr.getText() + "','" + text_pwd.getText() + "')";
    再试试,就能打印出正确的sql了。

    2019-07-17 19:14:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Why you should care about data layout in the file system 立即下载
Froma single droplet toafull b 立即下载
Semantic Search--Fast Results from Large,Foreign Language Corpora 立即下载