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.