UpdateQuantity() – Updates the quantity of a Part in stock by increasing or decr
ID: 3834013 • Letter: U
Question
UpdateQuantity() – Updates the quantity of a Part in stock by increasing or decreasing quantity
on hand.
// Update Part Quantity
public static int UpdateQuantity(int qtyChange, Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Declare and set sql statement string to update Quantity in DB
// for Part p using ID. New Quantity reflect the change in Quantity
// in qtyChange
// Execute update - set records to count affected
// Return number of records affected
return records;
}
Explanation / Answer
In java we use JDBC to connect with database. Steps to connect to database using JDBC are specified below.
Assuming that your database name is tblParts and column names are fldID and fldQuantity code for updating quantity is given below.
Enter your database driver class name, url , username and password.
public static int UpdateQuantity(int qtyChange, Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
//step1 load the driver class
Class.forName("Yur database driver");
//step2 create the connection object
Connection con=DriverManager.getConnection("your database url","username","password");
//step3 create the statement object
Statement stmt=con.createStatement();
String sql = "UPDATE Registration tblParts SET fldQuantity = "+qtyChange+" WHERE fldID="+p;
records= stmt.executeUpdate(sql);
return records;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.