Created
November 6, 2012 12:57
-
-
Save madan712/4024557 to your computer and use it in GitHub Desktop.
simple JDBC example to insert data into the blob column
This file contains hidden or 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
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(); | |
} | |
} | |
} |
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
For God's sake please use finally or try-with-resources to close resources correctly.