Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2012 12:57
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
simple JDBC example to insert data into the blob column
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class SimpleBlobExample {
public static void main(String[] args) {
try {
Connection con;
PreparedStatement pre;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName", "username", "password");
File picfile = new File("C:/Image.jpg");
FileInputStream fis = new FileInputStream(picfile);
pre = con
.prepareStatement("insert into blobtest (pic_name,pic_file) values (?,?)");
pre.setString(1, picfile.getName());
pre.setBinaryStream(2, fis, (int) picfile.length());
int count = pre.executeUpdate();
System.out.println("isUpdated? " + count);
} catch (Exception e) {
Copy link

highel commented Apr 18, 2018

For God's sake please use finally or try-with-resources to close resources correctly.

Copy link

asadomsk commented Jan 5, 2019

Is this a best practice? Going to use it now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment