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