2015-09-26 32 views
2

我正在使用Netbeans來製作java程序,我想將數據插入到我的「data supplier」表中。我無法發佈我的JFrame圖片,因爲我的聲望不夠。java應用程序不能將空值插入到mysql數據庫中

我設置"Kode Supplier「爲PRIMARY_KEYNOT_NULL,並讓其餘的是NULL

在下面的代碼,telpFieldhpField會顯示一個錯誤,如果我沒有在其中鍵入任何的文本框

是否有可能,因爲它是INT

這是我的代碼:?

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){ 

    } 
} 

這是我的表(使用MySQL社區服務器數據庫,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) 

+0

什麼是確切的錯誤信息? – Rahul

+0

是的,這可能是因爲它是整數類型。你不要在整數字段中使用'setString'。你爲什麼不添加你的表定義? – RealSkeptic

+0

@Rahul錯誤消息是:「java.sql.SQLException:錯誤的整數值:第1行'列'NoTelp'」 – Dante26

回答

2

是的,這是因爲你Kode SupplierNoTelpNoHP列是整數列。對於整數列,您應該使用setInt方法而不是setString

setInt方法只接受字段值的原始int。因此,您需要做的第一件事是將該字段的String值轉換爲int。這與像一個語句來完成:

int telpVal = Integer.parseInt(telpField.getText()); 

但是,這意味着你必須決定如何在以下情況下做到:

  • 的用戶輸入的GUI字段的值不是整數,如ABC1.2123456789123456789。如果發生這種情況,那麼我給出的陳述將拋出一個NumberFormatException

    您可以決定顯示錯誤消息,並在發生這種情況時不調用insert語句。或者您可能決定插入NULL。或者您可以決定插入一個默認值,如0

  • 用戶在GUI字段中沒有輸入任何值 - 它是一個空字符串。請注意,空字符串和null之間存在差異。你可以決定處理這個案例,就像處理前一個案例一樣。或者你可以決定分開處理。

假設你決定:

  • 如果用戶輸入了非法號碼,就顯示錯誤消息,並且不會插入該行。
  • 如果用戶沒有輸入值並將該字段留空,則需要插入空值。

然後,你需要處理這樣的:

String fieldName; 

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 

    // Kode supplier is integer, but is not allowed to be null 
    // so don't handle an empty field case, just let parseInt 
    // throw the exception. 

    fieldName = "Kode Supplier"; 
    pst.setInt(1, Integer.parseInt(codeField.getText())); 

    pst.setString(2, nameField.getText()); 
    pst.setString(3, cpField.getText()); 
    pst.setString(4, addressField.getText()); 

    // Handle the NoTelp field - if empty, insert null. If not, 
    // parse the number. Handle illegal number values in catch. 

    fieldName = "NoTelp"; 
    if (telpField.getText().isEmpty()) { 
     pst.setNull(5, Types.INTEGER); 
    } else { 
     pst.setInt(5, Integer.parseInt(telpField.getText()); 
    } 

    // Handle the NoHP field 

    fieldName = "NoHP"; 
    if (hpField.getText().isEmpty()) { 
     pst.setNull(6, Types.INTEGER); 
    } else { 
     pst.setInt(6, Integer.parseInt(hpField.getText()); 
    } 

    pst.setString(7, bankField.getText()); 
    pst.setString(8, acField.getText()); 
    pst.setString(9,anField.getText()); 
    pst.setString(10, ketField.getText()); 

    pst.executeUpdate(); 
    JOptionPane.showMessageDialog(null, "Tabel Telah Di Update"); 
} 
catch (NumberFormatException nfe) { 
    // Display error to the user 
    JOptionPane.showMessageDialog(null, "Invalid number in field " + fieldName) 
} 
catch(SQLException e){ 
    JOptionPane.showMessageDialog(null, "Data Invalid"); 
} 

注意

  • 我處理Kode Supplier不同於NoTelpNoHP,因爲它不允許爲空。如果該字段爲空,則NumberFormatException將從parseInt中拋出,並將轉至catch部分。
  • 我保留了一個fieldName變量,我在嘗試每個parseInt之前設置了這個變量。如果拋出異常,我們可以使用它來顯示對話框中發生錯誤的特定字段。你可以做其他的事情,比如保持你正在處理的JTextField,以及catch突出顯示它,並給予焦點。
  • 當您使用setNull時,必須傳遞該字段的類型作爲第二個參數。所有類型都在java.sql.Types。所以請記住import java.sql.Types。不要使用catch (Exception e)。它太廣泛了。在這種情況下,我們預計只有NumberFormatExceptionSQLException。如果發生任何其他異常,尤其是運行時異常,則需要了解它並查看堆棧跟蹤。如果您有catch (Exception e),您只會看到一個對話框,顯示「數據無效」並且沒有幫助。 「抓住所有」是不好的。
+0

我看到..你需要解析INT類型才能存儲,謝謝你的catch(錯誤)的建議,非常感謝你的非常詳細的信息。 – Dante26

相關問題