2017-05-21 10 views
0

imはインベントリソフトウェアを実行しています。私はIDを導入し、更新するフィールドを選択できるフレームを変更します。フィールドはJtextFieldであり、 checkBoxesによって。 ソフトウェアにはスペイン語の単語がありますので、お気軽にお気軽にご相談ください。Jtextfieldsから一部のフィールドのみを更新する準備済みの文

This is the frame

私は、PreparedStatementのコードを持っているが、それはすべてのフィールドを更新します。チェックフィールドからの更新のみを行うコードがあるかどうかはわかりません。

public static void modificar(int id,String nombre,String marca,String descripcion,String tamaño,String departamento,float precio) throws SQLException 
     { 
      Connection conn = null; 
    PreparedStatement prepStmt = null; 
    ResultSet rs = null; 
    try { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     String connectionUrl = "jdbc:mysql://localhost:3306/inventario"; 
     String connectionUser = "root"; 
     String connectionPassword = "root"; 
     conn = (Connection) DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword); 
     String sqlStmt = "UPDATE Producto SET nombre=?, marca=?,descripcion=?,tamaño=?,departamento=?,precio=? WHERE idProducto='"+id+"'"; 
     prepStmt = conn.prepareStatement(sqlStmt); 

        if(!nombre.isEmpty()){ 
     prepStmt.setString(1, nombre); 
        } 
        if(!marca.isEmpty()){ 
     prepStmt.setString(2, marca); 
        } 
        if(!descripcion.isEmpty()){ 
        prepStmt.setString(3, descripcion); 
        } 
        if(!nombre.isEmpty()){ 
        prepStmt.setString(4, tamaño); 
        } 
        if(!departamento.isEmpty()){ 
     prepStmt.setString(5, departamento); 
        } 
        if(precio!=0){ 
        prepStmt.setFloat(6, precio); 
        } 
     prepStmt.executeUpdate(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

考えられるすべての組み合わせに対してメソッドを書き込むことが考えられます。あなたが私を助けてくれることを願っています。

+0

可能性のある重複した[更新列入力値がnullでない場合、そうでない場合は、データベース内の列の既存の値を無視しておきます](http://stackoverflow.com/questions/32018418/update-columns- if-input-values-not-null-otherwise-ignore-and-keep-the-existi)は、 – Sam

答えて

0

確認フィールドからの更新のみを行うコードがあるかどうかわかりません。

テキストフィールドではなく、チェックボックスの状態を確認する必要があります。何かのように:の

if (nombreCheckBox.isSelected()) 
    prepStmt.setString(1, nombre); 
関連する問題