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
Related Tutorials/Questions & Answers:
Difference between Normal Insert and BLOB insertDifference 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
insertinsert
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 statementdifference 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 arrayInsert 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 JSPInsert 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 InsertDB Insert How to
insert XML data into a database column? Column data type is CLOB
jdbc insertjdbc 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
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 datainsert 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 databaseInsert 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
The INSERT INTO Statement, SQL TutorialThe
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 httpsDifference 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 jtable insert row swingjtable
insert row swing How to
insert and refresh row in JTable?
Inserting Rows in a JTable example
Difference between Mysql and SQLDifference 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 StatementThe
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 mysqlinsert query in mysql
insert query in mysql not working
[
INSERT INTO Birthdays(firstname, lastname, birthday, group) VALUES('Sam','Smith','June','Junior
Difference between SCJP ExamsDifference 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 ServletsDifference 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 synchronizationdifference 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 databaseinsert 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 hibernateinsert 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 imagehibernate
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 forward and sendRedirectDifference 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 ServletConfigDifference 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 & retrieveStruts
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 &