private void read_actionPerformed(ActionEvent e) {
String partNumber = txtPartNumber.getText();
try{
String queryString = "select partNumber, name, description, price from Inventory" +
" where Inventory.partNumber = " + partNumber ;
ResultSet rset = stmt.executeQuery(queryString);
if (rset.next()){
String partNumber1 = rset.getString(1);
String name = rset.getString(2);
String description = rset.getString(3);
String price = rset.getString(4);
txtName.setText(name);
txtDescription.setText(description);
txtPrice.setText(price);
} else { JOptionPane.showMessageDialog(null, "Inventory item not found");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
private void update_actionPerformed(ActionEvent e) {
String partNumber = txtPartNumber.getText();
String name = txtName.getText();
String description = txtDescription.getText();
String price = txtPrice.getText();
txtName.setText("");
txtDescription.setText("");
txtPrice.setText("");
txtPartNumber.setText(partNumber);
try{
String updateString = "update Inventory "+
" name= '"+ name +"',"+
" description= '"+description+"',"+ "price= "+price+
" where Inventory.partNumber = " + partNumber;
ResultSet rset = stmt.executeQuery(toString());
//Results
if(rset.next()) {
int resultInsert = stmt.executeUpdate(updateString);
}
else {
JOptionPane.showMessageDialog(null,"Update NOT executed, inventory item not found");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
private void delete_actionPerformed(ActionEvent e) {
String partNumber = txtPartNumber.getText();
try {
String deleteString = "delete from Inventory where partNumber= "+partNumber;
//Results
int result = stmt.executeUpdate(deleteString);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
private void insert_actionPerformed(ActionEvent e) {
try{
String partNumber = txtPartNumber.getText();
String name = txtName.getText();
String description = txtDescription.getText();
String price = txtPrice.getText();
String insertString = "insert into Inventory "+
"set partNumber = '"+ partNumber +"', name = '"+ name +"',"+
" description= '"+description+"',"+ "price= "+price;
System.out.println(insertString);
txtName.setText(name);
txtDescription.setText(description);
txtPrice.setText(price);
txtPartNumber.setText(partNumber);
int result = stmt.executeUpdate(toString());
ResultSet rset = stmt.executeQuery
("select * from inventory");
if (rset.next()) {
String partName = rset.getString(1);
String partDescription = rset.getString(2);
String partNumber1 = rset.getString(3);
String partPrice = rset.getString(5);
txtName.setText(name);
txtDescription.setText(description);
txtPrice.setText(price);
txtPartNumber.setText(partNumber);
// Display result in a dialog box
JOptionPane.showMessageDialog(null, "Part#: " + partNumber + " will be inserted with == > " + partDescription +
" " + partNumber + partPrice);
} else {
// Display result in a dialog box
JOptionPane.showMessageDialog(null, "Update NOT executed, inventory item not found");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}