JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to read an image from database table? or Write an example for reading BLOB from table.


Description:

BLOB is nothing bug Binary Large Object. BLOB is used to store and retrieve large amount of binary data from database like images, etc. Below example shows how to read images from database rows.


Code:
package com.java2novice.jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyBlobRead {

	public static void main(String a[]){
		
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		InputStream is = null;
		OutputStream os = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.
					getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>
						,"user","password");
			st = con.createStatement();
			rs = st.executeQuery("select student_img from student_profile where id=101");
			if(rs.next()){
				is = rs.getBinaryStream(1);
			}
			is = new FileInputStream(new File("Student_img.jpg"));
			os = new FileOutputStream("std_img.jpg");
			byte[] content = new byte[1024];
			int size = 0;
			while((size = is.read(content)) != -1){
				os.write(content, 0, size);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try{
				if(is != null) is.close();
				if(os != null) os.close();
				if(st != null) st.close();
				if(con != null) con.close();
			} catch(Exception ex){}
		}
	}
}
<< Previous Program | Next Program >>
blog comments powered by Disqus

List Of All JDBC Programs:

  1. How to create JDBC Connection?
  2. What is Statement & ResultSet in JDBC?
  3. How to execute and read select queries using JDBC?
  4. How to update a record in the database using JDBC?
  5. How to execute any type of query in JDBC?
  6. What are the types of JDBC Statements available?
  7. Write an example code for JDBC prepared statement.
  8. Write an example for JDBC prepared statement with ResultSet.
  9. How to get primary key value (auto-generated keys) from inserted queries using JDBC?
  10. Write a simple program for CallableStatement statement to execute stored procedure.
  11. Write a program for CallableStatement statement with stored procedure returns OUT parameters.
  12. Write a program for CallableStatement statement with batch execution.
  13. Write a program to execute SQL function using CallableStatement.
  14. Write a program to execute SQL cursors using CallableStatement.
  15. How to get column properties from ResultSet using ResultSetMetaData?
  16. Write an example for batch update using Statement.
  17. Write an example for batch update using PreparedStatement.
  18. What are the types of ResultSets in JDBC?
  19. Write an example for scrollable result set with read only mode.
  20. Write an example for updatable result set.
  21. How to insert an image into database table? or Write an example for inserting BLOB into table.
  22. How to read an image from database table? or Write an example for reading BLOB from table.
  23. What is DatabaseMetaData? Write an example code.
  24. How to get JDBC Connection object using properties file?
Knowledge Centre
Can you list serialization methods?
Serialization interface does not have any methods. It is a marker interface. It just tells the JVM that your class can be serializable.
Famous Quotations
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.