Contoh Codingan untuk Transaksi pada NetBeans
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package kingburger;
- import config.config_masakan;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.HashMap;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.view.JasperViewer;
- /**
- *
- * @author Rei Fandi
- */
- public class transaksi extends javax.swing.JFrame {
- private DefaultTableModel model;
- public void auto_key(){
- try {
- Connection koneksi = config_masakan.KoneksiDb();
- String sql = "select max(right(id_transaksi, 1)) as id_transaksi from tb_transaksi" ;
- java.sql.Statement st = config_masakan.KoneksiDb().createStatement();
- ResultSet rs = st.executeQuery(sql);
- while(rs.next()){
- if(rs.first()==false){
- id.setText("1");} else{
- rs.last();
- int autoid = rs.getInt(1)+1;
- String no = String.valueOf(autoid);
- int noLong = no.length();
- for (int a=0;a<3-noLong;a++){ no =""+ no;
- } id.setText(""+ no);
- }
- }
- rs.close(); st.close();} catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Terjadi kesalaahan");
- }
- }
- public void ShowData(){
- int i=table.getSelectedRow();
- if(i==-1)
- { return; }
- String ID=(String)model.getValueAt(i, 0);
- id.setText(ID);
- id.disable();
- }
- public final void loadData(){
- model.getDataVector().removeAllElements();
- model.fireTableDataChanged();
- try{
- Connection koneksi = config_masakan.KoneksiDb();
- java.sql.Statement st = config_masakan.KoneksiDb().createStatement();
- String sql="Select * from tb_transaksi WHERE id_transaksi";
- ResultSet r=st.executeQuery(sql);
- while(r.next()){
- Object[]o=new Object[9];
- o[0]=r.getString("id_transaksi");
- o[1]=r.getString("nama_pelanggan");
- o[2]=r.getString("tanggal");
- o[3]=r.getString("pesanan");
- o[4]=r.getString("harga");
- o[5]=r.getString("jumlah_pesanan");
- o[6]=r.getString("total_harga");
- o[7]=r.getString("dibayar");
- o[8]=r.getString("kembalian");
- model.addRow(o);
- }
- r.close();
- st.close();
- //ShowData();
- }catch(SQLException e){
- System.out.println("Terjadi Kesalahan");
- }
- }
- public void cari_id(){
- try {
- Connection koneksi= config_masakan.KoneksiDb();
- String sql = "select * from tb_transaksi, id where tb_transaksi.jumlah_pesanan = tb_transaksi.id_transaksi AND tb_transaksi.id_transaksi='"+this.id.getText()+"'";
- java.sql.Statement st = config_masakan.KoneksiDb().createStatement();
- ResultSet rs = st.executeQuery(sql);
- while(rs.next()){
- this.id.setText(rs.getString("id_transaksi"));
- this.nama.setText(rs.getString("nama_pelanggan"));
- this.pesanan.addItem(rs.getString("pesanan"));
- this.harga.setText(rs.getString("harga"));
- this.jumlah.setText(rs.getString("jumlah_pesanan"));
- this.total.setText(rs.getString("total_harga"));
- this.dibayar.setText(rs.getString("keterangan"));
- this.kembalian.setText(rs.getString("kembalian"));
- }
- rs.close();
- st.close();
- }
- catch (Exception e) {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- public void TambahData(){
- String tampilan = "dd-MM-yyyy";
- SimpleDateFormat format = new SimpleDateFormat (tampilan);
- String tanggal = String.valueOf(format.format(tgl.getDate()));
- String id_transaksi =this.id.getText();
- String Nama=this.nama.getText();
- String Pesanan=(String) this.pesanan.getSelectedItem();
- String Harga=this.harga.getText();
- String jumlah_pesanan=this.jumlah.getText();
- String total_harga=this.total.getText();
- String Dibayar=this.dibayar.getText();
- String Kembalian=this.kembalian.getText();
- try{
- Connection koneksi= config_masakan.KoneksiDb();
- String sql="Insert into tb_transaksi (id_transaksi,nama_pelanggan,tanggal,pesanan,harga,jumlah_pesanan,total_harga,dibayar,kembalian) values (?,?,?,?,?,?,?,?,?)";
- PreparedStatement p=(PreparedStatement)koneksi.prepareStatement(sql);
- p.setString(1,id_transaksi);
- p.setString(2,Nama);
- p.setString(3,tanggal);
- p.setString(4,Pesanan);
- p.setString(5,Harga);
- p.setString(6,jumlah_pesanan);
- p.setString(7,total_harga);
- p.setString(8,Dibayar);
- p.setString(9,Kembalian);
- p.executeUpdate();
- p.close();
- }catch(SQLException e){
- System.out.println(e);
- }finally{
- JOptionPane.showMessageDialog(this,"Data Telah Tersimpan");
- }
- auto_key();
- loadData();
- }
- public void tampil()
- {
- try {
- Connection koneksi = config_masakan.KoneksiDb();
- String sql = "select id_transaksi from tb_transaksi where id_transaksi='"+id.getText()+"'";
- java.sql.Statement st = config_masakan.KoneksiDb().createStatement();
- ResultSet rs = st.executeQuery(sql); // yang anda ingin kan
- while(rs.next()){
- Object[] ob = new Object[1];
- ob[0]= rs.getString(1);
- id.setText((String) ob[0]);
- }
- rs.close(); st.close();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- public void HapusText(){
- this.id.disable();
- this.nama.setText(null);
- this.pesanan.addItem(null);
- this.harga.setText(null);
- this.jumlah.setText(null);
- this.total.setText(null);
- this.dibayar.setText(null);
- this.kembalian.setText(null);
- id.enable();
- this.id.requestFocusInWindow();
- }
- /**
- * Creates new form transaksi
- */
- public transaksi() {
- initComponents();
- auto_key();
- model =new DefaultTableModel();
- table.setModel(model);
- model.addColumn("id_transaksi");
- model.addColumn("nama_pelanggan");
- model.addColumn("tanggal");
- model.addColumn("pesanan");
- model.addColumn("harga");
- model.addColumn("jumlah_pesanan");
- model.addColumn("total_harga");
- model.addColumn("dibayar");
- model.addColumn("kembalian");
- loadData();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- private void pesananActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String harga_satuan = null;
- int kapasitas_komputer = 0;
- if (pesanan.getSelectedItem()== "--Pilih--"){
- id.setText("");
- nama.setText("");
- harga.setText("");
- jumlah.setText("");
- total.setText("");
- dibayar.setText("");
- kembalian.setText("");
- id.setEnabled(false);
- nama.setEnabled(false);
- harga.setEnabled(false);
- jumlah.setEnabled(false);
- total.setEnabled(false);
- dibayar.setEnabled(false);
- kembalian.setEnabled(false);
- pesanan.requestFocus();
- }
- else{
- id.setEnabled(true);
- nama.setEnabled(true);
- harga.setEnabled(true);
- jumlah.setEnabled(true);
- total.setEnabled(true);
- dibayar.setEnabled(true);
- kembalian.setEnabled(true);
- if (pesanan.getSelectedItem()== "Double Beef Burgers"){
- harga_satuan = "20000";
- }
- else if (pesanan.getSelectedItem()== "Special Burgers"){
- harga_satuan = "30000";
- }
- else if (pesanan.getSelectedItem()== "Chiken Burgers"){
- harga_satuan = "30000";
- }
- else if (pesanan.getSelectedItem()== "Black Coffe"){
- harga_satuan = "10000";
- }
- else if (pesanan.getSelectedItem()== "Mocca Ice Cream"){
- harga_satuan = "15000";
- }
- else if (pesanan.getSelectedItem()== "Mocca Ice"){
- harga_satuan = "10000";
- }
- harga.setText(harga_satuan);
- }
- }
- private void totalActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- int Harga = Integer.parseInt(harga.getText());
- int jumlah_pesanan = Integer.parseInt(jumlah.getText());
- int total_harga = Harga * jumlah_pesanan;
- total.setText(String.valueOf(total_harga));
- }
- private void simpanActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String tampilan = "dd-MM-yyyy";
- SimpleDateFormat format = new SimpleDateFormat (tampilan);
- String tanggal = String.valueOf(format.format(tgl.getDate()));
- this.TambahData();
- this.ShowData();
- this.id.disable();
- }
- private void hapusActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try {
- String sql ="delete from tb_transaksi where id_transaksi='"+id.getText()+"'";
- java.sql.Connection conn=(Connection)config_masakan.KoneksiDb();
- java.sql.PreparedStatement pst=conn.prepareStatement(sql);
- pst.execute();
- JOptionPane.showMessageDialog(this, "berhasil di hapus");
- } catch (Exception e) {
- JOptionPane.showMessageDialog(this, e.getMessage());
- }
- loadData();
- HapusText();
- }
- private void tableMouseClicked(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- int i = table.getSelectedRow();
- TableModel model = table.getModel();
- id.setText(model.getValueAt(i,0).toString());
- nama.setText(model.getValueAt(i,1).toString());
- pesanan.addItem(model.getValueAt(i,3).toString());
- harga.setText(model.getValueAt(i,4).toString());
- jumlah.setText(model.getValueAt(i,5).toString());
- total.setText(model.getValueAt(i,6).toString());
- dibayar.setText(model.getValueAt(i,7).toString());
- kembalian.setText(model.getValueAt(i,8).toString());
- }
- private void kembalianActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- int Total = Integer.parseInt(total.getText());
- int Dibayar = Integer.parseInt(dibayar.getText());
- int Kembalian = Dibayar - Total ;
- kembalian.setText(String.valueOf(Kembalian));
- }
- private void printActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try {
- String NamaFile = "src/Laporan/masakan2.jasper";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost/dbmasakan","root","");
- HashMap param = new HashMap(2);
- //Mengambil parameter
- JasperPrint JPrint = JasperFillManager.fillReport(NamaFile, param, koneksi);
- JasperViewer.viewReport(JPrint, false);
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null, "Data tidak dapat dicetak!","Cetak Data",JOptionPane.ERROR_MESSAGE);
- }
- }
- private void keluarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- this.setVisible(false);
- new kasir().setVisible(true);
- }
- private void baruActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- id.disable();
- this.nama.setText(null);
- this.pesanan.addItem(null);
- this.harga.setText(null);
- this.jumlah.setText(null);
- this.total.setText(null);
- this.dibayar.setText(null);
- this.kembalian.setText(null);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new transaksi().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton baru;
- private javax.swing.JTextField dibayar;
- private javax.swing.JButton hapus;
- private javax.swing.JTextField harga;
- private javax.swing.JTextField id;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel10;
- private javax.swing.JLabel jLabel11;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JLabel jLabel8;
- private javax.swing.JLabel jLabel9;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTextField jumlah;
- private javax.swing.JButton keluar;
- private javax.swing.JTextField kembalian;
- private javax.swing.JTextField nama;
- private javax.swing.JComboBox<String> pesanan;
- private javax.swing.JButton print;
- private javax.swing.JButton simpan;
- private javax.swing.JTable table;
- private com.toedter.calendar.JDateChooser tgl;
- private javax.swing.JTextField total;
- // End of variables declaration
- }
SEMOGA BERMANFAAT