How to insert an Image in a Table - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

How to insert an Image in a Table

Share This



package com.t4b.jdbc.oracle.test; 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 InsertPictureToOracle { public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "pass123"); String INSERT_PICTURE = "insert into photos(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null; PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("photo_1.jpeg"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setInt(1, 1); ps.setString(2, "photo_1.jpeg"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.