예제 #1
0
      public void valueChanged(ListSelectionEvent e) {
        int baris = tabel.getSelectedRow();

        if (baris > -1) {
          Produk p = dataProduk.get(baris);
          idProduk = p.getIdProduk();
        }
      }
예제 #2
0
  public void simpan(Produk p) throws Exception {
    psInsert.setString(1, p.getKode());
    psInsert.setString(2, p.getNama());
    psInsert.setBigDecimal(3, p.getHarga());
    psInsert.setDate(4, new java.sql.Date(p.getTerakhirUpdate().getTime()));

    hasil = psInsert.executeUpdate();
  }
 public void simpanProduk(Produk p) throws SQLException {
   Connection c = dataSource.getConnection();
   String sqlSimpan =
       "INSERT INTO `produk`(`kodeProduk`, `nama`, `harga`, `terakhirUpdate`) VALUES (?,?,?,?)";
   PreparedStatement psSimpan = c.prepareStatement(sqlSimpan);
   psSimpan.setString(1, p.getKodeProduk());
   psSimpan.setString(2, p.getNama());
   psSimpan.setBigDecimal(3, p.getHarga());
   psSimpan.setDate(4, new java.sql.Date(p.getTerakhirUpdate().getTime()));
   psSimpan.executeUpdate();
   c.close();
 }
  // override artinya menimpa/mengganti implementasi superclass
  @Override
  public BigDecimal hitungDiskon(Penjualan p) {
    BigDecimal totalDiskon = BigDecimal.ZERO;

    for (Produk x : p.getDaftarProduk()) {
      if (x.sedangDiskon()) {
        BigDecimal diskon = x.nilaiDiskon().multiply(x.getHarga());
        totalDiskon = totalDiskon.add(diskon);
      }
    }

    return totalDiskon;
  }
 public List<Produk> tampilProduk() throws SQLException {
   List<Produk> lp = new ArrayList<Produk>();
   Connection c = dataSource.getConnection();
   String sqlTampil = "SELECT * FROM produk";
   PreparedStatement psTampil = c.prepareStatement(sqlTampil);
   ResultSet rs = psTampil.executeQuery();
   while (rs.next()) {
     Produk p = new Produk();
     p.setId(rs.getInt("id"));
     p.setKodeProduk(rs.getString("kodeProduk"));
     p.setNama(rs.getString("nama"));
     p.setHarga(rs.getBigDecimal("harga"));
     p.setTerakhirUpadate(rs.getDate("terakhirUpdate"));
     lp.add(p);
   }
   c.close();
   return lp;
 }
예제 #6
0
  // ambil data dari database untuk tabel
  public void setDataTabel() {
    // combobox jenis
    JComboBox cbJenis = new JComboBox();
    cbJenis.setModel(new DefaultComboBoxModel(dataJenis.toArray()));

    dataProduk = new ArrayList<Produk>();
    try {
      String qry =
          "SELECT * FROM produk,suplier,jenis,stok_produk WHERE produk.id_jenis = jenis.id_jenis AND produk.id_suplier = suplier.id_suplier AND produk.id_produk=stok_produk.id_produk";
      ResultSet rs = stm.executeQuery(qry);
      while (rs.next()) {
        Produk p = new Produk();
        p.setIdProduk(rs.getInt("id_produk"));
        p.setNamaProduk(rs.getString("nama_produk"));
        p.setJenis(rs.getString("nama_jenis"));
        p.setHarga(rs.getInt("harga"));
        p.setStok(rs.getInt("stok"));
        p.setNamaSuplier(rs.getString("nama_suplier"));

        p.setComboJenis(cbJenis);

        dataProduk.add(p);
      }
    } catch (Exception err) {
      err.printStackTrace();
    }

    model = new TableModelProduk(dataProduk);
    tabel.setModel(model);
    tabel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    class EditProdukListener implements TableModelListener {
      public void tableChanged(TableModelEvent tme) {
        int baris = tme.getFirstRow();
        int kolom = tme.getColumn();

        TableModel model = (TableModel) tme.getSource();
        int id = (Integer) model.getValueAt(baris, 0);

        String query = "";
        switch (kolom) {
          case 1:
            String nama = (String) model.getValueAt(baris, kolom);
            query = "UPDATE produk SET nama_produk='" + nama + "' WHERE id_produk=" + id;
            prosesEdit(query);
            break;
          case 2:
            String jenis = (String) model.getValueAt(baris, kolom);
            try {
              query = "select * from jenis where nama_jenis='" + jenis + "'";
              ResultSet rs = stm.executeQuery(query);
              if (rs.next()) {
                int idJenis = rs.getInt("id_jenis");
                query = "UPDATE produk SET id_jenis=" + idJenis + " WHERE id_produk=" + id;
                prosesEdit(query);
              } else {
                setDataTabel();
                JOptionPane.showMessageDialog(null, "gagal,jenis tidak ada");
              }
            } catch (SQLException SQLerr) {
              SQLerr.printStackTrace();
            }
            break;
          case 3:
            int stok = (Integer) model.getValueAt(baris, kolom);
            query = "UPDATE `stok_produk` SET stok=" + stok + " WHERE id_produk=" + id;
            prosesEdit(query);
            break;
          case 4:
            int harga = (Integer) model.getValueAt(baris, kolom);
            query = "UPDATE produk SET harga=" + harga + " WHERE id_produk=" + id;
            prosesEdit(query);
            break;
          case 5:
            String suplier = (String) model.getValueAt(baris, kolom);
            try {
              query = "SELECT * FROM suplier WHERE nama_suplier='" + suplier + "'";
              ResultSet rs = stm.executeQuery(query);
              if (rs.next()) {
                int idSuplier = rs.getInt("id_suplier");
                query = "UPDATE produk SET id_suplier=" + idSuplier + " WHERE id_produk=" + id;
                prosesEdit(query);
              } else {
                setDataTabel();
                JOptionPane.showMessageDialog(null, "gagal,suplier belum terdaftar");
              }
            } catch (SQLException SQLerr) {
              SQLerr.printStackTrace();
            }
            break;
          default:
            break;
        }
      }

      private void prosesEdit(String query) {
        try {
          int hasil = stm.executeUpdate(query);
          if (hasil == 1) {
            setDataTabel();
            JOptionPane.showMessageDialog(null, "edit berhasil");
          } else {
            JOptionPane.showMessageDialog(null, "gagal");
          }
        } catch (SQLException SQLerr) {
          SQLerr.printStackTrace();
        }
      }
    }
    model.addTableModelListener(new EditProdukListener());
  }