Difference between Normal Insert and BLOB insert

Difference between Normal Insert and BLOB insert

I have sql database with table Images, in that table there are 2 Columns ID (identity), DisplayImage (image). Table has 2 rows for first row i have written

insert into Image values(1,'Libraries\Pictures\Lotus.jpg') and 2nd row as

INSERT INTO [dbo].[Image] ([Id],Images) SELECT 2,
(select * FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg',
SINGLE_BLOB) AS BLOB) Here is the code for reading the image
WebClient instanceHTTP = new WebClient();
Uri MyUri = new Uri("http://localhost:52293/WebSite/ImageHandler.ashx?ImageId=" + TextBox5.Text);
//TextBox5.Text for id i.e 1 or 2 
Stream returnValue;
returnValue = instanceHTTP.OpenRead(MyUri);
**System.Drawing.Image MyImage = System.Drawing.Image.FromStream(returnValue);**
// Error if id=1 used i.e normal insert bytearray = imageToByteArray(MyImage); 

MyImage.Dispose();

if i use id 1 that is normal insert than it gives error that invalid Parmeter but if i use id 2 it runs properly. So what is the difference?? and what changes i have to make in order to runn normal insert image.. I dont want to use AS BLOB in insert statement

View Answers

September 7, 2012 at 5:41 PM

If you want to store the image into database, you have to use blob type. The other datatype will not store the image into database. There are different data types for different fields, varchar for string, bigint for integer, blob for image etc.If you will access the image with other datatypes, it will give nvalid parameter error.

Here is a code that insert image into database:

 import java.sql.*;
 import java.io.*;
class  InsertDataAndImage
{
    public static void main(String[] args) 
    {

    FileInputStream fis;
    String url="jdbc:mysql://localhost:3306/test";
   try{
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     Connection con=DriverManager.getConnection(url,"root","root");
     File image=new File("c:/image1.jpg");
     PreparedStatement psmt=con.prepareStatement("insert into data(name,city,image)"+"values(?,?,?)");
     psmt.setString(1,"John");
     psmt.setString(2,"Austin");
     fis=new FileInputStream(image);
     psmt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
     int s = psmt.executeUpdate();
     if(s>0) {
     System.out.println("Inserted Successfully!");
     }
     con.close();
     psmt.close();
     }
     catch(Exception ex){
     System.out.println(ex);
     }
  }
}

Here is a code that retrieves image from database.

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class RetrieveImage {
public static void main(String argv[]){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select image from data where id='1'");
byte[] bytes = null;
if (rs.next()) {
bytes = rs.getBytes(1);
}
rs.close();
stmt.close();
con.close();
if (bytes != null) {
JFrame f = new JFrame();
f.setTitle("Display Image From database");
Image image = f.getToolkit().createImage(bytes);
ImageIcon icon=new ImageIcon(image);
JLabel lab=new JLabel();
lab.setIcon(icon);
f.getContentPane().add(lab);
f.setSize(400, 300);
f.setVisible(true);
}
}
catch (Exception e){
}
}
}

September 10, 2012 at 4:04 PM

I am working with C#









