Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: How to store data in BLOB column type


Advanced Search

Re: How to store data in BLOB column type
Posted by: Liang Zhao ()
Date: October 20, 2006 11:33PM

forExample:

1.store data:

String sql = "insert into user(username, photo) values(?, ?)" //photo is Blob
PreparedStatement pstmt = con.prepareStatement(sql); //prepare statement
File file = new File("c:/images/photo.jsp"); //init a File obj
FileInputStream fis = new FileInputStream(file); //get inputStream of this file
pstmt.setString(1, "LiangZhao"); //set parameter 1
pstmt.setBinaryStream(2, fis, (int)file.length()); //set parameter of blob type
pstmt.executeUpdate(); //execute update statment
pstmt.close(); //close resources
fis.close();

2.get data:

String sql = "select photo from user where username = ?"; //query statment
PreparedStatement pstmt = con.prepareStatement(sql); //like above
pstmt.setString(1, "LiangZhao"); //set parameter 1
ResultSet rs = pstmt.executeQuery(); //execute query and get resultset
rs.next(); //cusore move to next result
Blob blob = rs.getBlob("photo"); //get Blob obj by the photo field
ImageIcon icon = new ImageIcon(blob.getBytes(1, (int)blob.length())); //read bytes from first byte to the end
JLabel photo = new JLabel(icon); //make a use of it wooo
rs.close(); //close resources
pstmt.close();

Options: ReplyQuote


Subject Written By Posted
How to store data in BLOB column type Jahnvi Vyas 10/13/2006 01:33AM
Re: How to store data in BLOB column type Wayne w 10/17/2006 07:55AM
Re: How to store data in BLOB column type Liang Zhao 10/20/2006 11:33PM
How to store BLOB type in MYSQL using java Sachin Karangutkar 08/20/2008 06:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.