ini kode hasil googling nya
Connection conn = null;
FileInputStream fis = null;
PreparedStatement ps = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/image", "root",
"");
String INSERT_PICTURE = "insert into MyPictures( name,
photo) values ( ?, ?)";
conn.setAutoCommit(false);
File file = ambilgmbr_jfch.getSelectedFile(); << ambil
dari jfilechooser
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, nama.getText());
ps.setBinaryStream(2, fis, (int) file.length());
ps.executeUpdate();
conn.commit();
( dalam kode ini file inputnya yaitu path image bentuk java.io.file
sedangkan program saya inputnya file Image( bentuk java.awt.Image))
gmana solusi menurut teman teman untuk save file saya / mungkin
confert bentuk image ke FileInputStream thank for answer
A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB differ only in the maximum length of the values they can hold.
import java.sql.*;
import java.io.*;
public class insertImage{
public static void main(String[] args) {
System.out.println("Insert Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "hibernatetutorial";
String userName = "root";
String password = "root";
Connection con = null;
try{
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
File imgfile = new File("images.jpg");
FileInputStream fin = new FileInputStream(imgfile);
PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
pre.setInt(1,5);
pre.setString(2,"Durga");
pre.setBinaryStream(3,fin,(int)imgfile.length());
pre.executeUpdate();
System.out.println("Inserting Successfully!");
pre.close();
con.close();
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}ni kode buat insert ke database (input java.awt.image)
img = file input image (java.awt.image)
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
ImageIO.write((RenderedImage) img, "jpeg", baos);
} catch (IOException ex) {
//
Logger.getLogger(StoreOnDatabaseGrabbedCamMouthImage.class.getName()).log(Level.SEVERE,
null, ex);
}
byte[] thumbnail = baos.toByteArray();
baos.close();
//insert ke database (blob)
ps.setBinaryStream(2, new ByteArrayInputStream(thumbnail),
thumbnail.length);
--
Ujian itu pasti, sabar itu pilihan
Mudzakkir