cara masukan coding dijframeform ke javaclass

sore mas/mbak,

bagaimana coding di jframeform dimasukan ke class di java netbeans

ini classbarang

package controller;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.JOptionPane;

import net.proteanit.sql.DbUtils;

/**

*

\* @author Suhandana

*/

public class classbarang {

    Connection conn = null;

    ResultSet rs = null;

    PreparedStatement pst = null;

    private String sql;

    public String id_barang;

    public String nama_barang;

    public String stock;

    public Double harga_pokok;

    public double harga_jual;

        public void simpan( )throws SQLException{

        conn = koneksi.getKoneksi();

        sql = "INSERT INTO barang(id_barang,nama_barang,stock,harga_pokok,harga_jual) VALUE(?,?,?,?,?)";

        pst = conn.prepareStatement(sql);

        pst.setString(1, id_barang);

        pst.setString(2, nama_barang);

        pst.setString(3, stock);

        pst.setDouble(4, harga_pokok);

        pst.setDouble(5, harga_jual);

        pst.execute();

        pst.close();

    }

}

ini jframeform pasok yang mau dimasukan ke class barang

    private void txtidbarangKeyPressed(java.awt.event.KeyEvent evt) {

        // TODO add your handling code here:

        classbarang idm = new classbarang();

        if(evt.getKeyCode()==KeyEvent.VK_ENTER){

           try{

               //Class.forName("com.mysql.jdbc.Driver");

               Class.forName("com.mysql.cj.jdbc.Driver");

               Connection cn = DriverManager.getConnection("jdbc:mysql:"+"///kucing","root","meong");

               Statement smt = cn.createStatement();

               ResultSet rs = smt.executeQuery("Select * from barang where id_barang ="+"'"+txtidbarang.getText()+"'");

               if(rs.next()){

                   javax.swing.JOptionPane.showMessageDialog(this, "Kode Tersebut Ada","Pesan", javax.swing.JOptionPane.INFORMATION_MESSAGE);

                   txtnamabarang.setText(rs.getString("nama_barang"));

                   txtnamabarang.setEnabled(false);

                   //tgllahir.setDate(rs.getDate("tgl"));

               }else{

                   javax.swing.JOptionPane.showMessageDialog(this, "Data Tidak Ada","Pesan", javax.swing.JOptionPane.INFORMATION_MESSAGE);

               }

           }catch (Exception e){

               e.printStackTrace();

           }

           txtjumlah.requestFocus();

        }

    }

nb.jpg

avatar suhandana
@suhandana

24 Kontribusi 1 Poin

Dipost 3 tahun yang lalu

Tanggapan

Mohon di uraikan permasalahannya dimana, dan tujuan yang ingin dicapai seperti apa, terimakasih

Kalau hanya ingin cara memasukkannya, tinggal di copas saja codingannya hehe *just kidding

1 Jawaban:

<div>Maaf mas, masksudnya gimana caranya agar sourcecode yang&nbsp;<br>private void txtidbarangKeyPressed(java.awt.event.KeyEvent evt) {&nbsp; &nbsp;<br>bisa saya pindahkan ke class barang<br>jadi entar yang di private void txtidbarangKeyPressed(java.awt.event.KeyEvent evt) {&nbsp; &nbsp;<br>saya hanya tinggal panggil seperti idm.penggilbarang<br>seperti idm.simpan seperti ini mas:<br>&nbsp; &nbsp; private void simpanActionPerformed(java.awt.event.ActionEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; // TODO add your handling code here:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; classbarang idm = new classbarang();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.id_barang = txtidbarang.getText();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.nama_barang = txtnamabarang.getText();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.stock = txtstock.getText();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.harga_pokok = Double.parseDouble(txthargapokok.getText());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.harga_jual = Double.parseDouble(txthargajual.getText());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //idm.tanggal = jDateChooser1.getDateFormatString();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idm.simpan();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //JOptionPane.showMessageDialog(null, "Berhasil");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; javax.swing.JOptionPane.showMessageDialog(this, "Data Berhasil Tersimpan","Pesan", javax.swing.JOptionPane.INFORMATION_MESSAGE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception e){<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, e);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }&nbsp;</div>

avatar suhandana
@suhandana

24 Kontribusi 1 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban