私は.Thisは私がこれまで持っているものである私は、テーブルを更新するために、すべての私のアプリケーション全体で使用することができますמ更新クエリ(インベントリ)を思い付くしようとしています:JavaのアップデートのPreparedStatement
public void updateInventory(Inventory inventory){
PreparedStatement ps=null;
try {
String query =
"UPDATE "+TableName.INVENTORY +" "+
"SET quantity=IFNULL(?, quantity), full_stock=IFNULL(?, full_stock), reorder_level=IFNULL(?, reorder_level), selling_price=IFNULL(?, selling_price), delete_status=IFNULL(?, delete_status), product_id=IFNULL(?, product_id) "+
"WHERE id = ? OR product_id=IFNULL(?, product_id) ";
connection = hikariDS.getConnection();
ps = connection.prepareStatement(query);
ps.setFloat(1,inventory.getQuantity());
ps.setFloat(2,inventory.getFullStock());
ps.setFloat(3,inventory.getReorderLevel());
ps.setFloat(4,inventory.getPrice());
ps.setInt(5, inventory.getDeleteStatus());
ps.setInt(6, inventory.getProdId());
ps.setInt(7, inventory.getId());
ps.setInt(8, inventory.getProdId());
ps.executeUpdate();
} catch(SQLException e){ e.printStackTrace();}
finally{
if(connection != null){
try {connection.close();}
catch (SQLException ex) {logger.log(Level.SEVERE, null, ex);}
}
if(ps != null){
try { ps.close();}
catch (SQLException ex) { logger.log(Level.SEVERE, null, ex);}
}
}
}
上記のクエリオブジェクト内の新しい値で列を更新することが想定されていますが、設定されていない場合は、列の既存の値を入力するだけです。
問題がある:インベントリオブジェクトから取得した値がヌルであるか、または0の値ならば、それは既存の値で更新されません。
どのようにインベントリオブジェクトは '0'することができます:null以外ゲッターについては
は、IF()を使いますか? –
このリンクを試してみてください。.. http://stackoverflow.com/questions/32018418/update-columns-if-input-values-are-not-null-otherwise-ignore-and-keep-the-existi –
ないオブジェクト自体しかし、属性。例えば私は、価格だけを更新したいが、数量が設定されていないので、私は、価格属性に値が含まれますが、量はゼロ –