package demo;
import java.awt.Color;
import java.awt.Font;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.TableModel;
import com.mysql.jdbc.PreparedStatement;
import net.proteanit.sql.DbUtils;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class ProductFrame extends javax.swing.JFrame {
private Connection con = null;
Statement stmt = null;
ResultSet rs = null;
@SuppressWarnings("rawtypes")
JComboBox CategorycomboBox = new JComboBox();
@SuppressWarnings("unchecked")
public ProductFrame() {
DBConnect config = new DBConnect();
con = config.getConnectionDB();
initComponents();
showResult();
getContentPane().setBackground(new Color(10, 210, 230));
setBounds(150, 50, 1100, 600);
categoryMap = db.listCategory();
System.out.println("Test point 3" + categoryMap);
for (String value : categoryMap.keySet()) {
// System.out.println("For"+catList.get(i));
CategorycomboBox.addItem(value);
}
ListSelectionModel selectionModel = ProductTable.getSelectionModel();
selectionModel.addListSelectionListener(ProductTable);
ProductTable.getColumnModel().getColumn(0).setMaxWidth(40);
ProductTable.getColumnModel().getColumn(4).setMaxWidth(350);
ProductTable.getColumnModel().getColumn(5).setMaxWidth(50);
ProductTable.getColumnModel().getColumn(6).setMaxWidth(50);
ProductTable.getColumnModel().getColumn(1).setMinWidth(0);
ProductTable.getColumnModel().getColumn(1).setMaxWidth(0);
}
public void valueChanged(ListSelectionEvent event) {
if (event.getSource() == ProductTable.getSelectionModel()
&& event.getValueIsAdjusting()) {
TableModel model = ProductTable.getModel();
int row = (int) model.getValueAt(ProductTable.getSelectedRow(), 0);
System.out.println("ROW IS: " + row);
}
}
public void showResult() {
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("call listProduct");
ProductTable.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
}
}
private void initComponents() {
ProductPanel = new javax.swing.JPanel();
ProductPanel.setBackground(SystemColor.inactiveCaptionBorder);
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
productnameTextField = new javax.swing.JTextField();
submitButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
ProductTable = new javax.swing.JTable();
deleteButton = new javax.swing.JButton();
updateButton = new javax.swing.JButton();
descriptionTextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
jLabel1.setText("Product Name");
jLabel3.setText("Description");
submitButton.setText("Submit");
submitButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
ProductTable.setModel(new DefaultTableModel(
new Object[][] {
{new Long(1L), new Integer(23), "T-Shirt", "Nike-19XL", "Colour: Red, Round Neck", "Clothing", null, new Integer(100)},
{new Long(2L), new Integer(37), "", "Abc", "", "Clothing", null, new Integer(100)},
{new Long(3L), new Integer(35), "Abc", "Abc", "asdf", "Fooding", null, new Integer(100)},
{new Long(4L), new Integer(20), "Abc", "Abc", "", "Stationary", null, new Integer(100)},
{new Long(5L), new Integer(36), "Abc", "Abc", "", "laptops", null, new Integer(100)},
},
new String[] {
"SN", "ID", "Name", "ModelNo", "Description", "Category", "Price", "Quantity"
}
));
ProductTable.getColumnModel().getColumn(1).setPreferredWidth(0);
ProductTable.getColumnModel().getColumn(1).setMinWidth(0);
ProductTable.getColumnModel().getColumn(1).setMaxWidth(0);
ProductTable.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
ProductTableMouseClicked(evt);
}
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
ProductTableMouseEntered(evt);
}
@Override
public void mousePressed(java.awt.event.MouseEvent evt) {
ProductTableMousePressed(evt);
}
});
ProductTable
.addContainerListener(new java.awt.event.ContainerAdapter() {
@Override
public void componentAdded(java.awt.event.ContainerEvent evt) {
ProductTableComponentAdded(evt);
}
@Override
public void componentRemoved(
java.awt.event.ContainerEvent evt) {
ProductTableComponentRemoved(evt);
}
});
ProductTable
.addInputMethodListener(new java.awt.event.InputMethodListener() {
@Override
public void caretPositionChanged(
java.awt.event.InputMethodEvent evt) {
}
@Override
public void inputMethodTextChanged(
java.awt.event.InputMethodEvent evt) {
ProductTableInputMethodTextChanged(evt);
}
});
ProductTable.addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyPressed(java.awt.event.KeyEvent evt) {
ProductTableKeyPressed(evt);
}
@Override
public void keyReleased(java.awt.event.KeyEvent evt) {
ProductTableKeyReleased(evt);
}
});
jScrollPane1.setViewportView(ProductTable);
deleteButton.setText("Delete");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
updateButton.setText("Update");
updateButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateButtonActionPerformed(evt);
}
});
lblModelNo = new JLabel("Model No");
modelTextField = new JTextField();
modelTextField.setColumns(10);
JLabel lblPrice = new JLabel("Price");
priceTextField = new JTextField();
priceTextField.setColumns(10);
JLabel lblQuantity = new JLabel("Quantity");
quantityTextField = new JTextField();
quantityTextField.setColumns(10);
descriptionTextArea_1 = new JTextArea();
JLabel lblCategory = new JLabel("Category");
javax.swing.GroupLayout ProductPanelLayout = new javax.swing.GroupLayout(
ProductPanel);
ProductPanelLayout.setHorizontalGroup(
ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addGroup(ProductPanelLayout.createSequentialGroup()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(ProductPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
.addGap(53)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.TRAILING, false)
.addGroup(ProductPanelLayout.createSequentialGroup()
.addComponent(descriptionTextArea_1)
.addGap(4))
.addComponent(productnameTextField, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)))
.addGroup(ProductPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblQuantity, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
.addComponent(lblCategory))
.addPreferredGap(ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(CategorycomboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(quantityTextField, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)))
.addGroup(ProductPanelLayout.createSequentialGroup()
.addGap(51)
.addComponent(updateButton)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(deleteButton)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(submitButton))
.addGroup(ProductPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(lblModelNo)
.addComponent(lblPrice, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
.addGap(81)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.TRAILING, false)
.addComponent(modelTextField, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
.addComponent(priceTextField))))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 760, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
ProductPanelLayout.setVerticalGroup(
ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addGroup(ProductPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
.addGroup(ProductPanelLayout.createSequentialGroup()
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(productnameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(descriptionTextArea_1, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(modelTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblModelNo))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblPrice, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addComponent(priceTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblQuantity, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addComponent(quantityTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.LEADING)
.addComponent(CategorycomboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblCategory))
.addGap(24)
.addGroup(ProductPanelLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(updateButton)
.addComponent(deleteButton)
.addComponent(submitButton))))
.addContainerGap())
);
ProductPanel.setLayout(ProductPanelLayout);
JLabel lblCategoryDashboard = new JLabel("Product Dashboard");
lblCategoryDashboard.setForeground(Color.WHITE);
lblCategoryDashboard.setFont(new Font("Verdana", Font.BOLD, 20));
lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("D:\\eclipse\\lab\\FppDemoProject\\producticon.png"));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(272)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblCategoryDashboard, GroupLayout.PREFERRED_SIZE, 333, GroupLayout.PREFERRED_SIZE)
.addContainerGap(536, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(ProductPanel, GroupLayout.DEFAULT_SIZE, 1171, Short.MAX_VALUE)
.addGap(38))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblCategoryDashboard, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(ProductPanel, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addContainerGap())
);
getContentPane().setLayout(layout);
pack();
}
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String query = "INSERT INTO product(Name, ModelNo, Price, Qty,description,categoryID) VALUES(?,?,?,?,?,?)";
System.out.println("CategorycomboBox : "+ CategorycomboBox.getSelectedItem());
System.out.println("categoryMap : "+ categoryMap.get(CategorycomboBox.getSelectedItem()));
PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);
ps.setString(1, productnameTextField.getText());
ps.setString(2, modelTextField.getText());
ps.setString(3, priceTextField.getText());
ps.setString(4, quantityTextField.getText());
ps.setString(5, descriptionTextArea_1.getText());
ps.setInt(6, categoryMap.get(CategorycomboBox.getSelectedItem()));
System.out.println(ps);
try{
ps.executeUpdate();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "Please insert price and quantity properly",null, 0);
}
showResult();
// conn.close();
} catch (SQLException e) {
System.out.println("Error while adding value");
}
productnameTextField.setText("");
}// GEN-LAST:event_submitButtonActionPerformed
private void ProductTableInputMethodTextChanged(
java.awt.event.InputMethodEvent evt) {// GEN-FIRST:event_ProductTableInputMethodTextChanged
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableInputMethodTextChanged
private void ProductTableKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_ProductTableKeyPressed
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableKeyPressed
private void ProductTableMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_ProductTableMouseClicked
//
int selectedRow = ProductTable.getSelectedRow();
selectedRow = ProductTable.convertRowIndexToModel(selectedRow);
// int val1 = (int) ProductTable.getModel().getValueAt(selectedRow, 0);
}// GEN-LAST:event_ProductTableMouseClicked
private void ProductTableComponentAdded(java.awt.event.ContainerEvent evt) {// GEN-FIRST:event_ProductTableComponentAdded
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableComponentAdded
private void ProductTableComponentRemoved(java.awt.event.ContainerEvent evt) {// GEN-FIRST:event_ProductTableComponentRemoved
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableComponentRemoved
private void ProductTableMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_ProductTableMouseEntered
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableMouseEntered
private void ProductTableKeyReleased(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_ProductTableKeyReleased
// TODO add your handling code here:
}// GEN-LAST:event_ProductTableKeyReleased
int updateId;
private void ProductTableMousePressed(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_ProductTableMousePressed
// TODO add your handling code here:
System.out.println("MOUSE PRESSED");
int selectedRow = ProductTable.getSelectedRow();
selectedRow = ProductTable.convertRowIndexToModel(selectedRow);
updateId = (int) ProductTable.getModel().getValueAt(selectedRow, 1);
System.out.println("VAR >>>>>" + updateId);
getUpdateDetails(updateId);
//submitButton.hide();
}// GEN-LAST:event_ProductTableMousePressed
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_updateButtonActionPerformed
// TODO add your handling code here:
isUpdate(updateId);
showResult();
}// GEN-LAST:event_updateButtonActionPerformed
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_deleteButtonActionPerformed
// TODO add your handling code here:
int confirm = JOptionPane.showConfirmDialog(null,
"Do you want delete. ?", null, WIDTH);
if (confirm == 0) {
System.out.println("DELETED");
isDeleted();
showResult();
} else {
System.out.println("NOT DEL");
}
}// GEN-LAST:event_deleteButtonActionPerformed
public boolean isUpdate(int id) {
try {
stmt = con.createStatement();
String query = "UPDATE product SET Name='"
+ productnameTextField.getText() + "',description='"
+ descriptionTextArea_1.getText() + "',ModelNo='"
+ modelTextField.getText() + "',price='"
+ priceTextField.getText() + "',qty='"
+ quantityTextField.getText() + "' WHERE id='" + id + "'";
System.out.println(query);
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(null, "Update Successfully !",null ,JOptionPane.DEFAULT_OPTION);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Problem while updating.",
null, 0);
return false;
}
return true;
}
public boolean isDeleted() {
try {
stmt = con.createStatement();
String query = "DELETE FROM product WHERE id='" + updateId + "'";
stmt.executeUpdate(query);
} catch (Exception e) {
return false;
}
return true;
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProductFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProductFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProductFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProductFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ProductFrame().setVisible(true);
}
});
}
public void getUpdateDetails(int id) {
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM product p inner Join category c on c.Id=p.categoryId where p.id='"
+ id + "'");
while (rs.next()) {
productnameTextField.setText(rs.getString("Name"));
descriptionTextArea_1.setText(rs.getString("description"));
modelTextField.setText(rs.getString("ModelNo"));
priceTextField.setText(rs.getString("Price"));
quantityTextField.setText(rs.getString("Qty"));
CategorycomboBox.setSelectedItem(rs.getString("categoryName"));
}
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
private javax.swing.JButton deleteButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel ProductPanel;
private javax.swing.JTable ProductTable;
private javax.swing.JButton submitButton;
private javax.swing.JButton updateButton;
private javax.swing.JTextField productnameTextField;
@SuppressWarnings("unused")
private javax.swing.JTextArea descriptionTextArea;
private JTextArea descriptionTextArea_1;
private JLabel lblModelNo;
private JTextField modelTextField;
private JTextField priceTextField;
private JTextField quantityTextField;
FileInputStream fis;
private Map<String, Integer> categoryMap = new HashMap<>();
DBConnect db = new DBConnect();
private JLabel lblNewLabel;
}