Source code for admin login of the project.
package RMSystems;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.proteanit.sql.DbUtils;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import java.awt.Toolkit;
public class Update extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table_Foods;
private JTable table_Drinks;
private JTextField text_Did;
private JTextField text_DName;
private JTextField text_DQuentity;
private JTextField text_DPrice;
private JTextField text_Fid;
private JTextField text_FName;
private JTextField text_FQuentity;
private JTextField text_FPrice;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Update frame = new Update();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
Connection con = null;
public void RefreshFood(){
try {
String query = “select Id, Name, Quentity, Price from Food_Price”;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table_Foods.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void RefreshDrink(){
try {
String query = “select * from Drinks_Price”;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table_Drinks.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Update() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Update.class.getResource(“/imgs/FoodPICCC.png”)));
setTitle(“Update”);
con = sqliteConnection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 877, 650);
contentPane = new JPanel();
contentPane.setBackground(new Color(95, 158, 160));
contentPane.setBorder(new LineBorder(new Color(255, 0, 255), 2, true));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnBack = new JButton(“Back”);
btnBack.setIcon(new ImageIcon(Update.class.getResource(“/imgs/back1.png”)));
btnBack.setBackground(Color.WHITE);
btnBack.setForeground(Color.GREEN);
btnBack.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 16));
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
RAdmin RB = new RAdmin();
RB.setVisible(true);
}
});
btnBack.setBounds(10, 11, 126, 50);
contentPane.add(btnBack);
JLabel lblUpdate = new JLabel(“Update Foods & Drinks”);
lblUpdate.setForeground(Color.WHITE);
lblUpdate.setFont(new Font(“Harlow Solid Italic”, Font.BOLD, 25));
lblUpdate.setBounds(265, 29, 276, 32);
contentPane.add(lblUpdate);
JButton btn_Drinks = new JButton(“Drinks”);
btn_Drinks.setForeground(Color.BLUE);
btn_Drinks.setFont(new Font(“Times New Roman”, Font.BOLD, 20));
btn_Drinks.setBackground(Color.WHITE);
btn_Drinks.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = “select * Drinks_Price”;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table_Drinks.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshDrink();
}
});
btn_Drinks.setBounds(568, 77, 104, 42);
contentPane.add(btn_Drinks);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(439, 130, 400, 213);
contentPane.add(scrollPane_1);
table_Drinks = new JTable();
table_Drinks.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
int row = table_Drinks.getSelectedRow();
String Id = (table_Drinks.getModel().getValueAt(row, 0)).toString();
String query = “select * from Drinks_Price where Id='”+Id+”‘ “;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
text_Did.setText(rs.getString(“Id”));
text_DName.setText(rs.getString(“Name”));
text_DQuentity.setText(rs.getString(“Quentity”));
text_DPrice.setText(rs.getString(“Price”));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
scrollPane_1.setViewportView(table_Drinks);
JLabel lbl_Did = new JLabel(“ID”);
lbl_Did.setForeground(Color.WHITE);
lbl_Did.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_Did.setBounds(452, 366, 89, 14);
contentPane.add(lbl_Did);
JLabel lbl_DName = new JLabel(“Name”);
lbl_DName.setForeground(Color.WHITE);
lbl_DName.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_DName.setBounds(452, 391, 89, 14);
contentPane.add(lbl_DName);
JLabel lbl_DQuentity = new JLabel(“Quentity”);
lbl_DQuentity.setForeground(Color.WHITE);
lbl_DQuentity.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_DQuentity.setBounds(452, 414, 89, 20);
contentPane.add(lbl_DQuentity);
JLabel lbl_DPrice = new JLabel(“Price”);
lbl_DPrice.setForeground(Color.WHITE);
lbl_DPrice.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_DPrice.setBounds(452, 440, 89, 14);
contentPane.add(lbl_DPrice);
text_Did = new JTextField();
text_Did.setBounds(585, 363, 159, 20);
contentPane.add(text_Did);
text_Did.setColumns(10);
text_DName = new JTextField();
text_DName.setBounds(585, 389, 159, 20);
contentPane.add(text_DName);
text_DName.setColumns(10);
text_DQuentity = new JTextField();
text_DQuentity.setBounds(585, 414, 159, 20);
contentPane.add(text_DQuentity);
text_DQuentity.setColumns(10);
text_DPrice = new JTextField();
text_DPrice.setBounds(585, 437, 159, 20);
contentPane.add(text_DPrice);
text_DPrice.setColumns(10);
JButton btn_DDelete = new JButton(“Delete”);
btn_DDelete.setForeground(Color.BLUE);
btn_DDelete.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_DDelete.setBackground(Color.WHITE);
btn_DDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rb = JOptionPane.showConfirmDialog(null, “Do You Really Want To Delete”,”Delete”,JOptionPane.YES_NO_OPTION);
if(rb == 0){
try {
String query = “delete from Drinks_Price where Id='”+text_Did.getText()+”‘ “;
PreparedStatement pst = con.prepareStatement(query);
pst.execute();
pst.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshDrink();
}
}
});
btn_DDelete.setBounds(750, 462, 101, 42);
contentPane.add(btn_DDelete);
JButton btn_DUPdate = new JButton(“Update”);
btn_DUPdate.setForeground(Color.BLUE);
btn_DUPdate.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_DUPdate.setBackground(Color.WHITE);
btn_DUPdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query2 = “Update Drinks_Price set Id='”+text_Did.getText()+”‘, Name='”+text_DName.getText()+”‘,”
+ ” Quentity='”+text_DQuentity.getText()+”‘, Price='”+text_DPrice.getText()+”‘ where Id='”+text_Did.getText()+”‘ “;
PreparedStatement pst2 = con.prepareStatement(query2);
pst2.execute();
pst2.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshDrink();
}
});
btn_DUPdate.setBounds(640, 462, 104, 42);
contentPane.add(btn_DUPdate);
JButton btn_DInsert = new JButton(“Insert”);
btn_DInsert.setForeground(Color.BLUE);
btn_DInsert.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_DInsert.setBackground(Color.WHITE);
btn_DInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = “insert into Drinks_Price (Id, Name, Quentity, Price) values(?, ?, ?, ?)”;
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, text_Did.getText());
pst.setString(2, text_DName.getText());
pst.setString(3, text_DQuentity.getText());
pst.setString(4, text_DPrice.getText());
pst.execute();
JOptionPane.showConfirmDialog(null, “Do You Really Want To Save”,”Saved”,JOptionPane.YES_NO_OPTION);
pst.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshDrink();
}
});
btn_DInsert.setBounds(750, 507, 101, 42);
contentPane.add(btn_DInsert);
JButton btn_DRefresh = new JButton(“Refresh”);
btn_DRefresh.setForeground(Color.BLUE);
btn_DRefresh.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_DRefresh.setBackground(Color.WHITE);
btn_DRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text_Did.setText(” “);
text_DName.setText(” “);
text_DQuentity.setText(” “);
text_DPrice.setText(” “);
}
});
btn_DRefresh.setBounds(640, 507, 104, 42);
contentPane.add(btn_DRefresh);
JButton btn_Food = new JButton(“Foods”);
btn_Food.setForeground(Color.BLUE);
btn_Food.setFont(new Font(“Times New Roman”, Font.BOLD | Font.ITALIC, 20));
btn_Food.setBackground(Color.WHITE);
btn_Food.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query = “select Id, Name, Quentity, Price from Food_Price”;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table_Foods.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RefreshFood();
}
});
btn_Food.setBounds(157, 77, 104, 42);
contentPane.add(btn_Food);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(21, 130, 400, 213);
contentPane.add(scrollPane);
table_Foods = new JTable();
table_Foods.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
int row = table_Foods.getSelectedRow();
String Id=(table_Foods.getModel().getValueAt(row, 0)).toString();
String query = “select * from Food_Price where Id='”+Id+”‘ “;
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
text_Fid.setText(rs.getString(“Id”));
text_FName.setText(rs.getString(“Name”));
text_FQuentity.setText(rs.getString(“Quentity”));
text_FPrice.setText(rs.getString(“Price”));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
scrollPane.setViewportView(table_Foods);
JLabel lbl_Fid = new JLabel(“ID”);
lbl_Fid.setForeground(Color.WHITE);
lbl_Fid.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_Fid.setBounds(37, 366, 89, 14);
contentPane.add(lbl_Fid);
JLabel lbl_FName = new JLabel(“Name”);
lbl_FName.setForeground(Color.WHITE);
lbl_FName.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_FName.setBounds(37, 391, 89, 14);
contentPane.add(lbl_FName);
JLabel lbl_FQuentity = new JLabel(“Quentity”);
lbl_FQuentity.setForeground(Color.WHITE);
lbl_FQuentity.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_FQuentity.setBounds(37, 415, 89, 19);
contentPane.add(lbl_FQuentity);
JLabel lbl_FPrice = new JLabel(“Price”);
lbl_FPrice.setForeground(Color.WHITE);
lbl_FPrice.setFont(new Font(“Tahoma”, Font.BOLD, 15));
lbl_FPrice.setBounds(37, 442, 89, 14);
contentPane.add(lbl_FPrice);
text_Fid = new JTextField();
text_Fid.setBounds(136, 363, 159, 20);
contentPane.add(text_Fid);
text_Fid.setColumns(10);
text_FName = new JTextField();
text_FName.setBounds(136, 388, 159, 20);
contentPane.add(text_FName);
text_FName.setColumns(10);
text_FQuentity = new JTextField();
text_FQuentity.setBounds(136, 412, 159, 20);
contentPane.add(text_FQuentity);
text_FQuentity.setColumns(10);
text_FPrice = new JTextField();
text_FPrice.setBounds(136, 437, 159, 20);
contentPane.add(text_FPrice);
text_FPrice.setColumns(10);
JButton btn_FDelete = new JButton(“Delete”);
btn_FDelete.setForeground(Color.BLUE);
btn_FDelete.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_FDelete.setBackground(Color.WHITE);
btn_FDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int action = JOptionPane.showConfirmDialog(null, “Do You Really Want To Delete”,”Delete”,JOptionPane.YES_NO_OPTION);
if(action == 0){
try {
String query = “delete from Food_Price where Id='”+text_Fid.getText()+”‘ “;
PreparedStatement pst = con.prepareStatement(query);
pst.execute();
pst.close();
} catch (Exception e1) {
e1.printStackTrace();
}
RefreshFood();
}
}
});
btn_FDelete.setBounds(300, 462, 104, 42);
contentPane.add(btn_FDelete);
JButton btn_FUpdate = new JButton(“Update”);
btn_FUpdate.setForeground(Color.BLUE);
btn_FUpdate.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_FUpdate.setBackground(Color.WHITE);
btn_FUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = “Update Food_Price set Id='”+text_Fid.getText()+”‘, Name='”+text_FName.getText()+”‘,”
+ ” Quentity='”+text_FQuentity.getText()+”‘, Price='”+text_FPrice.getText()+”‘ where Id='”+text_Fid.getText()+”‘ “;
PreparedStatement pst = con.prepareStatement(query);
pst.execute();
pst.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshFood();
}
});
btn_FUpdate.setBounds(191, 462, 104, 42);
contentPane.add(btn_FUpdate);
JButton btn_FInsert = new JButton(“Insert”);
btn_FInsert.setForeground(Color.BLUE);
btn_FInsert.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_FInsert.setBackground(Color.WHITE);
btn_FInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = “insert into Food_Price (Id, Name, Quentity, Price) values(?, ?, ?, ?)”;
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, text_Fid.getText());
pst.setString(2, text_FName.getText());
pst.setString(3, text_FQuentity.getText());
pst.setString(4, text_FPrice.getText());
pst.execute();
JOptionPane.showConfirmDialog(null, “Do You Really Want To Save”,”Saved”,JOptionPane.YES_NO_OPTION);
pst.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RefreshFood();
}
});
btn_FInsert.setBounds(300, 507, 104, 42);
contentPane.add(btn_FInsert);
JButton btn_FRefresh = new JButton(“Refresh”);
btn_FRefresh.setForeground(Color.BLUE);
btn_FRefresh.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 15));
btn_FRefresh.setBackground(Color.WHITE);
btn_FRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text_Fid.setText(” “);
text_FName.setText(” “);
text_FQuentity.setText(” “);
text_FPrice.setText(” “);
}
});
btn_FRefresh.setBounds(191, 507, 104, 42);
contentPane.add(btn_FRefresh);
JButton btnExit = new JButton(“Exit”);
btnExit.setIcon(new ImageIcon(Update.class.getResource(“/imgs/Exit.png”)));
btnExit.setBackground(Color.WHITE);
btnExit.setForeground(Color.RED);
btnExit.setFont(new Font(“Gill Sans MT”, Font.BOLD | Font.ITALIC, 16));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setBounds(736, 560, 115, 48);
contentPane.add(btnExit);
RefreshFood();
RefreshDrink();
}
}
Be First to Comment