Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. public class ProductFrame extends javax.swing.JFrame {
  2.  
  3. /**
  4. * Creates new form ProductFrame
  5. */
  6. public ProductFrame() {
  7. initComponents();
  8. Show_Products_In_JTable();
  9.  
  10.  
  11. }
  12.  
  13. public Connection getConnection()
  14. {
  15.  
  16. Connection con = null;
  17. try {
  18. con = DriverManager.getConnection("jdbc:mysql://localhost/smart-mart","root","");
  19. return con;
  20. } catch (SQLException ex) {
  21. Logger.getLogger(ProductFrame.class.getName()).log(Level.SEVERE, null, ex);
  22. return null;
  23. }
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. public boolean checkInputs()
  31. {
  32. if(
  33. txt_name.getText() == null
  34. || txt_price.getText() == null
  35. || txt_quantity.getText() == null
  36. || txt_arrdate.getText() == null
  37. || txt_minreq.getText() == null
  38. || txt_maxreq.getText() == null
  39. || txt_staff.getText() == null
  40.  
  41. ){
  42. return false;
  43. }
  44. else{
  45. try{
  46. Float.parseFloat(txt_price.getText());
  47. return true;
  48. }catch(Exception ex)
  49. {
  50. return false;
  51. }
  52. }
  53. }
  54.  
  55. //** Display Date in the ItemTable ** //
  56. //** Step 1- Create an arraylist and fill it with date **//
  57.  
  58. public ArrayList<Product> getProductList ()
  59. {
  60. ArrayList<Product> productList = new ArrayList<Product>();
  61. Connection con = getConnection();
  62. String query = "SELECT * FROM product";
  63.  
  64. Statement st;
  65. ResultSet rs;
  66.  
  67. try {
  68. st = con.createStatement();
  69. rs = st.executeQuery(query);
  70. Product product;
  71.  
  72.  
  73. Show_Products_In_JTable();
  74.  
  75.  
  76.  
  77. while (rs.next())
  78. {
  79. product = new Product (rs.getInt("id"),rs.getString("name"), Float.parseFloat(rs.getString("price")),rs.getInt("quantity"), rs.getString("arrdate"), rs.getInt("minreq"),rs.getInt("maxreq"), rs.getString("staff"));
  80. productList.add(product);
  81. con.close();
  82. }
  83. } catch (SQLException ex) {
  84. Logger.getLogger(ProductFrame.class.getName()).log(Level.SEVERE, null, ex);
  85.  
  86. }
  87. return productList;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. //** Stept 2- Populate the table ** //
  96.  
  97.  
  98.  
  99. public void Show_Products_In_JTable()
  100. {
  101. ArrayList<Product> list = getProductList();
  102. DefaultTableModel model = (DefaultTableModel) J_Table_Products.getModel();
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. Object[] row = new Object [8];
  110. for(int i = 0; i<list.size();i++)
  111. {
  112. row[0]=list.get(i).getId();
  113. row[1]=list.get(i).getName();
  114. row[2]=list.get(i).getPrice();
  115. row[3]=list.get(i).getQuantity();
  116. row[4]=list.get(i).getArrDate();
  117. row[5]=list.get(i).getMinReq();
  118. row[6]=list.get(i).getMaxReq();
  119. row[7]=list.get(i).getStaff();
  120.  
  121. model.addRow(row);
  122.  
  123.  
  124. }
  125. }
  126.  
  127. // Show Data In Inputs
  128. public void ShowItem(int index)
  129. {
  130. txt_id.setText(Integer.toString(getProductList().get(index).getId()));
  131. txt_name.setText(getProductList().get(index).getName());
  132. txt_price.setText(Float.toString(getProductList().get(index).getPrice()));
  133. txt_quantity.setText(Integer.toString(getProductList().get(index).getQuantity()));
  134. txt_arrdate.setText(getProductList().get(index).getArrDate());
  135. txt_minreq.setText(Integer.toString(getProductList().get(index).getMinReq()));
  136. txt_maxreq.setText(Integer.toString(getProductList().get(index).getMaxReq()));
  137. txt_staff.setText(getProductList().get(index).getStaff());
  138. }
  139.  
  140. private void btn_insertActionPerformed(java.awt.event.ActionEvent evt) {
  141. if (checkInputs() && txt_name !=null){
  142.  
  143. try {
  144. Connection con = getConnection();
  145. PreparedStatement ps;
  146. ps = con.prepareStatement("INSERT INTO product(name, price, quantity, arrdate, minreq, maxreq, staff)"+"values(?,?,?,?,?,?,?)");
  147. ps.setString(1, txt_name.getText());
  148. ps.setString(2, txt_price.getText());
  149. ps.setString(3, txt_quantity.getText());
  150. ps.setString(4, txt_arrdate.getText());
  151. ps.setString(5 , txt_minreq.getText());
  152. ps.setString(6 , txt_maxreq.getText());
  153. ps.setString(7, txt_staff.getText());
  154.  
  155.  
  156.  
  157.  
  158. ps.executeUpdate();
  159.  
  160. Show_Products_In_JTable();
  161. JOptionPane.showMessageDialog(null, "Data Inserted");
  162.  
  163. ps.close();
  164. } catch (SQLException ex) {
  165. JOptionPane.showMessageDialog(null, ex.getMessage());
  166. }
  167.  
  168. }
  169. else{
  170. JOptionPane.showMessageDialog(null, "One or more field empty");
  171. }
  172.  
  173.  
  174. System.out.println("Name=>" + txt_name.getText());
  175. System.out.println("Price=>" + txt_price.getText());
  176. System.out.println("Quantity=>" + txt_quantity.getText());
  177. System.out.println("Arrdate=>" + txt_arrdate.getText());
  178. System.out.println("MinReq=>" + txt_minreq.getText());
  179. System.out.println("MaxReq=>" + txt_maxreq.getText());
  180. System.out.println("Staff=>" + txt_staff.getText());
  181.  
  182.  
  183.  
  184. }
  185.  
  186. private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {
  187. if (checkInputs() && txt_id.getText () !=null)
  188. {
  189. String UpdateQuery = null;
  190. PreparedStatement ps = null;
  191. Connection con = getConnection ();
  192.  
  193. try {
  194.  
  195.  
  196. UpdateQuery = "UPDATE product SET name=?, price=?,quantity=?, arrdate=?, minreq=?,maxreq=?, staff=? WHERE id=?";
  197. ps = con.prepareStatement(UpdateQuery);
  198. ps.setString(1, txt_name.getText());
  199. ps.setString(2, txt_price.getText());
  200. ps.setString(3, txt_quantity.getText());
  201. ps.setString(4, txt_arrdate.getText());
  202. ps.setString(5 , txt_minreq.getText());
  203. ps.setString(6 , txt_maxreq.getText());
  204. ps.setString(7, txt_staff.getText());
  205. ps.setInt(8, Integer.parseInt(txt_id.getText()));
  206.  
  207.  
  208. ps.executeUpdate();
  209. ps.close();
  210. } catch (SQLException ex) {
  211. Logger.getLogger(ProductFrame.class.getName()).log(Level.SEVERE, null, ex);
  212. }
  213. }else{
  214. JOptionPane.showMessageDialog(null, "One or more fields are Empty");
  215. }
  216. }
  217.  
  218. private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {
  219.  
  220. if (!txt_id.getText().equals(""))
  221.  
  222. {
  223.  
  224. try {
  225. Connection con = getConnection();
  226. PreparedStatement ps = con.prepareStatement("DELETE FROM product WHERE id = ?");
  227. int id = Integer.parseInt(txt_id.getText());
  228. ps.setInt(1, id);
  229. ps.executeUpdate();
  230.  
  231.  
  232. JOptionPane.showMessageDialog(null, "Item Deleted!");
  233. ps.close();
  234. } catch (SQLException ex) {
  235. Logger.getLogger(ProductFrame.class.getName()).log(Level.SEVERE, null, ex);
  236. JOptionPane.showMessageDialog(null, "Item Not Deleted!");
  237. ex.printStackTrace();
  238.  
  239. } finally {
  240.  
  241. }
  242.  
  243. }
  244. else{
  245. JOptionPane.showMessageDialog(null, " Product not found. Please enter the product ID!");
  246. }
  247.  
  248. }
  249.  
  250. private void J_Table_ProductsMouseClicked(java.awt.event.MouseEvent evt) {
  251. int index = J_Table_Products.getSelectedRow();
  252. ShowItem(index);
  253. }
  254.  
  255.  
  256.  
  257.  
  258. /**
  259. * @param args the command line arguments
  260. */
  261. public static void main(String args[]) {
  262. /* Set the Nimbus look and feel */
  263. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  264. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  265. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  266. */
  267. try {
  268. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  269. if ("Nimbus".equals(info.getName())) {
  270. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  271. break;
  272. }
  273. }
  274. } catch (ClassNotFoundException ex) {
  275. java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  276. } catch (InstantiationException ex) {
  277. java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  278. } catch (IllegalAccessException ex) {
  279. java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  280. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  281. java.util.logging.Logger.getLogger(ProductFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  282. }
  283. //</editor-fold>
  284.  
  285. /* Create and display the form */
  286. java.awt.EventQueue.invokeLater(new Runnable() {
  287. public void run() {
  288. new ProductFrame().setVisible(true);
  289. }
  290. });
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement