Advertisement
Guest User

Untitled

a guest
May 29th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.38 KB | None | 0 0
  1. import java.awt.CardLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.EventQueue;
  5. import java.awt.Font;
  6. import java.awt.SystemColor;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.ButtonGroup;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JRadioButton;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTable;
  21. import javax.swing.JTextField;
  22. import javax.swing.JTextPane;
  23. import javax.swing.border.BevelBorder;
  24. import javax.swing.border.LineBorder;
  25. import javax.swing.table.DefaultTableModel;
  26. import java.sql.*;
  27.  
  28. import org.eclipse.wb.swing.FocusTraversalOnArray;
  29.  
  30. public class Database {
  31.  
  32. private JFrame frame;
  33. private JFrame frame1;
  34. private JTextField Username;
  35. private JPasswordField password;
  36. private JPanel panel;
  37. private CardLayout card;
  38. private JTextField usersign;
  39. private JTextField firstname;
  40. private JTextField lastname;
  41. private JPasswordField passwordsign;
  42. private JTextField textField_2;
  43. private JTextField threshold;
  44. private JTextField EnterISBN;
  45. private JTextField title;
  46. private JTextField Publisher;
  47. private JTextField Year;
  48. private JTextField price;
  49. private JTextField category;
  50. private JTextField copies;
  51. private JTextPane authorList;
  52. private JTable table;
  53. private DefaultTableModel model;
  54. private Connection con;
  55. private Statement stt;
  56.  
  57. String insertINtoTable(String table, ArrayList<String> attr,
  58. ArrayList<String> values) {
  59.  
  60. String query = "Insert into `" + table + "`(";
  61. for (int i = 0; i < attr.size(); i++) {
  62.  
  63. query += "`" + attr.get(i) + "`";
  64.  
  65. if (i != attr.size() - 1)
  66. query += ",";
  67. }
  68. query += ") values (";
  69. for (int i = 0; i < values.size(); i++) {
  70. query += "'" + values.get(i) + "'";
  71. if (i != values.size() - 1)
  72. query += ",";
  73. }
  74. query += ")";
  75. return query;
  76.  
  77. }
  78.  
  79. private void printSearch(ResultSet res ){
  80.  
  81. try {
  82. while (res.next()) {
  83. String isbn = res.getString("ISBN");
  84. String title = res.getString("Title");
  85. String Publisher = res.getString("Publisher");
  86. String Year = res.getString("Publication year");
  87. String price = res.getString("Price");
  88. String catg = res.getString("Category");
  89. String copy = res.getString("Copies");
  90. String threshold = res.getString("Threshold");
  91. model.addRow(new Object[] { isbn, title, Publisher,
  92. Year, price, catg, copy, threshold });
  93. }
  94. } catch (SQLException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. }
  99. boolean validateEmpty(String a) {
  100. if (a.length() == 0)
  101. return false;
  102.  
  103. return true;
  104.  
  105. }
  106.  
  107. boolean validatePrice(String a) {
  108. try {
  109. double k = Double.parseDouble(a);
  110. return true;
  111. } catch (Exception e) {
  112. return false;
  113. }
  114. }
  115.  
  116. boolean checkInsert() {
  117. boolean check = true;
  118. if (EnterISBN.getText().equals("") || !validateNum(EnterISBN.getText())) {
  119. check = false;
  120. } else if (title.getText().equals("")) {
  121. check = false;
  122. } else if (Publisher.getText().equals("")) {
  123. check = false;
  124.  
  125. } else if (Year.getText().equals("") || !validateNum(Year.getText())) {
  126. check = false;
  127. } else if (price.getText().equals("")
  128. || !validatePrice(price.getText())) {
  129. check = false;
  130. } else if (category.getText().equals("")) {
  131. check = false;
  132. } else if (copies.getText().equals("")
  133. || !validateNum(copies.getText())) {
  134. check = false;
  135. } else if (threshold.getText().equals("")
  136. || !validateNum(threshold.getText())) {
  137. check = false;
  138. } else if (authorList.getText().equals("")) {
  139. check = false;
  140. }
  141.  
  142. if (!check) {
  143.  
  144. JOptionPane.showMessageDialog(frame,
  145. "please check inserted fields.", "Inane error",
  146. JOptionPane.ERROR_MESSAGE);
  147.  
  148. }
  149. return check;
  150. }
  151.  
  152. boolean validatePhone(String a) {
  153. if (a.length() == 0)
  154. return false;
  155. if (a.charAt(0) != '+' && (a.charAt(0) < '0' || a.charAt(0) > '9'))
  156. return false;
  157. for (int i = 1; i < a.length(); i++) {
  158. if (a.charAt(i) >= '0' && a.charAt(i) <= '9')
  159. continue;
  160. return false;
  161. }
  162. return true;
  163. }
  164.  
  165. boolean validateNum(String a) {
  166. for (int i = 1; i < a.length(); i++) {
  167. if (a.charAt(i) >= '0' && a.charAt(i) <= '9')
  168. continue;
  169. return false;
  170. }
  171. return true;
  172. }
  173.  
  174. void publisherData() {
  175. String publisher = Publisher.getText();
  176.  
  177. try {
  178.  
  179. ResultSet res = stt
  180. .executeQuery("SELECT * FROM book WHERE `Publisher` = '"
  181. + publisher + "'");
  182.  
  183. if (!res.next()) {
  184. JTextField address = new JTextField();
  185. JTextField phone = new JTextField();
  186. Object[] message = { "address:", address, "phone:", phone };
  187.  
  188. int option = JOptionPane.showConfirmDialog(null, message,
  189. "Publisher", JOptionPane.OK_CANCEL_OPTION);
  190. while (option == JOptionPane.OK_OPTION
  191. && (address.getText().equals("") || !validatePhone(phone
  192. .getText()))) {
  193. option = JOptionPane.showConfirmDialog(null, message,
  194. "Publisher", JOptionPane.OK_CANCEL_OPTION);
  195. }
  196. if (option == JOptionPane.OK_OPTION) {
  197. ArrayList<String> attr = new ArrayList();
  198. ArrayList<String> value = new ArrayList();
  199. attr.add("Publisher");
  200. attr.add("Address");
  201. attr.add("Phone");
  202. value.add(publisher);
  203. value.add(address.getText());
  204. value.add(phone.getText());
  205. String query = insertINtoTable("Publisher", attr, value);
  206. stt.execute(query);
  207. System.out.println(query);
  208. }
  209.  
  210. }
  211. } catch (Exception e) {
  212. e.printStackTrace();
  213. }
  214. }
  215.  
  216. /**
  217. * Launch the application.
  218. */
  219. public static void main(String[] args) {
  220. EventQueue.invokeLater(new Runnable() {
  221. public void run() {
  222. try {
  223. Database window = new Database();
  224. window.frame.setVisible(true);
  225. } catch (Exception e) {
  226. e.printStackTrace();
  227. }
  228. }
  229. });
  230. }
  231.  
  232. /**
  233. * Create the application.
  234. *
  235. * @throws SQLException
  236. * @throws ClassNotFoundException
  237. * @throws IllegalAccessException
  238. * @throws InstantiationException
  239. */
  240. public Database() throws InstantiationException, IllegalAccessException,
  241. ClassNotFoundException, SQLException {
  242. initialize();
  243. }
  244.  
  245. /**
  246. * Initialize the contents of the frame.
  247. *
  248. * @throws SQLException
  249. * @throws ClassNotFoundException
  250. * @throws IllegalAccessException
  251. * @throws InstantiationException
  252. */
  253. private void initialize() throws SQLException, InstantiationException,
  254. IllegalAccessException, ClassNotFoundException {
  255.  
  256. Class.forName("com.mysql.jdbc.Driver").newInstance();
  257.  
  258. String url = "jdbc:mysql://localhost:3306/";
  259. con = DriverManager.getConnection(url, "root", "");
  260.  
  261. stt = con.createStatement();
  262. stt.execute("USE book_store");
  263.  
  264. frame = new JFrame();
  265. frame.getContentPane().setForeground(SystemColor.activeCaption);
  266. frame.getContentPane().setBackground(Color.LIGHT_GRAY);
  267. frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 13));
  268. frame.setBounds(100, 100, 541, 476);
  269. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  270. frame.getContentPane().setLayout(null);
  271. /*
  272. * frame1=new JFrame();
  273. * frame1.getContentPane().setForeground(SystemColor.activeCaption);
  274. * frame1.getContentPane().setBackground(Color.LIGHT_GRAY);
  275. * frame1.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 13));
  276. * frame1.setBounds(100, 100, 450, 476);
  277. * frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  278. * frame1.getContentPane().setLayout(null);
  279. */
  280. panel = new JPanel();
  281. panel.setBounds(0, 0, 544, 430);
  282. frame.getContentPane().add(panel);
  283. card = new CardLayout(0, 0);
  284. panel.setLayout(card);
  285.  
  286. JPanel panel_1 = new JPanel();
  287. panel_1.setBounds(0, 0, 544, 430);
  288. panel.add(panel_1, "first");
  289. panel_1.setLayout(null);
  290.  
  291. Username = new JTextField();
  292. Username.setBounds(154, 163, 126, 20);
  293. panel_1.add(Username);
  294. Username.setColumns(10);
  295.  
  296. JLabel lblUsername = new JLabel("Username");
  297. lblUsername.setBounds(64, 166, 75, 14);
  298. lblUsername.setFont(new Font("Tahoma", Font.BOLD, 13));
  299. lblUsername.setBackground(Color.BLUE);
  300. panel_1.add(lblUsername);
  301.  
  302. JLabel lblPassword = new JLabel("Password");
  303. lblPassword.setBounds(64, 197, 69, 14);
  304. lblPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
  305. panel_1.add(lblPassword);
  306.  
  307. password = new JPasswordField();
  308. password.setBounds(154, 194, 126, 20);
  309. panel_1.add(password);
  310.  
  311. JButton btnLogin = new JButton("login");
  312. btnLogin.setBounds(154, 249, 89, 23);
  313.  
  314. btnLogin.setBackground(Color.ORANGE);
  315. panel_1.add(btnLogin);
  316.  
  317. final JPanel panel_2 = new JPanel();
  318. panel_2.setBounds(318, 58, 89, 23);
  319. panel_1.add(panel_2, "second");
  320. panel_2.setLayout(null);
  321.  
  322. JButton btnSignUp = new JButton("sign up");
  323.  
  324. btnSignUp.setBounds(0, 0, 89, 23);
  325. panel_2.add(btnSignUp);
  326. btnSignUp.setFont(new Font("Tahoma", Font.BOLD, 13));
  327. btnSignUp.setBackground(Color.WHITE);
  328.  
  329. JPanel panel_3 = new JPanel();
  330. panel_3.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(255,
  331. 0, 0), Color.RED, Color.RED, Color.RED));
  332. panel_3.setBounds(36, 107, 331, 215);
  333. panel_1.add(panel_3, "third");
  334. panel_3.setLayout(null);
  335.  
  336. JLabel lblNewLabel = new JLabel("Login");
  337. lblNewLabel.setBounds(149, 24, 46, 14);
  338. panel_3.add(lblNewLabel);
  339.  
  340. JLabel errorMsg = new JLabel("");
  341. errorMsg.setBounds(85, 117, 197, 14);
  342. panel_3.add(errorMsg);
  343.  
  344. panel.setFocusTraversalPolicy(new FocusTraversalOnArray(
  345. new Component[] { Username, lblUsername, lblPassword, password,
  346. btnLogin, btnSignUp }));
  347. JPanel panel_4 = new JPanel();
  348. panel_4.setBackground(Color.WHITE);
  349. panel_4.setBounds(0, 0, 544, 430);
  350. panel.add(panel_4, "fourth");
  351. panel_4.setLayout(null);
  352.  
  353. JLabel lblSignUp = new JLabel("Sign up");
  354. lblSignUp.setFont(new Font("Tahoma", Font.BOLD, 14));
  355. lblSignUp.setBounds(188, 54, 102, 31);
  356. panel_4.add(lblSignUp);
  357.  
  358. usersign = new JTextField();
  359. usersign.setBounds(150, 122, 154, 20);
  360. panel_4.add(usersign);
  361. usersign.setColumns(10);
  362.  
  363. firstname = new JTextField();
  364. firstname.setBounds(150, 240, 154, 20);
  365. panel_4.add(firstname);
  366. firstname.setColumns(10);
  367.  
  368. lastname = new JTextField();
  369. lastname.setBounds(150, 300, 154, 20);
  370. panel_4.add(lastname);
  371. lastname.setColumns(10);
  372.  
  373. JLabel lblUsername_1 = new JLabel("Username");
  374. lblUsername_1.setBounds(35, 128, 105, 14);
  375. panel_4.add(lblUsername_1);
  376.  
  377. passwordsign = new JPasswordField();
  378. passwordsign.setBounds(150, 179, 154, 20);
  379. panel_4.add(passwordsign);
  380.  
  381. JLabel lblPassword_1 = new JLabel("Password");
  382. lblPassword_1.setBounds(35, 185, 105, 14);
  383. panel_4.add(lblPassword_1);
  384.  
  385. JLabel lblFirstName = new JLabel("First Name");
  386. lblFirstName.setBounds(35, 246, 105, 14);
  387. panel_4.add(lblFirstName);
  388.  
  389. JLabel lblNewLabel_1 = new JLabel("Last Name");
  390. lblNewLabel_1.setBounds(35, 306, 105, 14);
  391. panel_4.add(lblNewLabel_1);
  392.  
  393. JButton btnSubmit = new JButton("Register");
  394. btnSubmit.addActionListener(new ActionListener() {
  395. public void actionPerformed(ActionEvent arg0) {
  396.  
  397. if (validateEmpty(usersign.getText())
  398. && validateEmpty(passwordsign.getText())
  399. && validateEmpty(firstname.getText())
  400. && validateEmpty(lastname.getText())) {
  401. ArrayList<String> attr = new ArrayList();
  402. ArrayList<String> values = new ArrayList();
  403. attr.add("Username");
  404. attr.add("Password");
  405. attr.add("First_name");
  406. attr.add("Last_name");
  407. values.add(usersign.getText());
  408. values.add(passwordsign.getText());
  409. values.add(firstname.getText());
  410. values.add(lastname.getText());
  411. String query = insertINtoTable("Register", attr, values);
  412. System.out.println(query);
  413. // insert this query into register table
  414. // username is primary key
  415. // if this username is not duplicate ,show this message
  416.  
  417. JOptionPane.showMessageDialog(frame,
  418. "Register done successfully !", "A plain message",
  419. JOptionPane.PLAIN_MESSAGE);
  420.  
  421. // else show this message
  422. JOptionPane.showMessageDialog(frame,
  423. "This username exist before !", "Inane warning",
  424. JOptionPane.WARNING_MESSAGE);
  425. } else {
  426. JOptionPane.showMessageDialog(frame,
  427. "Please fill empty fields !", "Inane warning",
  428. JOptionPane.WARNING_MESSAGE);
  429.  
  430. }
  431.  
  432. }
  433. });
  434. btnSubmit.setBounds(178, 372, 89, 23);
  435. panel_4.add(btnSubmit);
  436.  
  437. JButton btnNewButton = new JButton("Back");
  438. btnNewButton.addActionListener(new ActionListener() {
  439. public void actionPerformed(ActionEvent arg0) {
  440. card.show(panel, "first");
  441.  
  442. }
  443. });
  444. btnNewButton.setBounds(10, 11, 89, 23);
  445. panel_4.add(btnNewButton);
  446.  
  447. JLabel usernameMsg = new JLabel("");
  448. usernameMsg.setBounds(150, 151, 236, 14);
  449. panel_4.add(usernameMsg);
  450.  
  451. JLabel passwordMsg = new JLabel("");
  452. passwordMsg.setBounds(150, 215, 236, 14);
  453. panel_4.add(passwordMsg);
  454.  
  455. JLabel firstnameMsg = new JLabel("");
  456. firstnameMsg.setBounds(150, 275, 236, 14);
  457. panel_4.add(firstnameMsg);
  458.  
  459. JLabel lastnameMsg = new JLabel("");
  460. lastnameMsg.setBounds(150, 331, 291, 14);
  461. panel_4.add(lastnameMsg);
  462. btnSignUp.addActionListener(new ActionListener() {
  463. public void actionPerformed(ActionEvent e) {
  464. card.show(panel, "fourth");
  465. }
  466. });
  467.  
  468. // *************************operations***************************
  469.  
  470. JPanel panel_5 = new JPanel();
  471. panel_5.setBackground(Color.CYAN);
  472. panel_5.setBounds(0, 0, 424, 419);
  473. panel.add(panel_5, "operations");
  474. panel_5.setLayout(null);
  475.  
  476. JPanel panel_7 = new JPanel();
  477. panel_7.setBackground(Color.BLUE);
  478. panel_7.setBorder(new LineBorder(new Color(0, 0, 0), 4));
  479. panel_7.setBounds(125, 92, 137, 254);
  480. panel_5.add(panel_7);
  481. panel_7.setLayout(null);
  482.  
  483. JButton btnNewButton1 = new JButton("add book");
  484. btnNewButton1.addActionListener(new ActionListener() {
  485. public void actionPerformed(ActionEvent e) {
  486. card.show(panel, "addbooks");
  487. }
  488. });
  489. btnNewButton1.setBounds(6, 16, 125, 23);
  490. panel_7.add(btnNewButton1);
  491.  
  492. JButton btnNewButton_1 = new JButton("update");
  493. btnNewButton_1.addActionListener(new ActionListener() {
  494. public void actionPerformed(ActionEvent e) {
  495. card.show(panel, "search/update");
  496. }
  497. });
  498. btnNewButton_1.setBounds(6, 86, 125, 23);
  499. panel_7.add(btnNewButton_1);
  500.  
  501. JButton btnNewButton_2 = new JButton("confirm order");
  502. btnNewButton_2.addActionListener(new ActionListener() {
  503. public void actionPerformed(ActionEvent e) {
  504. card.show(panel, "confirmOrders");
  505. }
  506. });
  507. btnNewButton_2.setBounds(6, 148, 125, 23);
  508. panel_7.add(btnNewButton_2);
  509.  
  510. JButton btnNewButton_3 = new JButton("search");
  511. btnNewButton_3.addActionListener(new ActionListener() {
  512. public void actionPerformed(ActionEvent e) {
  513. card.show(panel, "search/update");
  514. }
  515. });
  516. btnNewButton_3.setBounds(6, 216, 125, 23);
  517. panel_7.add(btnNewButton_3);
  518.  
  519. JLabel lblUsersOperations = new JLabel(
  520. " Users operations");
  521. lblUsersOperations.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC,
  522. 14));
  523. lblUsersOperations.setBounds(45, 50, 363, 39);
  524. panel_5.add(lblUsersOperations);
  525.  
  526. JLabel lblNewLabel_2 = new JLabel("New label");
  527. lblNewLabel_2.setBounds(10, 11, 150, 28);
  528. panel_5.add(lblNewLabel_2);
  529.  
  530. JButton btnLogout = new JButton("logout");
  531. btnLogout.addActionListener(new ActionListener() {
  532. public void actionPerformed(ActionEvent e) {
  533. card.show(panel, "first");
  534. }
  535. });
  536. btnLogout.setBounds(319, 14, 89, 23);
  537. panel_5.add(btnLogout);
  538.  
  539. JPanel panel_8 = new JPanel();
  540. panel_8.setBounds(50, 50, 10, 10);
  541. panel.add(panel_8, "confirmOrders");
  542. panel_8.setLayout(null);
  543.  
  544. JButton btnNewButton_4 = new JButton("Back");
  545. btnNewButton_4.addActionListener(new ActionListener() {
  546. public void actionPerformed(ActionEvent e) {
  547. card.show(panel, "operations");
  548. }
  549. });
  550. btnNewButton_4.setBounds(10, 11, 89, 23);
  551. panel_8.add(btnNewButton_4);
  552.  
  553. textField_2 = new JTextField();
  554. textField_2.setBounds(162, 138, 138, 20);
  555. panel_8.add(textField_2);
  556. textField_2.setColumns(10);
  557.  
  558. JLabel lblNewLabel_3 = new JLabel("Enter ISBN");
  559. lblNewLabel_3.setBounds(38, 141, 114, 14);
  560. panel_8.add(lblNewLabel_3);
  561.  
  562. JLabel lblNewLabel_4 = new JLabel("Confirm Order");
  563. lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
  564. lblNewLabel_4.setBounds(177, 73, 156, 14);
  565. panel_8.add(lblNewLabel_4);
  566.  
  567. JButton btnNewButton_5 = new JButton("Confirm");
  568. btnNewButton_5.setBounds(177, 207, 89, 23);
  569. panel_8.add(btnNewButton_5);
  570.  
  571. JLabel msg = new JLabel("");
  572. msg.setBounds(96, 303, 304, 14);
  573. panel_8.add(msg);
  574. panel_5.setLayout(null);
  575.  
  576. JPanel panel_9 = new JPanel();
  577. panel_9.setBounds(55, 92, 10, 10);
  578. panel.add(panel_9, "addbooks");
  579. panel_9.setLayout(null);
  580.  
  581. JButton btnNewButton_6 = new JButton("Back");
  582. btnNewButton_6.addActionListener(new ActionListener() {
  583. public void actionPerformed(ActionEvent e) {
  584. card.show(panel, "operations");
  585. }
  586. });
  587. btnNewButton_6.setBounds(0, 11, 89, 23);
  588. panel_9.add(btnNewButton_6);
  589.  
  590. threshold = new JTextField();
  591. threshold.setBounds(238, 258, 170, 20);
  592. panel_9.add(threshold);
  593. threshold.setColumns(10);
  594.  
  595. EnterISBN = new JTextField();
  596. EnterISBN.setBounds(21, 92, 170, 20);
  597. panel_9.add(EnterISBN);
  598. EnterISBN.setColumns(10);
  599.  
  600. JLabel lblNewLabel_5 = new JLabel("");
  601. lblNewLabel_5.setForeground(Color.RED);
  602. lblNewLabel_5.setBackground(Color.WHITE);
  603. lblNewLabel_5.setBounds(347, 58, 127, 14);
  604. panel_9.add(lblNewLabel_5);
  605.  
  606. title = new JTextField();
  607. title.setBounds(238, 92, 170, 20);
  608. panel_9.add(title);
  609. title.setColumns(10);
  610.  
  611. Publisher = new JTextField();
  612. Publisher.setBounds(21, 148, 170, 20);
  613. panel_9.add(Publisher);
  614. Publisher.setColumns(10);
  615.  
  616. Year = new JTextField();
  617. Year.setBounds(238, 148, 170, 20);
  618. panel_9.add(Year);
  619. Year.setColumns(10);
  620.  
  621. price = new JTextField();
  622. price.setBounds(21, 202, 170, 20);
  623. panel_9.add(price);
  624. price.setColumns(10);
  625.  
  626. category = new JTextField();
  627. category.setBounds(238, 202, 170, 20);
  628. panel_9.add(category);
  629. category.setColumns(10);
  630.  
  631. copies = new JTextField();
  632. copies.setBounds(21, 258, 170, 20);
  633. panel_9.add(copies);
  634. copies.setColumns(10);
  635.  
  636. JLabel lblNewLabel_6 = new JLabel("Insert Book");
  637. lblNewLabel_6.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 14));
  638. lblNewLabel_6.setBounds(216, 40, 176, 14);
  639. panel_9.add(lblNewLabel_6);
  640.  
  641. JLabel lblNewLabel_7 = new JLabel("Enter ISBN");
  642. lblNewLabel_7.setBounds(28, 67, 114, 14);
  643. panel_9.add(lblNewLabel_7);
  644.  
  645. JLabel lblNewLabel_8 = new JLabel("Title");
  646. lblNewLabel_8.setBounds(274, 67, 79, 14);
  647. panel_9.add(lblNewLabel_8);
  648.  
  649. JLabel lblNewLabel_9 = new JLabel("Publisher");
  650. lblNewLabel_9.setBounds(31, 121, 114, 14);
  651. panel_9.add(lblNewLabel_9);
  652.  
  653. JLabel year = new JLabel("Publication year");
  654. year.setBounds(274, 123, 147, 14);
  655. panel_9.add(year);
  656.  
  657. JLabel lblNewLabel_11 = new JLabel("Selling price");
  658. lblNewLabel_11.setBounds(31, 177, 114, 14);
  659. panel_9.add(lblNewLabel_11);
  660.  
  661. JLabel lblNewLabel_12 = new JLabel("Category");
  662. lblNewLabel_12.setBounds(274, 179, 114, 14);
  663. panel_9.add(lblNewLabel_12);
  664.  
  665. JLabel lblNewLabel_13 = new JLabel("Copies");
  666. lblNewLabel_13.setBounds(31, 233, 79, 14);
  667. panel_9.add(lblNewLabel_13);
  668.  
  669. JLabel lblNewLabel_14 = new JLabel("Threshold");
  670. lblNewLabel_14.setBounds(271, 233, 102, 14);
  671. panel_9.add(lblNewLabel_14);
  672.  
  673. JButton btnNewButton_7 = new JButton("Insert");
  674. btnNewButton_7.addActionListener(new ActionListener() {
  675. public void actionPerformed(ActionEvent e) {
  676. if (checkInsert()) {
  677. publisherData();
  678. ArrayList<String> attr = new ArrayList();
  679. ArrayList<String> value = new ArrayList();
  680. attr.add("ISBN");
  681. attr.add("Title");
  682. attr.add("Publisher");
  683. attr.add("Publication year");
  684. attr.add("Price");
  685. attr.add("Category");
  686. attr.add("Copies");
  687. attr.add("Threshold");
  688. value.add(EnterISBN.getText());
  689. value.add(title.getText());
  690. value.add(Publisher.getText());
  691. value.add(Year.getText());
  692. value.add(price.getText());
  693. value.add(category.getText());
  694. value.add(copies.getText());
  695. value.add(threshold.getText());
  696. String query = insertINtoTable("Book", attr, value);
  697.  
  698. try {
  699. stt.execute(query);
  700.  
  701. String kk[] = authorList.getText().split("\n");
  702. String query2 = "";
  703. ArrayList<String> attr1;
  704. ArrayList<String> value1;
  705. for (int i = 0; i < kk.length; i++) {
  706. attr1 = new ArrayList();
  707. value1 = new ArrayList();
  708. attr1.add("ISBN");
  709. attr1.add("Author");
  710. value1.add(EnterISBN.getText());
  711. value1.add(kk[i]);
  712. query2 = insertINtoTable("Authors", attr1, value1);
  713.  
  714. try {
  715. stt.execute(query2);
  716. } catch (SQLException e1) {
  717. // TODO Auto-generated // catch // block //
  718. // e1.printStackTrace();
  719. }
  720. System.out.println(query2);
  721. }
  722. } catch (SQLException e1) {
  723. JOptionPane.showConfirmDialog(null, "duplicate ISBN");
  724.  
  725. // TODO Auto-generated
  726. // e1.printStackTrace();
  727. }
  728.  
  729. }
  730.  
  731. }
  732. });
  733. btnNewButton_7.setBounds(200, 384, 89, 23);
  734. panel_9.add(btnNewButton_7);
  735. authorList = new JTextPane();
  736. authorList.setEditable(false);
  737. JScrollPane scrollPane = new JScrollPane();
  738. scrollPane.setBounds(21, 312, 429, 71);
  739. panel_9.add(scrollPane);
  740. scrollPane.setViewportView(authorList);
  741. JButton btnNewButton_9 = new JButton("insert author");
  742. btnNewButton_9.addActionListener(new ActionListener() {
  743. public void actionPerformed(ActionEvent e) {
  744. JTextField author = new JTextField();
  745. int option = JOptionPane.showConfirmDialog(null, author,
  746. "search", JOptionPane.OK_CANCEL_OPTION);
  747. if (option == JOptionPane.OK_OPTION
  748. && !author.getText().equals("")) {
  749. if (!authorList.getText().equals(""))
  750. authorList.setText(authorList.getText() + "\n"
  751. + author.getText());
  752. else
  753. authorList.setText(author.getText());
  754. }
  755.  
  756. }
  757. });
  758. btnNewButton_9.setBounds(168, 285, 121, 23);
  759. panel_9.add(btnNewButton_9);
  760.  
  761. JPanel panel_6 = new JPanel();
  762. panel_6.setBounds(0, 0, 432, 419);
  763. panel.add(panel_6, "search/update");
  764. btnLogin.addActionListener(new ActionListener() {
  765. public void actionPerformed(ActionEvent arg0) {
  766. if (validateEmpty(Username.getText())) {
  767. if (validateEmpty(password.getText())) {
  768. // search username and password in your database
  769. // if found do this
  770. card.show(panel, "operations");
  771. // else show this message
  772. JOptionPane.showMessageDialog(frame,
  773. "Invalid username or password !",
  774. "Inane warning", JOptionPane.WARNING_MESSAGE);
  775. } else {
  776. JOptionPane.showMessageDialog(frame,
  777. "Invalid Password", "Inane error",
  778. JOptionPane.ERROR_MESSAGE);
  779. }
  780. } else {
  781. JOptionPane.showMessageDialog(frame, "Invalid Username",
  782. "Inane error", JOptionPane.ERROR_MESSAGE);
  783. }
  784. }
  785. });
  786. panel_6.setLayout(null);
  787.  
  788. JLabel lblSearchBy = new JLabel("search by");
  789. lblSearchBy.setBounds(10, 39, 65, 14);
  790. panel_6.add(lblSearchBy);
  791.  
  792. JButton btnNewButton_8 = new JButton("back");
  793. btnNewButton_8.addActionListener(new ActionListener() {
  794. public void actionPerformed(ActionEvent arg0) {
  795. card.show(panel, "operations");
  796. }
  797. });
  798. btnNewButton_8.setBounds(0, 5, 89, 23);
  799. panel_6.add(btnNewButton_8);
  800.  
  801. table = new JTable();
  802. Object rows[][] = new Object[][] { { "ISBN", "Title", "Publisher",
  803. "Year", "Price", "Categ", "#Copy", "Threshold" },
  804. // { null, null, null, null, null, null, null, null },
  805. // { null, null, null, null, null, null, null, null },
  806. // { null, null, null, null, null, null, null, null },
  807. // { null, null, null, null, null, null, null, null },
  808. // { null, null, null, null, null, null, null, null },
  809. // { null, null, null, null, null, null, null, null },
  810. // { null, null, null, null, null, null, null, null },
  811. // { null, null, null, null, null, null, null, null },
  812. // { null, null, null, null, null, null, null, null },
  813. // { null, null, null, null, null, null, null, null },
  814. // { null, null, null, null, null, null, null, null },
  815. // { null, null, null, null, null, null, null, null },
  816. // { null, null, null, null, null, null, null, null },
  817. // { null, null, null, null, null, null, null, null },
  818. // { null, null, null, null, null, null, null, null },
  819. // { null, null, null, null, null, null, null, null },
  820. // { null, null, null, null, null, null, null, null },
  821. // { null, null, null, null, null, null, null, null },
  822. // { null, null, null, null, null, null, null, null },
  823. };
  824.  
  825. table.setModel(new DefaultTableModel(rows, new String[] { "ISBN",
  826. "Title", "Publisher", "Year", "Price", "Categ", "#Copy",
  827. "Threshold" }) {
  828. boolean[] columnEditables = new boolean[] { false, false, false,
  829. false, false, false, false, false };
  830.  
  831. public boolean isCellEditable(int row, int column) {
  832. return columnEditables[column];
  833. }
  834. });
  835.  
  836. table.setBounds(10, 99, 512, 320);
  837. panel_6.add(table);
  838. model = (DefaultTableModel) table.getModel();
  839. table.setModel(model);
  840. // model.addRow( new Object[] { "ISBN555", "Title", "Publisher", "Year",
  841. // "Price",
  842. // "Categ", "#Copy", "Threshold" });
  843.  
  844. JRadioButton rdbtnIsbnAndTiltle = new JRadioButton("ISBN and Tiltle");
  845. rdbtnIsbnAndTiltle.addActionListener(new ActionListener() {
  846. public void actionPerformed(ActionEvent e) {
  847.  
  848. JTextField ISBN = new JTextField();
  849. JTextField Title = new JTextField();
  850. Object[] message = { "ISBN:", ISBN, "Title:", Title };
  851.  
  852. int option = JOptionPane.showConfirmDialog(null, message,
  853. "search", JOptionPane.OK_CANCEL_OPTION);
  854. while (option == JOptionPane.OK_OPTION
  855. && (Title.getText().equals("") || ISBN.getText()
  856. .equals(""))) {
  857. option = JOptionPane.showConfirmDialog(null, message,
  858. "Publisher", JOptionPane.OK_CANCEL_OPTION);
  859. }
  860. if (option == JOptionPane.OK_OPTION) {
  861. try {
  862. ResultSet res = stt
  863. .executeQuery("SELECT * FROM book WHERE `ISBN` LIKE '"
  864. + ISBN.getText()
  865. + "' AND `Title` LIKE '"
  866. + Title.getText() + "'");
  867. printSearch(res);
  868. } catch (Exception ex) {
  869. ex.printStackTrace();
  870. }
  871. }
  872. }
  873. });
  874. rdbtnIsbnAndTiltle.setBounds(66, 35, 115, 23);
  875. panel_6.add(rdbtnIsbnAndTiltle);
  876.  
  877. JRadioButton rdbtnCategory = new JRadioButton("Category");
  878. rdbtnCategory.addActionListener(new ActionListener() {
  879. public void actionPerformed(ActionEvent arg0) {
  880. JTextField catg = new JTextField();
  881. int option = JOptionPane.showConfirmDialog(null, catg,
  882. "search", JOptionPane.OK_CANCEL_OPTION);
  883. if (option == JOptionPane.OK_OPTION) {
  884. try {
  885. ResultSet res = stt
  886. .executeQuery("SELECT * FROM book WHERE `Category` LIKE '"
  887. + catg.getText()
  888. + "'");
  889. printSearch(res);
  890. } catch (Exception ex) {
  891. ex.printStackTrace();
  892. }
  893. }
  894. }
  895. });
  896. rdbtnCategory.setBounds(178, 35, 83, 23);
  897. panel_6.add(rdbtnCategory);
  898.  
  899. JRadioButton rdbtnAuthor = new JRadioButton("author");
  900. rdbtnAuthor.addActionListener(new ActionListener() {
  901. public void actionPerformed(ActionEvent arg0) {
  902. JTextField author = new JTextField();
  903. int option = JOptionPane.showConfirmDialog(null, author,
  904. "search", JOptionPane.OK_CANCEL_OPTION);
  905. if (option == JOptionPane.OK_OPTION) {
  906. try {
  907. ResultSet res = stt
  908. .executeQuery("SELECT * FROM book AS b, authors AS a WHERE b.`ISBN`=a.`ISBN` AND a.`Author` LIKE '"
  909. + author.getText()
  910. + "'");
  911. printSearch(res);
  912. } catch (Exception ex) {
  913. ex.printStackTrace();
  914. }
  915. }
  916. }
  917. });
  918. rdbtnAuthor.setBounds(257, 35, 65, 23);
  919. panel_6.add(rdbtnAuthor);
  920.  
  921. JRadioButton rdbtnPublisher = new JRadioButton("publisher");
  922. rdbtnPublisher.addActionListener(new ActionListener() {
  923. public void actionPerformed(ActionEvent arg0) {
  924. JTextField pub = new JTextField();
  925. int option = JOptionPane.showConfirmDialog(null, pub,
  926. "search", JOptionPane.OK_CANCEL_OPTION);
  927. if (option == JOptionPane.OK_OPTION) {
  928. try {
  929. ResultSet res = stt
  930. .executeQuery("SELECT * FROM book AS b, Publisher AS a WHERE b.`ISBN`=a.`ISBN` AND a.`Publisher` LIKE '"
  931. + pub.getText()
  932. + "'");
  933. printSearch(res);
  934. } catch (Exception ex) {
  935. ex.printStackTrace();
  936. }
  937. }
  938. }
  939. });
  940. rdbtnPublisher.setBounds(324, 35, 94, 23);
  941. panel_6.add(rdbtnPublisher);
  942.  
  943. JRadioButton rdbtnCopies = new JRadioButton("copies");
  944. rdbtnCopies.addActionListener(new ActionListener() {
  945. public void actionPerformed(ActionEvent arg0) {
  946. JTextField copy = new JTextField();
  947. int option = JOptionPane.showConfirmDialog(null, copy,
  948. "search", JOptionPane.OK_CANCEL_OPTION);
  949. if (option == JOptionPane.OK_OPTION) {
  950. try {
  951. ResultSet res = stt
  952. .executeQuery("SELECT * FROM book WHERE `Copies` = '"
  953. + copy.getText()
  954. + "'");
  955. printSearch(res);
  956. } catch (Exception ex) {
  957. ex.printStackTrace();
  958. }
  959. }
  960. }
  961. });
  962. rdbtnCopies.setBounds(414, 35, 77, 23);
  963. panel_6.add(rdbtnCopies);
  964.  
  965. ButtonGroup group = new ButtonGroup();
  966. group.add(rdbtnIsbnAndTiltle);
  967. group.add(rdbtnCategory);
  968. group.add(rdbtnAuthor);
  969. group.add(rdbtnPublisher);
  970. group.add(rdbtnCopies);
  971.  
  972.  
  973. JButton btnNewButton_41 = new JButton("Update");
  974. btnNewButton_41.addActionListener(new ActionListener() {
  975. public void actionPerformed(ActionEvent arg0) {
  976. int selectedRowIndex =0;
  977. try{
  978.  
  979. selectedRowIndex=table.getSelectedRow();
  980. Object isbn = (Object) table.getModel().getValueAt(selectedRowIndex,0);
  981. // check search al 2wl
  982. // lma y3ml
  983. JTextField update = new JTextField();
  984. int option = JOptionPane.showConfirmDialog(null, update,
  985. "update", JOptionPane.OK_CANCEL_OPTION);
  986. if (option == JOptionPane.OK_OPTION) {
  987.  
  988. }
  989. }catch(Exception e){
  990. JOptionPane.showMessageDialog(null,"select a book please before update" );
  991. }
  992.  
  993. }
  994. });
  995. btnNewButton_41.setBounds(182, 72, 94, 23);
  996. panel_6.add(btnNewButton_41);
  997.  
  998. }
  999. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement