import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class updateDeletePro extends JFrame {
private JButton search = new JButton();
private JButton clear = new JButton();
private JButton update = new JButton();
private JLabel productNamelbl = new JLabel();
private JLabel productIDlbl = new JLabel();
private JLabel productQulitylbl = new JLabel();
private JLabel productDatelbl = new JLabel();
private JLabel productWeightlbl = new JLabel();
private JLabel productBePricelbl = new JLabel();
private JTextField producNametText = new JTextField();
private JTextField SearchText = new JTextField();
private JTextField productTextID = new JTextField();
private JTextField productQtext = new JTextField();
private JTextField productDateText = new JTextField();
private JTextField productPriceText = new JTextField();
private JTextField productWeightText = new JTextField();
private JLabel lblll1 = new JLabel();
private JLabel lblll3 = new JLabel();
private JLabel lblll4 = new JLabel();
Connection conn = null;
Statement state = null;
ResultSet rs=null;
private class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == search) {
SearchP();
}
else if (e.getSource() == clear) {
clear();
}
else if (e.getSource() == update) {
up_date();
}
}
}
private class Closer implements WindowListener {
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
}
}
public updateDeletePro() {
this.setLayout(null);
this.setFont(new Font("Arial", 1, 13));
search.setText("Search");
search.setBounds(new Rectangle(5, 250, 75, 25));
search.setToolTipText("Click to Search by ID");
clear.setText("New");
clear.setBounds(new Rectangle(200, 250, 75, 25));
clear.setToolTipText("Click to Search New One");
update.setText("update");
update.setBounds(new Rectangle(285, 250, 75, 25));
update.setToolTipText("Click to Update");
productNamelbl.setText("Product Name");
productNamelbl.setBounds(new Rectangle(5, 10, 65, 25));
productIDlbl.setText("ID");
productIDlbl.setBounds(new Rectangle(5, 40, 65, 25));
productQulitylbl.setText("Qulity");
productQulitylbl.setBounds(new Rectangle(5, 75, 65, 25));
productDatelbl.setText("Date");
productDatelbl.setBounds(new Rectangle(5, 115, 65, 25));
productWeightlbl.setText("Weight");
productWeightlbl.setBounds(new Rectangle(5, 155, 65, 25));
productBePricelbl.setText("Before Price");
productBePricelbl.setBounds(new Rectangle(5, 190, 75, 25));
producNametText.setBounds(new Rectangle(95, 10, 180, 25));
productTextID.setBounds(new Rectangle(95, 45, 100, 25));
productQtext.setBounds(new Rectangle(95, 80, 100, 25));
SearchText.setBounds(new Rectangle(95, 250, 100, 25));
productDateText.setBounds(new Rectangle(95, 115, 100, 25));
productPriceText.setBounds(new Rectangle(95, 190, 100, 25));
lblll1.setText("By Pounds");
lblll1.setBounds(new Rectangle(205, 185, 85, 30));
lblll1.setFont(new Font("Arial", 3, 12));
productWeightText.setBounds(new Rectangle(95, 150, 100, 25));
lblll3.setText("By Kg");
lblll3.setBounds(new Rectangle(205, 150, 85, 30));
lblll3.setFont(new Font("Arial", 3, 12));
lblll4.setText("Day / Mon /Year");
lblll4.setBounds(new Rectangle(205, 115, 100, 25));
lblll4.setFont(new Font("Arial", 3, 12));
JPanel panel=new JPanel();
panel = (JPanel)getContentPane();
panel.add(lblll4, null);
panel.add(lblll3, null);
panel.add(productWeightText, null);
panel.add(lblll1, null);
panel.add(productPriceText, null);
panel.add(productDateText, null);
panel.add(productQtext, null);
panel.add(productTextID, null);
panel.add(producNametText, null);
panel.add(productBePricelbl, null);
panel.add(productWeightlbl, null);
panel.add(productDatelbl, null);
panel.add(productQulitylbl, null);
panel.add(productIDlbl, null);
panel.add(productNamelbl, null);
panel.add(search, null);
panel.add(SearchText,null);
panel.add(clear,null);
panel.add(update,null);
panel.setBackground(Color.WHITE);
this.setVisible(true);
this.setSize(375,325);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setTitle(":: Find Product ::");
updateDeletePro.ClickListener cl = new updateDeletePro.ClickListener();
search.addActionListener(cl);
clear.addActionListener(cl);
update.addActionListener(cl);
this.addWindowListener(new Closer());
}
public void SearchP() {
String Name =SearchText.getText();
String SQL = "SELECT Product_ID,Product_Name,Product_Qulity,Product_Weight,Product_Date,Product_Price FROM ProductT WHERE Product_Name = '"+Name+"'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:SuperMarket");
state = conn.createStatement();
rs = state.executeQuery(SQL);
while(rs.next()){
producNametText.setText(rs.getString("Product_Name"));
productTextID.setText(rs.getString("Product_ID"));
productQtext.setText(rs.getString("Product_Qulity"));
productDateText.setText(rs.getString("Product_Date"));
productWeightText.setText(rs.getString("Product_Weight"));
productPriceText.setText(rs.getString("Product_Price"));
JOptionPane.showMessageDialog(null,"Record Found");
}
conn.close();
state.close();
}
catch (Exception er) {
JOptionPane.showMessageDialog(null ,"Record not Found","Warning", JOptionPane.INFORMATION_MESSAGE);
}
}
public void up_date()
{
String Name =SearchText.getText();
int PID=Integer.parseInt(productTextID.getText());
String PN=producNametText.getText();
String PQ=productQtext.getText();
int PW=Integer.parseInt(productWeightText.getText());
String PD=productDateText.getText();
int PP=Integer.parseInt(productPriceText.getText());
String SQL = "SELECT Product_ID,Product_Name,Product_Qulity,Product_Weight,Product_Date,Product_Price FROM ProductT WHERE Product_Name = '"+Name+"'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:SuperMarket");
state = conn.createStatement();
rs = state.executeQuery(SQL);
while(rs.next()){
String str="UPDATE ProductT SET Product_Name = '"+PN+"' ,Product_ID="+PID+", Product_Qulity= '"+PQ+"',Product_Price = "+PP+", Product_Date= '"+PD+"',Product_Weight = "+PW+" WHERE Product_Name = '"+Name+"'" ;
state.executeUpdate(str);
}
conn.close();
state.close();
}
catch (Exception er) {
JOptionPane.showMessageDialog(null ,"Record not Updated","Warning", JOptionPane.INFORMATION_MESSAGE);
System.out.println(er);
}
}
public void clear(){
productPriceText.setText("");
productDateText.setText("");
productQtext.setText("");
productTextID.setText("");
producNametText.setText("");
productWeightText.setText("");
SearchText.setText("");
productTextID.requestFocus();
}
}