Created
May 23, 2019 11:46
-
-
Save bashizip/2a35a99b4ae210292b3911af2c5b56f4 to your computer and use it in GitHub Desktop.
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.FileOutputStream; | |
import java.sql.Blob; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import com.codesjava.util.JDBCUtil; | |
/** | |
* This class is used to retrieve a image from DB. | |
* @author bashizip | |
*/ | |
public class JDBCTest { | |
public static void main(String args[]){ | |
Connection conn = null; | |
PreparedStatement preparedStatement = null; | |
String query = "select * from IMAGESTORE " + | |
"where IMAGE_ID = 1"; | |
try{ | |
//get connection | |
conn = JDBCUtil.getConnection(); | |
//create preparedStatement | |
preparedStatement = | |
conn.prepareStatement(query); | |
//execute query | |
ResultSet resultSet = | |
preparedStatement.executeQuery(); | |
resultSet.next(); | |
Blob clob = resultSet.getBlob(2); | |
byte[] byteArr = | |
clob.getBytes(1,(int)clob.length()); | |
FileOutputStream fileOutputStream = | |
new FileOutputStream("F:\\savedImage.jpg"); | |
fileOutputStream.write(byteArr); | |
System.out.println("Image retrieved successfully."); | |
//close connection | |
fileOutputStream.close(); | |
preparedStatement.close(); | |
conn.close(); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment