Inserting Image in Database Table
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "javatutorial";
String userName = "root";
String password = "root";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url + dbName, userName, password);
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, "A");
pre.setBinaryStream(3, fin, (int) imgfile.length());
pre.executeUpdate();
pre.close();
con.close();
}
}
Related examples in the same category