HarrJ

Day 29-23

Oct 17th, 2023
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. package weekDay27;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import javax.swing.JTable;
  8. import javax.swing.table.DefaultTableModel;
  9.  
  10. public class Day29G23MethodsCommit {
  11.     private String address = "jdbc:mysql://localhost:3306/db_mng_b8";
  12.     private String userName = "root";
  13.     private String passWord = "";
  14.    
  15.     public void getAllRows(JTable tableIn) {
  16.         DefaultTableModel tblModel = (DefaultTableModel) tableIn.getModel();
  17.         tblModel.setRowCount(0);
  18.         String sqlQuery = "SELECT * FROM tbl_infolist;";
  19.        
  20.         try {
  21.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  22.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  23.            
  24.             ResultSet rs = stmt.executeQuery();
  25.             //result set starts with 1 or field name
  26.             while (rs.next()) {
  27.                 Object[] newRow = {
  28.                     rs.getInt(1)
  29.                     ,rs.getString(2)
  30.                     ,rs.getString(3)
  31.                     ,rs.getInt(4)
  32.                     ,rs.getDouble(5)
  33.                     ,rs.getString(6)
  34.                     ,rs.getString(7)};
  35.                 tblModel.addRow(newRow);
  36.             }
  37.             conn.close();
  38.         } catch (Exception e) {
  39.             Object[] error = new Object[7];
  40.             error[0] = "Connection error:\n" + e.getMessage();
  41.             tblModel.addRow(error);
  42.         }
  43.     }
  44.    
  45.     public int recordRecieptAndOrder(int rcQty, double rcTotal, JTable tblOrderSource){
  46.         String resultCode = "";
  47.         int rowsAffected = 0;
  48.         boolean willCommit = true;
  49.         int rcID = 0;
  50.        
  51.         try {
  52.             String sqlQuery = "INSERT INTO tbl_g23_reciept(fld_rec_qty, fld_rec_qtytotal)"
  53.                     + " VALUE (?,?);";
  54.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  55.            
  56.             conn.setAutoCommit(false);
  57.            
  58.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  59.            
  60.             stmt.setInt(1, rcQty);
  61.             stmt.setDouble(2, rcTotal);
  62.            
  63.             rowsAffected = stmt.executeUpdate();
  64.            
  65.             if (rowsAffected != 1) {
  66.                 willCommit = false;
  67.                 resultCode+= "A";
  68.             }
  69.            
  70.             sqlQuery = "SELECT fld_recid FROM tbl_g23_reciept"
  71.                     + " WHERE fld_rec_qty = ? AND fld_rec_qtytotal = ?"
  72.                     + " ORDER BY fld_recid DESC;";
  73.            
  74.             stmt = conn.prepareStatement(sqlQuery);
  75.            
  76.             stmt.setInt(1, rcQty);
  77.             stmt.setDouble(2, rcTotal);
  78.            
  79.             ResultSet rs = stmt.executeQuery();
  80.            
  81.             rs.next();
  82.            
  83.             rcID = rs.getInt(1);
  84.            
  85.             if (rcID == 0) {
  86.                 willCommit = false;
  87.                 resultCode+= "B";
  88.             }
  89.            
  90.             sqlQuery = "INSERT INTO tbl_g23_orderlst(fld_id, fld_curr_price"
  91.                 + ", fld_or_qty, fld_or_qtytotal, fld_recid) VALUES (?,?,?,?,?);";            
  92.            
  93.             for(int i = 0; i < tblOrderSource.getRowCount(); i++){
  94.                 int currentPID = Integer.parseInt(tblOrderSource.getValueAt(i, 0).toString());
  95.                 double currentPrice = Double.parseDouble(tblOrderSource
  96.                         .getValueAt(i, 1).toString());  
  97.                 int currentQTY = Integer.parseInt(tblOrderSource.getValueAt(i, 2).toString());
  98.                 double currentTotal = Double.parseDouble(tblOrderSource
  99.                         .getValueAt(i, 3).toString());
  100.                
  101.                 stmt = conn.prepareStatement(sqlQuery);
  102.                
  103.                 stmt.setInt(1, currentPID);
  104.                 stmt.setDouble(2, currentPrice);
  105.                 stmt.setInt(3, currentQTY);
  106.                 stmt.setDouble(4, currentTotal);
  107.                 stmt.setInt(5, rcID);
  108.                
  109.                 rowsAffected = stmt.executeUpdate();
  110.            
  111.                 if (rowsAffected != 1) {
  112.                     willCommit = false;
  113.                     resultCode+= "C"+i;
  114.                 }
  115.             }
  116.            
  117.             if (willCommit == true) {
  118.                 conn.commit();
  119.                 resultCode += "_COMPLETE_";
  120.             }
  121.            
  122.             conn.close();
  123.         } catch (Exception e) {
  124.             rowsAffected = -1;
  125.             System.out.println(e.getMessage());
  126.         }
  127.        
  128.         System.out.println(resultCode);
  129.         return rowsAffected;
  130.     }
  131.  
  132.    
  133. }
  134. //--- form events-------------------------------
  135.  
  136.     private void btnAddToOrderActionPerformed(java.awt.event.ActionEvent evt) {                                              
  137.         // Taking inspiration from Day22AForm
  138.         Object[] forTblPOut = new Object[5];
  139.         forTblPOut[0] = tblProductList.getValueAt(tblProductList.getSelectedRow(), 0);
  140.         forTblPOut[1] = tblProductList.getValueAt(tblProductList.getSelectedRow(), 4);
  141.         forTblPOut[2] = Integer.parseInt(txtQty.getText());
  142.         Double orTotal = Double.parseDouble(txtQty.getText())
  143.         * Double.parseDouble(tblProductList.getValueAt(tblProductList.getSelectedRow(), 4).toString());
  144.  
  145.         forTblPOut[3] = String.format("%.4f", orTotal);
  146.  
  147.         DefaultTableModel tblModel = (DefaultTableModel) tblForOrderRecord.getModel();
  148.         tblModel.addRow(forTblPOut);
  149.  
  150.         double totalReceipt = 0;
  151.         int itemSold = 0;
  152.         String printout="";
  153.         for(int i = 0; i < tblForOrderRecord.getRowCount(); i++){
  154.             //        JOptionPane.showMessageDialog(this, "awooo");
  155.             itemSold+= Integer.parseInt(tblForOrderRecord.getValueAt(i, 2).toString());
  156.             totalReceipt += Double.parseDouble(tblForOrderRecord.getValueAt(i, 3).toString());
  157.         }
  158.         printout += "Items Sold: " + itemSold;
  159.         printout += "\nTotal Amount: " + totalReceipt;
  160.         JOptionPane.showMessageDialog(this, printout);
  161.         txtTotalQty.setText(String.valueOf(itemSold));
  162.         txtTotalPrice.setText(String.format("%.4f",totalReceipt));
  163.     }                                            
  164.  
  165.     private void btnCheckOutActionPerformed(java.awt.event.ActionEvent evt) {                                            
  166.         int rcQty = Integer.parseInt(txtTotalQty.getText());
  167.         double rcTotal = Double.parseDouble(txtTotalPrice.getText());
  168.        
  169.         Day29G23MethodsCommit callG23Methods = new Day29G23MethodsCommit();
  170.  
  171.         int result = callG23Methods.recordRecieptAndOrder(rcQty, rcTotal, tblForOrderRecord);
  172.        
  173.         JOptionPane.showMessageDialog(this, "record: " + result);
  174.     }                                          
  175.  
  176.     private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
  177.         Day29G23MethodsCommit callG23Methods = new Day29G23MethodsCommit();
  178.         callG23Methods.getAllRows(tblProductList);
  179.     }                                
  180.  
Advertisement
Add Comment
Please, Sign In to add comment