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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing text file into BLOB field

Status
Not open for further replies.

Ciarrai

Technical User
Jun 27, 2003
17
US
I'm trying to use a servlet to write a file into a BLOB field in a mysql database. I can display the contents on my browser but all I seem to be entering in the blob field is "com.mysql.jdbc.Blob@6295eb". Heres the code. Any ideas?

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import java.sql.Blob;

public class photo extends HttpServlet {
// The database connection
Connection con=null;
ResultSet rs=null;
Statement stmt=null;

public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase");
stmt = con.createStatement();
}
catch (Exception e) {
throw new ServletException("Can't init Counter servlet: " +
e.getMessage(), e);
}
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
BufferedInputStream in = null; //declare buffered reader
OutputStream out = null;
long length;
final int EOF = -1;
int c;

File original = new File("c:\\home.txt");
File copy = new File("c:\\copy.txt");
Blob testBlob = null;
try {
rs = stmt.executeQuery("select document from documents where docid =9");
if (rs.next())
{ testBlob = rs.getBlob(1);
FileInputStream reader = new FileInputStream(original);
FileWriter writer = new FileWriter(copy);

out = testBlob.setBinaryStream(1);
byte[] buffer = new byte[(int)original.length()];

while ((length = reader.read(buffer)) != -1)
{System.out.println(testBlob.toString());
reader.close();
writer.close();
System.out.println("Copied \"original.txt\" to \"copy.txt\"");

}
rs.close();
stmt.close();
con.close();
} catch(FileNotFoundException fnfe)
{System.out.println("File not found: " +fnfe.getMessage());}
catch(IOException ioe){System.out.println("IOException: " +ioe.getMessage());}
catch(java.sql.SQLException e){System.out.println("SQL exception: " +e.getMessage());}
}
}
 
Use this instead for reading the byte data from the blob, its better to work with streams capable of working with bytes, rather than character writer streams :
Code:
Blob blob =  rs.getBlob(1);
InputStream is = blob.getBinaryStream();
byte[] bytes = new byte[(int)blob.length()];
is.read(bytes);

FileOutputStream fis = new FileOutputStream("file.txt");
fis.write(bytes);
fis.flush();
fis.close();
is.close();

Also, your code "testblob.toString()" will only render the results you get - as this does not print out the actual char data contained (if char data is contained) within the blob - it prints out the name of the object ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top