I'm making a java program using Netbeans, I want to insert data into my "data supplier" table. I cannot post my JFrame picture as my reputation is not enough.
I've set "Kode Supplier" as PRIMARY_KEY and NOT_NULL, and allow the rest to be NULL
In the code below, telpField and hpField will show an error if I didn't type anything in it's textbox
Is it possible because it is INT type?
This is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
String sql = "INSERT INTO datasupplier (`Kode Supplier`, `Nama Supplier`, `Contact Person`,"
+ " `Alamat`, `NoTelp`, `NoHP`, `Bank Account`, `A/C Info`, `A.N.`, `Keterangan`)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pst = conn.prepareStatement(sql);
//Get value from the textboxes
pst.setString(1, codeField.getText());
pst.setString(2, nameField.getText());
pst.setString(3, cpField.getText());
pst.setString(4, addressField.getText());
pst.setString(5, telpField.getText());
pst.setString(6, hpField.getText());
pst.setString(7, bankField.getText());
pst.setString(8, acField.getText());
pst.setString(9,anField.getText());
pst.setString(10, ketField.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Tabel Telah Di Update");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Data Invalid");
}
DataSupplierTable();
}
//Set JComboBox First Diplayed Item
private void setTableCombo(){
tableCombo.setSelectedItem("Data Supplier");
}
//Bind the table and databarang.datasupplier
private void DataSupplierTable(){
String sql = "SELECT * FROM datasupplier";
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
supplierTable.setModel(DbUtils.resultSetToTableModel(rs));
supplierTable.isCellEditable(0,0);
}catch(Exception e){
}
}
This is my table (using MySQL Community Server Database, InnoDB)
Kode Supplier INT(30) PRIMARY_KEY NOT_NULL, Nama Supplier CHAR(45), Contact Person VARCHAR(20), Alamat VARCHAR(45), NoTelp INT(30), NoHP INT(30), Bank Account CHAR(30), A/C Info VARCHAR(45), A.N. CHAR(45), Keterangan VARCHAR(100)
setStringfor integer fields. Why don't you add your table definition?