Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.CardLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.EventQueue;
- import java.awt.Font;
- import java.awt.SystemColor;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JRadioButton;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- import javax.swing.border.BevelBorder;
- import javax.swing.border.LineBorder;
- import javax.swing.table.DefaultTableModel;
- import java.sql.*;
- import org.eclipse.wb.swing.FocusTraversalOnArray;
- public class Database {
- private JFrame frame;
- private JFrame frame1;
- private JTextField Username;
- private JPasswordField password;
- private JPanel panel;
- private CardLayout card;
- private JTextField usersign;
- private JTextField firstname;
- private JTextField lastname;
- private JPasswordField passwordsign;
- private JTextField textField_2;
- private JTextField threshold;
- private JTextField EnterISBN;
- private JTextField title;
- private JTextField Publisher;
- private JTextField Year;
- private JTextField price;
- private JTextField category;
- private JTextField copies;
- private JTextPane authorList;
- private JTable table;
- private DefaultTableModel model;
- private Connection con;
- private Statement stt;
- String insertINtoTable(String table, ArrayList<String> attr,
- ArrayList<String> values) {
- String query = "Insert into `" + table + "`(";
- for (int i = 0; i < attr.size(); i++) {
- query += "`" + attr.get(i) + "`";
- if (i != attr.size() - 1)
- query += ",";
- }
- query += ") values (";
- for (int i = 0; i < values.size(); i++) {
- query += "'" + values.get(i) + "'";
- if (i != values.size() - 1)
- query += ",";
- }
- query += ")";
- return query;
- }
- private void printSearch(ResultSet res ){
- try {
- while (res.next()) {
- String isbn = res.getString("ISBN");
- String title = res.getString("Title");
- String Publisher = res.getString("Publisher");
- String Year = res.getString("Publication year");
- String price = res.getString("Price");
- String catg = res.getString("Category");
- String copy = res.getString("Copies");
- String threshold = res.getString("Threshold");
- model.addRow(new Object[] { isbn, title, Publisher,
- Year, price, catg, copy, threshold });
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- boolean validateEmpty(String a) {
- if (a.length() == 0)
- return false;
- return true;
- }
- boolean validatePrice(String a) {
- try {
- double k = Double.parseDouble(a);
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- boolean checkInsert() {
- boolean check = true;
- if (EnterISBN.getText().equals("") || !validateNum(EnterISBN.getText())) {
- check = false;
- } else if (title.getText().equals("")) {
- check = false;
- } else if (Publisher.getText().equals("")) {
- check = false;
- } else if (Year.getText().equals("") || !validateNum(Year.getText())) {
- check = false;
- } else if (price.getText().equals("")
- || !validatePrice(price.getText())) {
- check = false;
- } else if (category.getText().equals("")) {
- check = false;
- } else if (copies.getText().equals("")
- || !validateNum(copies.getText())) {
- check = false;
- } else if (threshold.getText().equals("")
- || !validateNum(threshold.getText())) {
- check = false;
- } else if (authorList.getText().equals("")) {
- check = false;
- }
- if (!check) {
- JOptionPane.showMessageDialog(frame,
- "please check inserted fields.", "Inane error",
- JOptionPane.ERROR_MESSAGE);
- }
- return check;
- }
- boolean validatePhone(String a) {
- if (a.length() == 0)
- return false;
- if (a.charAt(0) != '+' && (a.charAt(0) < '0' || a.charAt(0) > '9'))
- return false;
- for (int i = 1; i < a.length(); i++) {
- if (a.charAt(i) >= '0' && a.charAt(i) <= '9')
- continue;
- return false;
- }
- return true;
- }
- boolean validateNum(String a) {
- for (int i = 1; i < a.length(); i++) {
- if (a.charAt(i) >= '0' && a.charAt(i) <= '9')
- continue;
- return false;
- }
- return true;
- }
- void publisherData() {
- String publisher = Publisher.getText();
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book WHERE `Publisher` = '"
- + publisher + "'");
- if (!res.next()) {
- JTextField address = new JTextField();
- JTextField phone = new JTextField();
- Object[] message = { "address:", address, "phone:", phone };
- int option = JOptionPane.showConfirmDialog(null, message,
- "Publisher", JOptionPane.OK_CANCEL_OPTION);
- while (option == JOptionPane.OK_OPTION
- && (address.getText().equals("") || !validatePhone(phone
- .getText()))) {
- option = JOptionPane.showConfirmDialog(null, message,
- "Publisher", JOptionPane.OK_CANCEL_OPTION);
- }
- if (option == JOptionPane.OK_OPTION) {
- ArrayList<String> attr = new ArrayList();
- ArrayList<String> value = new ArrayList();
- attr.add("Publisher");
- attr.add("Address");
- attr.add("Phone");
- value.add(publisher);
- value.add(address.getText());
- value.add(phone.getText());
- String query = insertINtoTable("Publisher", attr, value);
- stt.execute(query);
- System.out.println(query);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Database window = new Database();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- *
- * @throws SQLException
- * @throws ClassNotFoundException
- * @throws IllegalAccessException
- * @throws InstantiationException
- */
- public Database() throws InstantiationException, IllegalAccessException,
- ClassNotFoundException, SQLException {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- *
- * @throws SQLException
- * @throws ClassNotFoundException
- * @throws IllegalAccessException
- * @throws InstantiationException
- */
- private void initialize() throws SQLException, InstantiationException,
- IllegalAccessException, ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String url = "jdbc:mysql://localhost:3306/";
- con = DriverManager.getConnection(url, "root", "");
- stt = con.createStatement();
- stt.execute("USE book_store");
- frame = new JFrame();
- frame.getContentPane().setForeground(SystemColor.activeCaption);
- frame.getContentPane().setBackground(Color.LIGHT_GRAY);
- frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 13));
- frame.setBounds(100, 100, 541, 476);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- /*
- * frame1=new JFrame();
- * frame1.getContentPane().setForeground(SystemColor.activeCaption);
- * frame1.getContentPane().setBackground(Color.LIGHT_GRAY);
- * frame1.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 13));
- * frame1.setBounds(100, 100, 450, 476);
- * frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- * frame1.getContentPane().setLayout(null);
- */
- panel = new JPanel();
- panel.setBounds(0, 0, 544, 430);
- frame.getContentPane().add(panel);
- card = new CardLayout(0, 0);
- panel.setLayout(card);
- JPanel panel_1 = new JPanel();
- panel_1.setBounds(0, 0, 544, 430);
- panel.add(panel_1, "first");
- panel_1.setLayout(null);
- Username = new JTextField();
- Username.setBounds(154, 163, 126, 20);
- panel_1.add(Username);
- Username.setColumns(10);
- JLabel lblUsername = new JLabel("Username");
- lblUsername.setBounds(64, 166, 75, 14);
- lblUsername.setFont(new Font("Tahoma", Font.BOLD, 13));
- lblUsername.setBackground(Color.BLUE);
- panel_1.add(lblUsername);
- JLabel lblPassword = new JLabel("Password");
- lblPassword.setBounds(64, 197, 69, 14);
- lblPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
- panel_1.add(lblPassword);
- password = new JPasswordField();
- password.setBounds(154, 194, 126, 20);
- panel_1.add(password);
- JButton btnLogin = new JButton("login");
- btnLogin.setBounds(154, 249, 89, 23);
- btnLogin.setBackground(Color.ORANGE);
- panel_1.add(btnLogin);
- final JPanel panel_2 = new JPanel();
- panel_2.setBounds(318, 58, 89, 23);
- panel_1.add(panel_2, "second");
- panel_2.setLayout(null);
- JButton btnSignUp = new JButton("sign up");
- btnSignUp.setBounds(0, 0, 89, 23);
- panel_2.add(btnSignUp);
- btnSignUp.setFont(new Font("Tahoma", Font.BOLD, 13));
- btnSignUp.setBackground(Color.WHITE);
- JPanel panel_3 = new JPanel();
- panel_3.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(255,
- 0, 0), Color.RED, Color.RED, Color.RED));
- panel_3.setBounds(36, 107, 331, 215);
- panel_1.add(panel_3, "third");
- panel_3.setLayout(null);
- JLabel lblNewLabel = new JLabel("Login");
- lblNewLabel.setBounds(149, 24, 46, 14);
- panel_3.add(lblNewLabel);
- JLabel errorMsg = new JLabel("");
- errorMsg.setBounds(85, 117, 197, 14);
- panel_3.add(errorMsg);
- panel.setFocusTraversalPolicy(new FocusTraversalOnArray(
- new Component[] { Username, lblUsername, lblPassword, password,
- btnLogin, btnSignUp }));
- JPanel panel_4 = new JPanel();
- panel_4.setBackground(Color.WHITE);
- panel_4.setBounds(0, 0, 544, 430);
- panel.add(panel_4, "fourth");
- panel_4.setLayout(null);
- JLabel lblSignUp = new JLabel("Sign up");
- lblSignUp.setFont(new Font("Tahoma", Font.BOLD, 14));
- lblSignUp.setBounds(188, 54, 102, 31);
- panel_4.add(lblSignUp);
- usersign = new JTextField();
- usersign.setBounds(150, 122, 154, 20);
- panel_4.add(usersign);
- usersign.setColumns(10);
- firstname = new JTextField();
- firstname.setBounds(150, 240, 154, 20);
- panel_4.add(firstname);
- firstname.setColumns(10);
- lastname = new JTextField();
- lastname.setBounds(150, 300, 154, 20);
- panel_4.add(lastname);
- lastname.setColumns(10);
- JLabel lblUsername_1 = new JLabel("Username");
- lblUsername_1.setBounds(35, 128, 105, 14);
- panel_4.add(lblUsername_1);
- passwordsign = new JPasswordField();
- passwordsign.setBounds(150, 179, 154, 20);
- panel_4.add(passwordsign);
- JLabel lblPassword_1 = new JLabel("Password");
- lblPassword_1.setBounds(35, 185, 105, 14);
- panel_4.add(lblPassword_1);
- JLabel lblFirstName = new JLabel("First Name");
- lblFirstName.setBounds(35, 246, 105, 14);
- panel_4.add(lblFirstName);
- JLabel lblNewLabel_1 = new JLabel("Last Name");
- lblNewLabel_1.setBounds(35, 306, 105, 14);
- panel_4.add(lblNewLabel_1);
- JButton btnSubmit = new JButton("Register");
- btnSubmit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if (validateEmpty(usersign.getText())
- && validateEmpty(passwordsign.getText())
- && validateEmpty(firstname.getText())
- && validateEmpty(lastname.getText())) {
- ArrayList<String> attr = new ArrayList();
- ArrayList<String> values = new ArrayList();
- attr.add("Username");
- attr.add("Password");
- attr.add("First_name");
- attr.add("Last_name");
- values.add(usersign.getText());
- values.add(passwordsign.getText());
- values.add(firstname.getText());
- values.add(lastname.getText());
- String query = insertINtoTable("Register", attr, values);
- System.out.println(query);
- // insert this query into register table
- // username is primary key
- // if this username is not duplicate ,show this message
- JOptionPane.showMessageDialog(frame,
- "Register done successfully !", "A plain message",
- JOptionPane.PLAIN_MESSAGE);
- // else show this message
- JOptionPane.showMessageDialog(frame,
- "This username exist before !", "Inane warning",
- JOptionPane.WARNING_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(frame,
- "Please fill empty fields !", "Inane warning",
- JOptionPane.WARNING_MESSAGE);
- }
- }
- });
- btnSubmit.setBounds(178, 372, 89, 23);
- panel_4.add(btnSubmit);
- JButton btnNewButton = new JButton("Back");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- card.show(panel, "first");
- }
- });
- btnNewButton.setBounds(10, 11, 89, 23);
- panel_4.add(btnNewButton);
- JLabel usernameMsg = new JLabel("");
- usernameMsg.setBounds(150, 151, 236, 14);
- panel_4.add(usernameMsg);
- JLabel passwordMsg = new JLabel("");
- passwordMsg.setBounds(150, 215, 236, 14);
- panel_4.add(passwordMsg);
- JLabel firstnameMsg = new JLabel("");
- firstnameMsg.setBounds(150, 275, 236, 14);
- panel_4.add(firstnameMsg);
- JLabel lastnameMsg = new JLabel("");
- lastnameMsg.setBounds(150, 331, 291, 14);
- panel_4.add(lastnameMsg);
- btnSignUp.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "fourth");
- }
- });
- // *************************operations***************************
- JPanel panel_5 = new JPanel();
- panel_5.setBackground(Color.CYAN);
- panel_5.setBounds(0, 0, 424, 419);
- panel.add(panel_5, "operations");
- panel_5.setLayout(null);
- JPanel panel_7 = new JPanel();
- panel_7.setBackground(Color.BLUE);
- panel_7.setBorder(new LineBorder(new Color(0, 0, 0), 4));
- panel_7.setBounds(125, 92, 137, 254);
- panel_5.add(panel_7);
- panel_7.setLayout(null);
- JButton btnNewButton1 = new JButton("add book");
- btnNewButton1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "addbooks");
- }
- });
- btnNewButton1.setBounds(6, 16, 125, 23);
- panel_7.add(btnNewButton1);
- JButton btnNewButton_1 = new JButton("update");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "search/update");
- }
- });
- btnNewButton_1.setBounds(6, 86, 125, 23);
- panel_7.add(btnNewButton_1);
- JButton btnNewButton_2 = new JButton("confirm order");
- btnNewButton_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "confirmOrders");
- }
- });
- btnNewButton_2.setBounds(6, 148, 125, 23);
- panel_7.add(btnNewButton_2);
- JButton btnNewButton_3 = new JButton("search");
- btnNewButton_3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "search/update");
- }
- });
- btnNewButton_3.setBounds(6, 216, 125, 23);
- panel_7.add(btnNewButton_3);
- JLabel lblUsersOperations = new JLabel(
- " Users operations");
- lblUsersOperations.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC,
- 14));
- lblUsersOperations.setBounds(45, 50, 363, 39);
- panel_5.add(lblUsersOperations);
- JLabel lblNewLabel_2 = new JLabel("New label");
- lblNewLabel_2.setBounds(10, 11, 150, 28);
- panel_5.add(lblNewLabel_2);
- JButton btnLogout = new JButton("logout");
- btnLogout.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "first");
- }
- });
- btnLogout.setBounds(319, 14, 89, 23);
- panel_5.add(btnLogout);
- JPanel panel_8 = new JPanel();
- panel_8.setBounds(50, 50, 10, 10);
- panel.add(panel_8, "confirmOrders");
- panel_8.setLayout(null);
- JButton btnNewButton_4 = new JButton("Back");
- btnNewButton_4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "operations");
- }
- });
- btnNewButton_4.setBounds(10, 11, 89, 23);
- panel_8.add(btnNewButton_4);
- textField_2 = new JTextField();
- textField_2.setBounds(162, 138, 138, 20);
- panel_8.add(textField_2);
- textField_2.setColumns(10);
- JLabel lblNewLabel_3 = new JLabel("Enter ISBN");
- lblNewLabel_3.setBounds(38, 141, 114, 14);
- panel_8.add(lblNewLabel_3);
- JLabel lblNewLabel_4 = new JLabel("Confirm Order");
- lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
- lblNewLabel_4.setBounds(177, 73, 156, 14);
- panel_8.add(lblNewLabel_4);
- JButton btnNewButton_5 = new JButton("Confirm");
- btnNewButton_5.setBounds(177, 207, 89, 23);
- panel_8.add(btnNewButton_5);
- JLabel msg = new JLabel("");
- msg.setBounds(96, 303, 304, 14);
- panel_8.add(msg);
- panel_5.setLayout(null);
- JPanel panel_9 = new JPanel();
- panel_9.setBounds(55, 92, 10, 10);
- panel.add(panel_9, "addbooks");
- panel_9.setLayout(null);
- JButton btnNewButton_6 = new JButton("Back");
- btnNewButton_6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- card.show(panel, "operations");
- }
- });
- btnNewButton_6.setBounds(0, 11, 89, 23);
- panel_9.add(btnNewButton_6);
- threshold = new JTextField();
- threshold.setBounds(238, 258, 170, 20);
- panel_9.add(threshold);
- threshold.setColumns(10);
- EnterISBN = new JTextField();
- EnterISBN.setBounds(21, 92, 170, 20);
- panel_9.add(EnterISBN);
- EnterISBN.setColumns(10);
- JLabel lblNewLabel_5 = new JLabel("");
- lblNewLabel_5.setForeground(Color.RED);
- lblNewLabel_5.setBackground(Color.WHITE);
- lblNewLabel_5.setBounds(347, 58, 127, 14);
- panel_9.add(lblNewLabel_5);
- title = new JTextField();
- title.setBounds(238, 92, 170, 20);
- panel_9.add(title);
- title.setColumns(10);
- Publisher = new JTextField();
- Publisher.setBounds(21, 148, 170, 20);
- panel_9.add(Publisher);
- Publisher.setColumns(10);
- Year = new JTextField();
- Year.setBounds(238, 148, 170, 20);
- panel_9.add(Year);
- Year.setColumns(10);
- price = new JTextField();
- price.setBounds(21, 202, 170, 20);
- panel_9.add(price);
- price.setColumns(10);
- category = new JTextField();
- category.setBounds(238, 202, 170, 20);
- panel_9.add(category);
- category.setColumns(10);
- copies = new JTextField();
- copies.setBounds(21, 258, 170, 20);
- panel_9.add(copies);
- copies.setColumns(10);
- JLabel lblNewLabel_6 = new JLabel("Insert Book");
- lblNewLabel_6.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 14));
- lblNewLabel_6.setBounds(216, 40, 176, 14);
- panel_9.add(lblNewLabel_6);
- JLabel lblNewLabel_7 = new JLabel("Enter ISBN");
- lblNewLabel_7.setBounds(28, 67, 114, 14);
- panel_9.add(lblNewLabel_7);
- JLabel lblNewLabel_8 = new JLabel("Title");
- lblNewLabel_8.setBounds(274, 67, 79, 14);
- panel_9.add(lblNewLabel_8);
- JLabel lblNewLabel_9 = new JLabel("Publisher");
- lblNewLabel_9.setBounds(31, 121, 114, 14);
- panel_9.add(lblNewLabel_9);
- JLabel year = new JLabel("Publication year");
- year.setBounds(274, 123, 147, 14);
- panel_9.add(year);
- JLabel lblNewLabel_11 = new JLabel("Selling price");
- lblNewLabel_11.setBounds(31, 177, 114, 14);
- panel_9.add(lblNewLabel_11);
- JLabel lblNewLabel_12 = new JLabel("Category");
- lblNewLabel_12.setBounds(274, 179, 114, 14);
- panel_9.add(lblNewLabel_12);
- JLabel lblNewLabel_13 = new JLabel("Copies");
- lblNewLabel_13.setBounds(31, 233, 79, 14);
- panel_9.add(lblNewLabel_13);
- JLabel lblNewLabel_14 = new JLabel("Threshold");
- lblNewLabel_14.setBounds(271, 233, 102, 14);
- panel_9.add(lblNewLabel_14);
- JButton btnNewButton_7 = new JButton("Insert");
- btnNewButton_7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (checkInsert()) {
- publisherData();
- ArrayList<String> attr = new ArrayList();
- ArrayList<String> value = new ArrayList();
- attr.add("ISBN");
- attr.add("Title");
- attr.add("Publisher");
- attr.add("Publication year");
- attr.add("Price");
- attr.add("Category");
- attr.add("Copies");
- attr.add("Threshold");
- value.add(EnterISBN.getText());
- value.add(title.getText());
- value.add(Publisher.getText());
- value.add(Year.getText());
- value.add(price.getText());
- value.add(category.getText());
- value.add(copies.getText());
- value.add(threshold.getText());
- String query = insertINtoTable("Book", attr, value);
- try {
- stt.execute(query);
- String kk[] = authorList.getText().split("\n");
- String query2 = "";
- ArrayList<String> attr1;
- ArrayList<String> value1;
- for (int i = 0; i < kk.length; i++) {
- attr1 = new ArrayList();
- value1 = new ArrayList();
- attr1.add("ISBN");
- attr1.add("Author");
- value1.add(EnterISBN.getText());
- value1.add(kk[i]);
- query2 = insertINtoTable("Authors", attr1, value1);
- try {
- stt.execute(query2);
- } catch (SQLException e1) {
- // TODO Auto-generated // catch // block //
- // e1.printStackTrace();
- }
- System.out.println(query2);
- }
- } catch (SQLException e1) {
- JOptionPane.showConfirmDialog(null, "duplicate ISBN");
- // TODO Auto-generated
- // e1.printStackTrace();
- }
- }
- }
- });
- btnNewButton_7.setBounds(200, 384, 89, 23);
- panel_9.add(btnNewButton_7);
- authorList = new JTextPane();
- authorList.setEditable(false);
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(21, 312, 429, 71);
- panel_9.add(scrollPane);
- scrollPane.setViewportView(authorList);
- JButton btnNewButton_9 = new JButton("insert author");
- btnNewButton_9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JTextField author = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, author,
- "search", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION
- && !author.getText().equals("")) {
- if (!authorList.getText().equals(""))
- authorList.setText(authorList.getText() + "\n"
- + author.getText());
- else
- authorList.setText(author.getText());
- }
- }
- });
- btnNewButton_9.setBounds(168, 285, 121, 23);
- panel_9.add(btnNewButton_9);
- JPanel panel_6 = new JPanel();
- panel_6.setBounds(0, 0, 432, 419);
- panel.add(panel_6, "search/update");
- btnLogin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if (validateEmpty(Username.getText())) {
- if (validateEmpty(password.getText())) {
- // search username and password in your database
- // if found do this
- card.show(panel, "operations");
- // else show this message
- JOptionPane.showMessageDialog(frame,
- "Invalid username or password !",
- "Inane warning", JOptionPane.WARNING_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(frame,
- "Invalid Password", "Inane error",
- JOptionPane.ERROR_MESSAGE);
- }
- } else {
- JOptionPane.showMessageDialog(frame, "Invalid Username",
- "Inane error", JOptionPane.ERROR_MESSAGE);
- }
- }
- });
- panel_6.setLayout(null);
- JLabel lblSearchBy = new JLabel("search by");
- lblSearchBy.setBounds(10, 39, 65, 14);
- panel_6.add(lblSearchBy);
- JButton btnNewButton_8 = new JButton("back");
- btnNewButton_8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- card.show(panel, "operations");
- }
- });
- btnNewButton_8.setBounds(0, 5, 89, 23);
- panel_6.add(btnNewButton_8);
- table = new JTable();
- Object rows[][] = new Object[][] { { "ISBN", "Title", "Publisher",
- "Year", "Price", "Categ", "#Copy", "Threshold" },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- // { null, null, null, null, null, null, null, null },
- };
- table.setModel(new DefaultTableModel(rows, new String[] { "ISBN",
- "Title", "Publisher", "Year", "Price", "Categ", "#Copy",
- "Threshold" }) {
- boolean[] columnEditables = new boolean[] { false, false, false,
- false, false, false, false, false };
- public boolean isCellEditable(int row, int column) {
- return columnEditables[column];
- }
- });
- table.setBounds(10, 99, 512, 320);
- panel_6.add(table);
- model = (DefaultTableModel) table.getModel();
- table.setModel(model);
- // model.addRow( new Object[] { "ISBN555", "Title", "Publisher", "Year",
- // "Price",
- // "Categ", "#Copy", "Threshold" });
- JRadioButton rdbtnIsbnAndTiltle = new JRadioButton("ISBN and Tiltle");
- rdbtnIsbnAndTiltle.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JTextField ISBN = new JTextField();
- JTextField Title = new JTextField();
- Object[] message = { "ISBN:", ISBN, "Title:", Title };
- int option = JOptionPane.showConfirmDialog(null, message,
- "search", JOptionPane.OK_CANCEL_OPTION);
- while (option == JOptionPane.OK_OPTION
- && (Title.getText().equals("") || ISBN.getText()
- .equals(""))) {
- option = JOptionPane.showConfirmDialog(null, message,
- "Publisher", JOptionPane.OK_CANCEL_OPTION);
- }
- if (option == JOptionPane.OK_OPTION) {
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book WHERE `ISBN` LIKE '"
- + ISBN.getText()
- + "' AND `Title` LIKE '"
- + Title.getText() + "'");
- printSearch(res);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- rdbtnIsbnAndTiltle.setBounds(66, 35, 115, 23);
- panel_6.add(rdbtnIsbnAndTiltle);
- JRadioButton rdbtnCategory = new JRadioButton("Category");
- rdbtnCategory.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JTextField catg = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, catg,
- "search", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION) {
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book WHERE `Category` LIKE '"
- + catg.getText()
- + "'");
- printSearch(res);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- rdbtnCategory.setBounds(178, 35, 83, 23);
- panel_6.add(rdbtnCategory);
- JRadioButton rdbtnAuthor = new JRadioButton("author");
- rdbtnAuthor.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JTextField author = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, author,
- "search", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION) {
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book AS b, authors AS a WHERE b.`ISBN`=a.`ISBN` AND a.`Author` LIKE '"
- + author.getText()
- + "'");
- printSearch(res);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- rdbtnAuthor.setBounds(257, 35, 65, 23);
- panel_6.add(rdbtnAuthor);
- JRadioButton rdbtnPublisher = new JRadioButton("publisher");
- rdbtnPublisher.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JTextField pub = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, pub,
- "search", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION) {
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book AS b, Publisher AS a WHERE b.`ISBN`=a.`ISBN` AND a.`Publisher` LIKE '"
- + pub.getText()
- + "'");
- printSearch(res);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- rdbtnPublisher.setBounds(324, 35, 94, 23);
- panel_6.add(rdbtnPublisher);
- JRadioButton rdbtnCopies = new JRadioButton("copies");
- rdbtnCopies.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JTextField copy = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, copy,
- "search", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION) {
- try {
- ResultSet res = stt
- .executeQuery("SELECT * FROM book WHERE `Copies` = '"
- + copy.getText()
- + "'");
- printSearch(res);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- rdbtnCopies.setBounds(414, 35, 77, 23);
- panel_6.add(rdbtnCopies);
- ButtonGroup group = new ButtonGroup();
- group.add(rdbtnIsbnAndTiltle);
- group.add(rdbtnCategory);
- group.add(rdbtnAuthor);
- group.add(rdbtnPublisher);
- group.add(rdbtnCopies);
- JButton btnNewButton_41 = new JButton("Update");
- btnNewButton_41.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- int selectedRowIndex =0;
- try{
- selectedRowIndex=table.getSelectedRow();
- Object isbn = (Object) table.getModel().getValueAt(selectedRowIndex,0);
- // check search al 2wl
- // lma y3ml
- JTextField update = new JTextField();
- int option = JOptionPane.showConfirmDialog(null, update,
- "update", JOptionPane.OK_CANCEL_OPTION);
- if (option == JOptionPane.OK_OPTION) {
- }
- }catch(Exception e){
- JOptionPane.showMessageDialog(null,"select a book please before update" );
- }
- }
- });
- btnNewButton_41.setBounds(182, 72, 94, 23);
- panel_6.add(btnNewButton_41);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement