Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. import java.awt.Container;
  2.  
  3. import java.awt.GridLayout;
  4.  
  5. import java.awt.event.ActionEvent;
  6.  
  7. import java.awt.event.ActionListener;
  8.  
  9. import java.sql.Connection;
  10.  
  11. import java.sql.DriverManager;
  12.  
  13. import java.sql.ResultSet;
  14.  
  15. import java.sql.Statement;
  16.  
  17.  
  18.  
  19. import javax.swing.BoxLayout;
  20.  
  21. import javax.swing.JButton;
  22.  
  23. import javax.swing.JFrame;
  24.  
  25. import javax.swing.JLabel;
  26.  
  27. import javax.swing.JOptionPane;
  28.  
  29. import javax.swing.JPanel;
  30.  
  31. import javax.swing.JTextField;
  32.  
  33.  
  34.  
  35. public class Menu implements ActionListener {
  36.  
  37.  
  38. JLabel lblId,lblProduct,lblDescription,lblPrice,lblCategory,lblI,lblT,lblD,lblP,lblC,
  39.  
  40. lblIVal,lblTVal,lblDVal,lblPVal,lblCVal;
  41. //id integer Primary key,title varchar(255), description varchar(255), price varchar(255), category varchar(255)";
  42. JTextField txtId,txtTitle,txtDescription,txtPrice,txtCategory;
  43.  
  44. JButton btnAdd,btnPrev,btnNext;
  45.  
  46. ResultSet rs;
  47. private final JButton btnList_1 = new JButton(" List");
  48.  
  49. public static void main(String[] args) {
  50.  
  51. Menu obj = new Menu();
  52.  
  53. obj.createUI();
  54.  
  55. obj.fetch();
  56. try {
  57. obj.populateValue();
  58. } catch (Exception e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. private void fetch()
  65. {
  66. try
  67. {
  68. Class.forName("com.mysql.jdbc.Driver");
  69.  
  70. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/veer","root","");
  71.  
  72.  
  73. String sql = "SELECT * FROM Products";
  74.  
  75.  
  76.  
  77. Statement st=((java.sql.Connection) con).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  78.  
  79. ResultSet.CONCUR_UPDATABLE);
  80.  
  81.  
  82. rs = st.executeQuery(sql);
  83. rs.next();
  84.  
  85. }
  86. catch(Exception e)
  87. {
  88. System.out.println(e);
  89. }
  90. }
  91.  
  92. private void createUI()
  93.  
  94. {
  95.  
  96. JFrame frame = new JFrame("Products details ");
  97.  
  98.  
  99.  
  100. JPanel pnlInput = new JPanel(new GridLayout(5,2));
  101.  
  102.  
  103.  
  104. lblId = new JLabel(" Id : ");
  105.  
  106. txtId = new JTextField(15);
  107.  
  108.  
  109.  
  110. lblProduct = new JLabel(" Product : ");
  111.  
  112. txtTitle = new JTextField();
  113.  
  114.  
  115.  
  116. lblDescription = new JLabel(" Description : ");
  117.  
  118. txtDescription = new JTextField();
  119.  
  120.  
  121.  
  122. lblPrice = new JLabel(" Price : ");
  123.  
  124. txtPrice = new JTextField();
  125.  
  126.  
  127. lblCategory = new JLabel(" Category : ");
  128.  
  129. txtCategory = new JTextField();
  130.  
  131.  
  132. pnlInput.add(lblId);
  133.  
  134. pnlInput.add(txtId);
  135.  
  136.  
  137.  
  138. pnlInput.add(lblProduct);
  139.  
  140. pnlInput.add(txtTitle);
  141.  
  142.  
  143.  
  144. pnlInput.add(lblDescription);
  145.  
  146. pnlInput.add(txtDescription);
  147.  
  148.  
  149.  
  150. pnlInput.add(lblPrice);
  151.  
  152. pnlInput.add(txtPrice);
  153.  
  154. pnlInput.add(lblCategory);
  155.  
  156. pnlInput.add(txtCategory);
  157.  
  158.  
  159.  
  160. JPanel pnlButton = new JPanel(new GridLayout(1,3));
  161.  
  162.  
  163.  
  164. btnAdd = new JButton("Add");
  165.  
  166. btnAdd.addActionListener(this);
  167.  
  168.  
  169.  
  170. // btnUpdate = new JButton("Update");
  171. //
  172. // btnUpdate.addActionListener(this);
  173. //
  174. //
  175. //
  176. // btnList = new JButton("Delete");
  177. //
  178. // btnList.addActionListener(this);
  179. //
  180. //
  181.  
  182. pnlButton.add(btnAdd);
  183.  
  184. // pnlButton.add(btnUpdate);
  185.  
  186. // pnlButton.add(btnList);
  187.  
  188.  
  189.  
  190. JPanel pnlNavigate = new JPanel(new GridLayout(1,2));
  191.  
  192. btnPrev = new JButton(" << ");
  193.  
  194. btnPrev.setActionCommand("Prev");
  195.  
  196. btnPrev.addActionListener(this);
  197.  
  198.  
  199.  
  200. btnNext = new JButton(" >> ");
  201.  
  202. btnNext.setActionCommand("Next");
  203.  
  204. btnNext.addActionListener(this);
  205.  
  206.  
  207.  
  208. pnlNavigate.add(btnPrev);
  209.  
  210. pnlNavigate.add(btnNext);
  211.  
  212.  
  213.  
  214. JPanel pnlNavAns = new JPanel(new GridLayout(5,2));
  215.  
  216.  
  217.  
  218. lblI = new JLabel(" Id : ");
  219.  
  220. lblIVal = new JLabel("Val");
  221.  
  222.  
  223.  
  224. lblT = new JLabel(" Product : ");
  225.  
  226. lblTVal = new JLabel("Val");
  227.  
  228.  
  229.  
  230. lblD = new JLabel(" Description : ");
  231.  
  232. lblDVal = new JLabel("Val");
  233.  
  234.  
  235.  
  236. lblP = new JLabel(" Price : ");
  237.  
  238. lblPVal = new JLabel("Val");
  239.  
  240.  
  241.  
  242. lblC = new JLabel(" Category : ");
  243.  
  244. lblCVal = new JLabel("Val");
  245.  
  246.  
  247.  
  248. pnlNavAns.add(lblI);
  249. pnlNavAns.add(lblIVal);
  250.  
  251.  
  252.  
  253. pnlNavAns.add(lblT);
  254.  
  255. pnlNavAns.add(lblTVal);
  256.  
  257.  
  258.  
  259. pnlNavAns.add(lblD);
  260.  
  261. pnlNavAns.add(lblDVal);
  262.  
  263.  
  264.  
  265. pnlNavAns.add(lblP);
  266.  
  267. pnlNavAns.add(lblPVal);
  268.  
  269. pnlNavAns.add(lblC);
  270.  
  271. pnlNavAns.add(lblCVal);
  272.  
  273.  
  274.  
  275.  
  276.  
  277. Container cn = frame.getContentPane();
  278.  
  279. cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));
  280.  
  281.  
  282.  
  283. frame.getContentPane().add(pnlInput);
  284. frame.getContentPane().add(pnlButton);
  285. btnList_1.addActionListener(new ActionListener() {
  286. public void actionPerformed(ActionEvent arg0) {
  287.  
  288. List List = new List();
  289. List.setVisible(true);
  290. }
  291. });
  292. pnlButton.add(btnList_1);
  293.  
  294. frame.getContentPane().add(pnlNavAns);
  295.  
  296. frame.getContentPane().add(pnlNavigate);
  297.  
  298.  
  299.  
  300. //If this will not be written, the only frame will be closed
  301.  
  302. // but the application will be active.
  303.  
  304. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  305.  
  306. frame.pack();
  307. frame.setVisible(true);
  308.  
  309. }
  310.  
  311. @Override
  312.  
  313. public void actionPerformed(ActionEvent evt) {
  314.  
  315.  
  316.  
  317. String action = evt.getActionCommand();
  318.  
  319. if(action.equals("Add"))
  320.  
  321. {
  322.  
  323. addOperation();
  324.  
  325. }
  326. // else if(action.equals("Update"))
  327. //
  328. // {
  329. //
  330. // updateOperation();
  331. //
  332. // }else if(action.equals("Delete"))
  333. //
  334. // {
  335. //
  336. // deleteOperation();
  337. //
  338. // }
  339. // else
  340. if(action.equals("Prev"))
  341.  
  342. {
  343.  
  344. preNavigation();
  345.  
  346. }else if(action.equals("Next"))
  347.  
  348. {
  349.  
  350. nextNavigation();
  351.  
  352. }
  353.  
  354. }
  355.  
  356. private void addOperation()
  357.  
  358. {
  359.  
  360. try
  361.  
  362. {
  363.  
  364. //Load Jdbc Odbc Driver
  365.  
  366. Class.forName("com.mysql.jdbc.Driver");
  367.  
  368. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/veer","root","");
  369.  
  370. String sql="CREATE TABLE IF NOT EXISTS Products(id integer Primary key,title varchar(255), description varchar(255), price int, category varchar(255)";
  371.  
  372.  
  373.  
  374. String sql1 = "INSERT INTO Products (id,title,description,price,category) " +
  375.  
  376. "Values ('"+txtId.getText()+"'," +
  377.  
  378. "'"+txtTitle.getText()+"'," +
  379.  
  380. "'"+txtDescription.getText()+"'," +
  381.  
  382. "'"+txtPrice.getText()+"'," +
  383.  
  384. "'"+txtCategory.getText()+"')";
  385.  
  386.  
  387.  
  388. Statement st=((java.sql.Connection) con).createStatement();
  389. st.execute(sql1);
  390.  
  391.  
  392.  
  393. JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
  394.  
  395. JOptionPane.INFORMATION_MESSAGE);
  396.  
  397. clearControls();
  398.  
  399. }catch(Exception e)
  400.  
  401. {
  402.  
  403. JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  404.  
  405. JOptionPane.ERROR_MESSAGE);
  406.  
  407. }
  408. fetch();
  409. }
  410.  
  411. // private void updateOperation()
  412. //
  413. // {
  414. //
  415. // try
  416. //
  417. // {
  418. //
  419. // //Load Jdbc Odbc Driver
  420. //
  421. // Class.forName("com.mysql.jdbc.Driver");
  422. //
  423. // Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/veer","root","");
  424. //
  425. //
  426. //
  427. // String sql = "Update Products " +
  428. //
  429. // "SET Title = '"+txtTitle.getText()+"'," +
  430. //
  431. // " Description = '"+txtDescription.getText()+"'," +
  432. //
  433. // " Price = '"+txtPrice.getText()+"'," +
  434. //
  435. // " Category = '"+txtCategory.getText()+"'" +
  436. //
  437. // "Where Id = '"+txtId.getText()+"'";
  438. //
  439. //
  440. //
  441. // JOptionPane.showMessageDialog(null, sql,"Record Updated",
  442. //
  443. // JOptionPane.INFORMATION_MESSAGE);
  444. //
  445. // Statement st=((java.sql.Connection) con).createStatement();
  446. //
  447. // st.execute(sql);
  448. //
  449. //
  450. //
  451. // JOptionPane.showMessageDialog(null, "Record Update Succesfully.",
  452. //
  453. // "Record Updated",JOptionPane.INFORMATION_MESSAGE);
  454. //
  455. // clearControls();
  456. //
  457. // }catch(Exception e)
  458. // {
  459. //
  460. // JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  461. //
  462. // JOptionPane.ERROR_MESSAGE);
  463. //
  464. // }
  465. // fetch();
  466. //
  467. // }
  468.  
  469. // private void deleteOperation()
  470. //
  471. // {
  472. //
  473. // int ans = JOptionPane.showConfirmDialog(null,
  474. //
  475. // "Are you sure to delete the Record ?", "Delete Record",
  476. //
  477. // JOptionPane.YES_NO_OPTION);
  478. //
  479. // if(ans == JOptionPane.YES_OPTION)
  480. //
  481. // {
  482. //
  483. // try{
  484. //
  485. // //Load Jdbc Odbc Driver
  486. //
  487. // Class.forName("com.mysql.jdbc.Driver");
  488. //
  489. // Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/veer","root","");
  490. //
  491. //
  492. // String sql = "Delete FROM Products where Id = '"+txtId.getText()+"'";
  493. //
  494. //
  495. //
  496. // Statement st=((java.sql.Connection) con).createStatement();
  497. //
  498. //
  499. // st.execute(sql);
  500. // }catch(Exception e)
  501. //
  502. // {
  503. //
  504. // JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  505. //
  506. // JOptionPane.ERROR_MESSAGE);
  507. //
  508. // }
  509. //
  510. // JOptionPane.showMessageDialog(null, "Record Deleted","Success",
  511. //
  512. // JOptionPane.INFORMATION_MESSAGE);
  513. //
  514. // }
  515. //
  516. // else
  517. //
  518. // {
  519. //
  520. // JOptionPane.showMessageDialog(null, "Operation Canceled","Cancel",
  521. //
  522. // JOptionPane.INFORMATION_MESSAGE);
  523. //
  524. // }
  525. // fetch();
  526. //
  527. // }
  528.  
  529. private void preNavigation()
  530.  
  531. {
  532.  
  533. try{
  534.  
  535.  
  536. if(rs.previous())
  537.  
  538. {
  539.  
  540. populateValue();
  541.  
  542. }
  543.  
  544. }catch(Exception e)
  545.  
  546. {
  547.  
  548. JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  549.  
  550. JOptionPane.ERROR_MESSAGE);
  551.  
  552. }
  553.  
  554. }
  555.  
  556. private void nextNavigation()
  557.  
  558. {
  559.  
  560. try{
  561.  
  562.  
  563. if(rs.next())
  564.  
  565. {
  566.  
  567. populateValue();
  568.  
  569. }
  570.  
  571. }catch(Exception e)
  572.  
  573. {
  574.  
  575. JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  576.  
  577. JOptionPane.ERROR_MESSAGE);
  578.  
  579. }
  580.  
  581. }
  582.  
  583. private void populateValue() throws Exception
  584.  
  585. {
  586.  
  587. String Id = rs.getString("id");
  588.  
  589. String Product = rs.getString("title");
  590.  
  591. String Description = rs.getString("description");
  592.  
  593. String Price = rs.getString("price");
  594.  
  595. String Category = rs.getString("category");
  596.  
  597.  
  598.  
  599. lblIVal.setText(Id);
  600.  
  601. lblTVal.setText(Product);
  602.  
  603. lblDVal.setText(Description);
  604.  
  605. lblPVal.setText(Price);
  606.  
  607. lblCVal.setText(Category);
  608.  
  609.  
  610.  
  611. // txtId.setText(Id);
  612. //
  613. // txtTitle.setText(Product);
  614. //
  615. // txtDescription.setText(Description);
  616. //
  617. // txtPrice.setText(Price);
  618. //
  619. // txtCategory.setText(Category);
  620.  
  621. }
  622.  
  623. private void clearControls()
  624. {
  625.  
  626. txtId.setText("");
  627.  
  628. txtTitle.setText("");
  629.  
  630. txtDescription.setText("");
  631.  
  632. txtPrice.setText("");
  633.  
  634. txtCategory.setText("");
  635.  
  636. }
  637.  
  638. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement