列数与第1行的值计数不匹配

时间:2013-02-17 20:01:56

标签: java mysql

将数据从java表单插入MySql数据库详细信息表个人

但我收到错误列数与第1行的值计数不匹配

我正在使用Netbeans 6.9,MySql 5.6

我已将ID声明为int,主键,Phone和mobile为int,其他所有为char。

 import java.sql.*;
 import javax.swing.*;

  public class Personal_Details extends javax.swing.JFrame{

   Connection conn;
   String url;
   Statement stmt;
   ResultSet rs;

  public javax.swing.table.DefaultTableModel tableview;
    public Personal_Details() {
    initComponents();
          tableview= new javax.swing.table.DefaultTableModel();
          jTable1 = new javax.swing.JTable(tableview);
           tableview.addColumn("ID");
           tableview.addColumn("Name");
           tableview.addColumn("Address");
           tableview.addColumn("Gender");
           tableview.addColumn("Education");
           tableview.addColumn("Phone");
           tableview.addColumn("Mobie");
           tableview.addColumn("DOB");
           jScrollPane1.setViewportView(jTable1);
      try{
      url = "jdbc:mysql:///Details";
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(url,"root","anand");
      stmt = conn.createStatement();

        }catch(Exception e){
     JOptionPane.showMessageDialog(null,e);
    }
    }

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
    {
    try{
    stmt.executeUpdate("insert into
    personal(ID,Name,Address,Gender,Education,Phone,Mobile,DOB) values
    ("+tfIDNo.getText()+
         ",'"+tfName.getText()+"','"+tfAddress.getText()+"','"+tfGender.getText()+"','"
         +                                                                        
      "','"+tfEducation.getText()+"',"+tfPhone.getText()+","+tfMobile.getText()+",'"+
     tfDob.getText()+"')");


   rs = stmt.executeQuery("select * from personal where ID="+tfIDNo.getText()+" and   
   Name='"+tfName.getText()+"' and Addess='"+tfAddress.getText()+"'"
         + "' and Gender='"+tfGender.getText()+"' and  
    Education='"+tfEducation.getText()+"' and phone="+tfPhone.getText()+" "
         + " and Mobile="+tfMobile.getText()+" and DOB='"+tfDob.getText()+"'");
    }catch(Exception e){
   JOptionPane.showMessageDialog(null,e);
   }
   }
   }

   public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Personal_Details().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton btnClear;
private javax.swing.JButton btnClose;
private javax.swing.JButton btnSubmit;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JLabel lblAddress;
private javax.swing.JLabel lblDob;
private javax.swing.JLabel lblDobFormat;
private javax.swing.JLabel lblEducation;
private javax.swing.JLabel lblEnterYourDetails;
private javax.swing.JLabel lblGender;
private javax.swing.JLabel lblIDNo;
private javax.swing.JLabel lblMobile;
private javax.swing.JLabel lblName;
private javax.swing.JLabel lblPhone;
private javax.swing.JTextField tfAddress;
private javax.swing.JTextField tfDob;
private javax.swing.JTextField tfEducation;
private javax.swing.JTextField tfGender;
private javax.swing.JTextField tfIDNo;
private javax.swing.JTextField tfMobile;
private javax.swing.JTextField tfName;
private javax.swing.JTextField tfPhone;
}


  Database - ID, Name, Address, Gender, Education, Phone, Mobile, DOB.

1 个答案:

答案 0 :(得分:1)

使用以下内容纠正您的INSERT声明:

String s = "insert into 
            personal(ID, Name,Address, Gender, Education, Phone, Mobile, DOB) 
            values (" + "'" + t.getText() + "','" + t.getText() + "','" 
            + t.getText() + "','" + t.getText() + "','" + t.getText() + "','"
            + t.getText() + "','" + t.getText() + "','" + t.getText() + "')";

t.getText()替换为您的PreparedStatement,现在它应该有效。


注意:但我建议您使用Statement而不是?并使用占位符 {{1}} 。 这个解决方案更干净,更安全!(SQL injection)更具人性化。