将java表单中的数据插入到表personal中的MySql数据库详细信息中
但我得到的错误列计数与第1行的值计数不匹配
我使用的是Netbeans 6.9和MySql 5.6
我声明ID为int,主键为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.
最佳答案
用以下语句更正您的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
istead ofStatement
并使用占位符?
。这个解决方案更干净,更安全!(
SQL injection
)和更多的人类可读性。关于java - 列数与第1行的值数不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14925463/