Skip to content

Instantly share code, notes, and snippets.

@madan712
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
Embed
What would you like to do?
simple JDBC example to insert data into the blob column
import java.io.File;
import java.io.FileInputStream;
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;
Class.forName("com.mysql.jdbc.Driver");
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);
pre.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@highel
Copy link

highel commented Apr 18, 2018

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

@asadomsk
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