Require Driver : mysql-connector-java-5.0.8-bin.jar
Table Structure :
CREATE TABLE myImageFiles (
id int(11) NOT NULL auto_increment,
document blob
)
Table Structure :
CREATE TABLE myImageFiles (
id int(11) NOT NULL auto_increment,
document blob
)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jay | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
public class JaySQLImageExample { | |
public static void main(String[] args) throws Exception, IOException, SQLException { | |
Class.forName("com.mysql.jdbc.Driverr"); | |
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:4928/jptProject", "sa", "root"); | |
String INSERT_PICTURE = "insert into myImageFiles(id,document) values (?, ?)"; | |
FileInputStream fis = null; | |
PreparedStatement ps = null; | |
try { | |
conn.setAutoCommit(false); | |
File file = new File("Tulips.jpg"); | |
fis = new FileInputStream(file); | |
ps = conn.prepareStatement(INSERT_PICTURE); | |
ps.setString(1, "1"); | |
ps.setBinaryStream(2, fis, (int) file.length()); | |
ps.executeUpdate(); | |
conn.commit(); | |
} finally { | |
ps.close(); | |
fis.close(); | |
} | |
} | |
} |