Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created June 21, 2017 14:29
Show Gist options
  • Select an option

  • Save darbyluv2code/0703315589ecb0fe74e0c36ee6b3def9 to your computer and use it in GitHub Desktop.

Select an option

Save darbyluv2code/0703315589ecb0fe74e0c36ee6b3def9 to your computer and use it in GitHub Desktop.
JDBC BLOB Demo
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author www.luv2code.com
*
*/
public class ReadBlobDemo {
public static void main(String[] args) throws Exception {
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
InputStream input = null;
FileOutputStream output = null;
try {
// 1. Get a connection to database
myConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/demo", "student", "student");
// 2. Execute statement
myStmt = myConn.createStatement();
String sql = "select resume from employees where email='[email protected]'";
myRs = myStmt.executeQuery(sql);
// 3. Set up a handle to the file
File theFile = new File("resume_from_db.pdf");
output = new FileOutputStream(theFile);
if (myRs.next()) {
input = myRs.getBinaryStream("resume");
System.out.println("Reading resume from database...");
System.out.println(sql);
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
output.write(buffer);
}
System.out.println("\nSaved to file: " + theFile.getAbsolutePath());
System.out.println("\nCompleted successfully!");
}
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
close(myConn, myStmt);
}
}
private static void close(Connection myConn, Statement myStmt)
throws SQLException {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
create database if not exists demo;
use demo;
drop table if exists employees;
CREATE TABLE `employees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(64) DEFAULT NULL,
`first_name` varchar(64) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
`department` varchar(64) DEFAULT NULL,
`salary` DECIMAL(10,2) DEFAULT NULL,
`resume` BLOB,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (1,'Doe','John','[email protected]', 'HR', 55000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (2,'Public','Mary','[email protected]', 'Engineering', 75000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (3,'Queue','Susan','[email protected]', 'Legal', 130000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (4,'Williams','David','[email protected]', 'HR', 120000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (5,'Johnson','Lisa','[email protected]', 'Engineering', 50000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (6,'Smith','Paul','[email protected]', 'Legal', 100000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (7,'Adams','Carl','[email protected]', 'HR', 50000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (8,'Brown','Bill','[email protected]', 'Engineering', 50000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (9,'Thomas','Susan','[email protected]', 'Legal', 80000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (10,'Davis','John','[email protected]', 'HR', 45000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (11,'Fowler','Mary','[email protected]', 'Engineering', 65000.00);
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (12,'Waters','David','[email protected]', 'Legal', 90000.00);
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author www.luv2code.com
*
*/
public class WriteBlobDemo {
public static void main(String[] args) throws Exception {
Connection myConn = null;
PreparedStatement myStmt = null;
FileInputStream input = null;
try {
// 1. Get a connection to database
myConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/demo", "student", "student");
// 2. Prepare statement
String sql = "update employees set resume=? where email='[email protected]'";
myStmt = myConn.prepareStatement(sql);
// 3. Set parameter for resume file name
File theFile = new File("sample_resume.pdf");
input = new FileInputStream(theFile);
myStmt.setBinaryStream(1, input);
System.out.println("Reading input file: " + theFile.getAbsolutePath());
// 4. Execute statement
System.out.println("\nStoring resume in database: " + theFile);
System.out.println(sql);
myStmt.executeUpdate();
System.out.println("\nCompleted successfully!");
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (input != null) {
input.close();
}
close(myConn, myStmt);
}
}
private static void close(Connection myConn, Statement myStmt)
throws SQLException {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment