Postingan lainnya
Cara Simpan Photo/Gambar di java Netbeans mysql
Gan / Sis,
Gimana cara simpan Photo di java Netbeans mysql, saya udah coba sana sini nggak berhasil, ini sourcecodenya
Sourcecode di Classbarang saya buat seperti ini
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;
public class classbarang {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
private String sql;
public String id_barang;
public String nama_barang;
public Double stock;
public Double harga_pokok;
public Double harga_jual;
public byte pas_foto;
public void simpan( )throws SQLException{
conn = koneksi.getKoneksi();
sql = "INSERT INTO barang(id_barang,nama_barang,stock,harga_pokok,harga_jual,pas_foto) VALUE(?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, id_barang);
pst.setString(2, nama_barang);
pst.setDouble(3, stock);
pst.setDouble(4, harga_pokok);
pst.setDouble(5, harga_jual);
pst.setBytes(6, pas_foto); “ERROR TANDA SERU MERAH”
pst.execute();
pst.close();
}
}
Sorucecode di Formbarang saya buat seperti ini :
package home;
import com.sun.glass.events.KeyEvent;
import controller.classbarang;
import controller.sambungkan;
import java.awt.Image;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.proteanit.sql.DbUtils;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
public class barang extends javax.swing.JFrame {
Image images;
String path;
byte[] bitarray=null;
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
classbarang idm = new classbarang();
idm.id_barang = txtidbarang.getText();
idm.nama_barang = txtnamabarang.getText();
idm.stock = Double.parseDouble(txtstock.getText());
idm.harga_pokok = Double.parseDouble(txthargapokok.getText());
idm.harga_jual = Double.parseDouble(txthargajual.getText());
try{
if(!txgambar.getText().equals("")){
ByteArrayOutputStream output=new ByteArrayOutputStream();
File img=new File(path);
FileInputStream input=new FileInputStream(img);
try {
byte[] buf=new byte[1024];
for(int readbyte;(readbyte=input.read(buf))!=-1;){
output.write(buf, 0 , readbyte);
}
bitarray=output.toByteArray();
idm.pas_foto(bitarray); “ERROR TANDA SERU MERAH”
} catch (Exception e){
}
}
idm.simpan();
javax.swing.JOptionPane.showMessageDialog(this, "Data Berhasil Tersimpan","Pesan", javax.swing.JOptionPane.INFORMATION_MESSAGE);
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
ViewData();
}
}
Terima kasih gan / Sis
Salam,
Suhandana
screenshoot 1
image.pngscreen shoot 2
image.png
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban