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());}
}
}
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());}
}
}