MySQL Forums
Forum List  »  Connector/JDBC and Java

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
Re: How to store data in BLOB column type
October 20, 2006 11:33PM


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.