Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert bytea to String

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
I am using postgres 8.0.
I have a table trn.
create table trn (trn_id int, trn_file bytea);

insert into trn(1,"myFile.doc");

myFile.doc is a wordfile.

I have successfully inserted data in trn table.
Now i want to retrieve trn table and change the contents of myFile.doc.
But by using this below code i can't retrieve myFile.doc in readble (String) form....
It give me the binary string....

How i get the content of myFile.doc in readable form...


Code:
	  public String   retrieveFile(int trnId) {
	        
	        
	        preparedStatement = null;
		connection 		  = null;
		InputStream is = null;
	      	StringBuffer out = null;
			
		try {
		 ApplicationDataSource ads = new ApplicationDataSource(Constants.DATA_SOURCE_NAME);
		connection = ads.getConnection();
		String selectSql = "select trn_file from trn where trn_id=" + trnId + ";";
						preparedStatement  = connection.prepareStatement(selectSql);
				
	ResultSet rs = preparedStatement.executeQuery();
							
	if(rs!=null) {
	    while(rs.next()) {
	        is = rs.getBinaryStream(1);
	        out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = is.read(b)) != -1;) {
						            out.append(new String(b, 0, n));
		}
		        
	        is.close();
	    }
	    rs.close();
	}
	preparedStatement.close();
			       
 } catch (SQLException sqle) {
	System.err.println("Retrieve content: " + sqle.getMessage());
 } catch (Exception ioe) {
	System.err.println("Writing stuff: " + ioe.getMessage());
 }finally {				
				closeDatasourceWrappers();
 }
  return out.toString();
 }

here is the snippet of my jsp page..

<% String selectedTrnId = (String)request.getParameter("trnId");
System.out.println("selectedTrnId in UpdateTranscriptedFile(jsp) ==> " + selectedTrnId);
TranscriptDAO trnDao = new TranscriptDAO();
String fileContent = trnDao.retrieveFile(Integer.parseInt(selectedTrnId));
System.out.println("******************************** ");
System.out.println("fileContent in UpdateTranscriptedFile(jsp) ==> " + fileContent);
%>
.....
.....
.....
<TR>
<TH align="right">File :</TH>
<TD align="left"><html:text property="trnFile" value="<%= fileContent %>" /></TD>
</TR>


So any how i want trn_file content in string form...and then update this content as and when require and again store in bytea format in database.

pl. help me out...
Thanks in advanced.
ketan
 
I take it by saying you have a "*.doc" file it is a MS Word document.
If this is the case, then of course you cannot convert it to a string - its just not possible - you need the MS Word or a programme such as OpenOffice to interpret the binary data into a readabke format.

If you want to edit the file, then you need to extract the binary data, and edit it in a programme that can handle that data - or write your own programme that can handle MS Word format (unadvisable and not easy).

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top