Related Tutorials/Questions & Answers:
Difference between Normal Insert and BLOB insert
Difference between Normal Insert and BLOB insert  I have sql database... normal insert image.. I dont want to use AS BLOB in insert statement   ... = System.Drawing.Image.FromStream(returnValue);** // Error if id=1 used i.e normal insert bytearray
Insert Mysql Blob data
Insert Mysql Blob data   In this section, we will discuss about how to insert a blob data into a database table. A Blob stores a binary large object in the database table's row. Blob object contains a logical pointer
Advertisements
insert
insert  insert data in database from servlet through JDBC   Ho Friend, Please visit the following:ADS_TO_REPLACE_1 Insert data into database Thanks
difference between prepared statement and statement
difference between prepared statement and statement  i mean...(?,?) but in normal statement we write insert into tablename(jtextfiled1.gettext(),jtextfield2.getText()); whats the difference between these two and which is more
Insert and Delete an element in between an array
Insert and Delete an element in between an array In this section, you will learn how to insert and delete an element in between an array. For this purpose, we... a new array. Following code insert an element in an array: public int
Insert Blob(Image) in Mysql table using JSP
Insert Blob(Image) in Mysql table using JSP In this Section, we will insert blob data(image) in Mysql database table using JSP code. A Blob stores a binary large object in the database table's row. Blob object contains a logical
DB Insert
DB Insert  How to insert XML data into a database column? Column data type is CLOB
jdbc insert
jdbc insert  Hi , i want to insert a declared integer variable into a mysql table through jdbc. how to insert that. help me with query... thanks... a table there. After creating a table in database, it insert a rows in the database
Difference between DispatchAction and LookupDispatchAction
Difference between DispatchAction and LookupDispatchAction  What is the Difference between DispatchAction and LookupDispatchAction
difference between SessionState and ViewState
difference between SessionState and ViewState  What is the difference between SessionState and ViewState
difference between ForwardAction and IncludeAction
difference between ForwardAction and IncludeAction  What is the difference between ForwardAction and IncludeAction
difference between == and === operators?
difference between == and === operators?  Is (====) operator available in java or not? difference between
Difference between struts and JSF
Difference between struts and JSF  What is the difference between struts and JSF
Mysql Insert
Mysql Insert       Mysql Insert is used to insert the records or rows to the table. Understand with ExampleADS_TO_REPLACE_1 The Tutorial illustrate an example from 'Mysql Insert
insert data
insert data  i've got a problem to insert my data to database.i can upload my multipart file but not text data.Please help me to solve this .Attached...("insert into images values(?,?,?,?,?)"); st.setString(2
Insert image in database
Insert image in database  Want a jsp-struts program in which the imageFile is in the form of getter and setter and inserted in the database as a Blob using . and then download the image and show on next jsp pg. No use
insert code jsp to access
insert code jsp to access   insert code jsp to access
Difference between 3d and 4d
Difference between 3d and 4d  what is the difference between 3d and 4d
What is the difference between the >> and >>> operators?
What is the difference between the >> and >>> operators?   hi, What is the difference between the >> and >>> operators? Thanks
difference between varchar & varchar2?
difference between varchar & varchar2?  What is the difference between varchar & varchar2
The INSERT INTO Statement, SQL Tutorial
The INSERT INTO Statement       The INSERT INTO statement is used to insert or add a record of data into the table. To insert records into a table, just write the key word
Difference between http and https
Difference between http and https  Difference b/w HTTP and HTTPS... in order to prevent unauthorized access. 2) HTTP transmits normal data where as HTTPS transmits closed or encrypted data. 3) HTTP is for normal applications
The INSERT INTO Statements in SQL?
The INSERT INTO Statements in SQL?  The INSERT INTO Statements in SQL?   Hi, The INSERT INTO statement is used to insert a new row or multiple rows into a table.ADS_TO_REPLACE_1 SQL INSERT INTO Syntax INSERT
insert images into a Mysql database
insert images into a Mysql database  How can I insert images into a Mysql database
how to insert image into server
how to insert image into server  how to insert an image into sql server and retrieve it using jSP
insert and delete a row programmatically
insert and delete a row programmatically  How to insert and delete a row programmatically ? (new feature in JDBC 2.0
how to insert check box
how to insert check box   how to insert check box into jtable row in swing
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
single query to insert the data
single query to insert the data  How to insert data for all HTML fields in single MYSQL query
insert query in jsp
insert query in jsp  give me insert code in jsp at run time
insert multiple selection - Java
insert multiple selection - Java  how to insert multiple selection values from html into database using servlets
Difference between Mysql and SQL
Difference between Mysql and SQL  hello, What is the difference between Mysql and SQL??   hii,ADS_TO_REPLACE_1 SQL is structural quary language but mysql is database package
Difference between Timer and Thread?
Difference between Timer and Thread?  Can anyone tell me about the difference between Timer and Thread, Why we need to have Timer in case we have Thread implimentation startegy in Java
The INSERT INTO Statement
The INSERT INTO Statement       The INSERT INTO statement is used to insert or add a record of data into the table. To insert records into a table, just write the key word
insert query in mysql
insert query in mysql  insert query in mysql not working   [INSERT INTO Birthdays(firstname, lastname, birthday, group) VALUES('Sam','Smith','June','Junior
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
Difference between SCJP Exams
Difference between SCJP Exams  What is the differences between SCJP 5 (310 - 055) exam and SCJP 6 (310 - 065) exam??? Thank You In Adv
Difference between JSP and Servlets
Difference between JSP and Servlets  What is the difference between JSP and Servlets ?   JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP
difference between lock and synchronization
difference between lock and synchronization  Hi, I am new in java please anyone tell me difference between lock and synchronization in java. its urgent. Thank in advance   Please visit the following link: Lock
insert and delete data in database
insert and delete data in database  insert and delete data in database from servlets through JDBC   Hi Friend, Please visit the following links:ADS_TO_REPLACE_1 Insert Data Delete Data ThanksADS_TO_REPLACE_2
insert image using hibernate
insert image using hibernate  Sir, Today I hadposted question regarding upload image, th answer you had given is using SQL, but I am using Hibernate to insert data in the same table where I want to insert image. Plz hlp me
hibernate insert image
hibernate insert image  Sir, Today I hadposted question regarding upload image, th answer you had given is using SQL, but I am using Hibernate to insert data in the same table where I want to insert image. Plz hlp me. Thanks
Difference between request.getRequestDispatcher() and context.getRequestDispatcher()
Difference between request.getRequestDispatcher() and context.getRequestDispatcher()  What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?   request.getRequestDispatcher
Difference between forward and sendRedirect
Difference between forward and sendRedirect  What's the difference between forward and sendRedirect?   RequestDispatcher.forward() and HttpServletResponse.sendRedirect() are the two methods available for URL redirecting
Difference between translate and replace
Difference between translate and replace   hiii, What is the difference between translate and replace?   hello,ADS_TO_REPLACE_1 Replace replace every instence of character with character sting by the given charator
Difference between ServletContext and ServletConfig
Difference between ServletContext and ServletConfig   What is the difference between ServletContext and ServletConfig?   ServletContext :Defines a set of methods that a servlet uses to communicate with its servlet
Difference between GET and POST
Difference between GET and POST   Difference between GET and POST ?   The difference between a GET and a POST is the way data is transferred to a servlet. With a GET, the URL will show each name/value pair on the query
Struts insert & retrieve
Struts insert & retrieve  Give a struts prg to insert and retrieve data from mysql db using the struts tags(no jsp scriplets and expressions) following mvc arch. i.e. model containing db connectivity, form containing get &
Difference between session.save() , session.saveOrUpdate() and session.persist() in Hibernate
Difference between session.save() , session.saveOrUpdate() and session.persist() in Hibernate  What is difference between session.save...() : Save does an insert and will fail if the primary key is already persistent

Ads