I
Ike
When reading a Blob, using JDBC, without using a servlet as an intermediary,
I am able to succesfully read the Blob and decode to an ImageIcon as:
blob = rs.getBlob( 1 );
if(blob!=null){
iLength = (int)(blob.length());
ii = new ImageIcon(blob.getBytes( 1, iLength ));
However, I have problems now when I put a servlet as the intermediary
between my applet and the (MySql) database.Since Blobs are not serializable,
I am converting the blob to a byte[] within the servlet, sending the byte[]
to the Applet, where I try to instantiate an ImagIcon from the byte[].
My applet side thus looks like this:
//calling the data from teh servlet...
Vector h = new Vector();
Vector d = new Vector()
execSQL("select * from table", h,d);
//later, I retrieve the byte[] and go to create the ImageIcon...
byte[] jack = (byte[])v2.elementAt(u); //jack[] is now filled with data and
appears to be the right size
ImageIcon ii = new ImageIcon(jack);
The byte[] IS coming over, but somehow it is being transformed. I fear I
have something REALLY stupid in the code that follows, but I cannot discern
what it is. -Ike
public String execSQL(String sqlField,Vector headers,Vector rows) {
try {
int i;
String s;
String host = applet.getDocumentBase().getHost();
int port = applet.getDocumentBase().getPort();
if (port != -1) host += ":" + port;
URL servletURL = null;
servletURL = new URL("http://" + host + servletURLstring);
URLConnection uc = servletURL.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type","application/octet-stream");
ObjectOutputStream objOut = new
ObjectOutputStream(uc.getOutputStream());
String conArray[] = new String[5];
conArray[0] = driverField;
conArray[1] = urlField;
conArray[2] = userField;
conArray[3] = passwordField;
conArray[4] = sqlField;
if(hacking) System.out.println("send to servlet: " + sqlField);
objOut.writeObject(conArray);
objOut.flush();
objOut.close();
// get objects from servlet (1. message-string, 2. headers
3.resultset-vector)
ObjectInputStream objIn = new ObjectInputStream
(uc.getInputStream());
String returnString = (String)objIn.readObject();
headers.addAll( (Vector)(objIn.readObject()) );
// get results of the query
rows.addAll( (Vector)(objIn.readObject()) );
objIn.close();
return returnString;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
};
public class TestServlet2 extends HttpServlet {
public boolean DEBUG;
/** Creates a new instance of TestServlet2 */
public TestServlet2() {
}
public void init(ServletConfig config) throws ServletException {
// This method initializes the servlet and only gets call once.
// Allocate all of the servlet resources here.
super.init(config);
}
public void destroy() {
// Once this method is called then any instance of this class can be
garbage collected
// Here is where all servlets resources can be deallocated.
}
// public synchronized void service (HttpServletRequest request,
HttpServletResponse response)
public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
String returnString = "OK";;
Vector headers = new Vector();
Vector rows = new Vector();
// get an input stream from the applet
(driver,url,userid,password,query-string)
ObjectInputStream inputFromApplet = new
ObjectInputStream(request.getInputStream());
String connArray[] = (String[])inputFromApplet.readObject();
inputFromApplet.close();
String driver = connArray[0];
String url = connArray[1];
String user = connArray[2];
String password = connArray[3];
String SQLString = connArray[4];
// perform query
int numberOfRows = 0;
Statement st = null;
Connection con = null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,user,password);
st = con.createStatement();
st.execute(SQLString);
int updRows = st.getUpdateCount(); // -1 if no updates
if (updRows > 0) { // update,insert......
returnString = ("Rows affected: " + updRows);
}
else if (updRows == 0) { // no updates
returnString = ("Error, no rows affected");
}
else { // result of a sql-select
ResultSet rs = st.getResultSet();
ResultSetMetaData md = rs.getMetaData();
// headers
int numberOfColumns = md.getColumnCount();
for(int column = 0; column < numberOfColumns; column++)
{
headers.addElement(md.getColumnLabel(column+1));
}
// result
while (rs.next()) {
numberOfRows++;
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
if(((String)headers.get(i-1)).equals("picture")){
try{
Blob blob = rs.getBlob(i);
if(blob!=null){
int iLength = (int)(blob.length());
byte[] jack = blob.getBytes( 1,
iLength );
newRow.addElement(jack);
}
}catch(Exception ex){returnString=
ex.toString();}
} else
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
rs.close();
if (numberOfRows == 0) returnString = "no rows
selected";
}
st.close();
con.close();
}
catch (SQLException e) {
if (st != null) st.close();
if (con != null) con.close();
returnString = e.toString();
}
// send objects back to applet
ObjectOutputStream outputToApplet = new
ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(returnString); // sql-message
outputToApplet.writeObject(headers); // fieldnames
outputToApplet.writeObject(rows); // result-vector
outputToApplet.flush();
outputToApplet.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
I am able to succesfully read the Blob and decode to an ImageIcon as:
blob = rs.getBlob( 1 );
if(blob!=null){
iLength = (int)(blob.length());
ii = new ImageIcon(blob.getBytes( 1, iLength ));
However, I have problems now when I put a servlet as the intermediary
between my applet and the (MySql) database.Since Blobs are not serializable,
I am converting the blob to a byte[] within the servlet, sending the byte[]
to the Applet, where I try to instantiate an ImagIcon from the byte[].
My applet side thus looks like this:
//calling the data from teh servlet...
Vector h = new Vector();
Vector d = new Vector()
execSQL("select * from table", h,d);
//later, I retrieve the byte[] and go to create the ImageIcon...
byte[] jack = (byte[])v2.elementAt(u); //jack[] is now filled with data and
appears to be the right size
ImageIcon ii = new ImageIcon(jack);
The byte[] IS coming over, but somehow it is being transformed. I fear I
have something REALLY stupid in the code that follows, but I cannot discern
what it is. -Ike
public String execSQL(String sqlField,Vector headers,Vector rows) {
try {
int i;
String s;
String host = applet.getDocumentBase().getHost();
int port = applet.getDocumentBase().getPort();
if (port != -1) host += ":" + port;
URL servletURL = null;
servletURL = new URL("http://" + host + servletURLstring);
URLConnection uc = servletURL.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type","application/octet-stream");
ObjectOutputStream objOut = new
ObjectOutputStream(uc.getOutputStream());
String conArray[] = new String[5];
conArray[0] = driverField;
conArray[1] = urlField;
conArray[2] = userField;
conArray[3] = passwordField;
conArray[4] = sqlField;
if(hacking) System.out.println("send to servlet: " + sqlField);
objOut.writeObject(conArray);
objOut.flush();
objOut.close();
// get objects from servlet (1. message-string, 2. headers
3.resultset-vector)
ObjectInputStream objIn = new ObjectInputStream
(uc.getInputStream());
String returnString = (String)objIn.readObject();
headers.addAll( (Vector)(objIn.readObject()) );
// get results of the query
rows.addAll( (Vector)(objIn.readObject()) );
objIn.close();
return returnString;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
};
public class TestServlet2 extends HttpServlet {
public boolean DEBUG;
/** Creates a new instance of TestServlet2 */
public TestServlet2() {
}
public void init(ServletConfig config) throws ServletException {
// This method initializes the servlet and only gets call once.
// Allocate all of the servlet resources here.
super.init(config);
}
public void destroy() {
// Once this method is called then any instance of this class can be
garbage collected
// Here is where all servlets resources can be deallocated.
}
// public synchronized void service (HttpServletRequest request,
HttpServletResponse response)
public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
String returnString = "OK";;
Vector headers = new Vector();
Vector rows = new Vector();
// get an input stream from the applet
(driver,url,userid,password,query-string)
ObjectInputStream inputFromApplet = new
ObjectInputStream(request.getInputStream());
String connArray[] = (String[])inputFromApplet.readObject();
inputFromApplet.close();
String driver = connArray[0];
String url = connArray[1];
String user = connArray[2];
String password = connArray[3];
String SQLString = connArray[4];
// perform query
int numberOfRows = 0;
Statement st = null;
Connection con = null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,user,password);
st = con.createStatement();
st.execute(SQLString);
int updRows = st.getUpdateCount(); // -1 if no updates
if (updRows > 0) { // update,insert......
returnString = ("Rows affected: " + updRows);
}
else if (updRows == 0) { // no updates
returnString = ("Error, no rows affected");
}
else { // result of a sql-select
ResultSet rs = st.getResultSet();
ResultSetMetaData md = rs.getMetaData();
// headers
int numberOfColumns = md.getColumnCount();
for(int column = 0; column < numberOfColumns; column++)
{
headers.addElement(md.getColumnLabel(column+1));
}
// result
while (rs.next()) {
numberOfRows++;
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
if(((String)headers.get(i-1)).equals("picture")){
try{
Blob blob = rs.getBlob(i);
if(blob!=null){
int iLength = (int)(blob.length());
byte[] jack = blob.getBytes( 1,
iLength );
newRow.addElement(jack);
}
}catch(Exception ex){returnString=
ex.toString();}
} else
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
rs.close();
if (numberOfRows == 0) returnString = "no rows
selected";
}
st.close();
con.close();
}
catch (SQLException e) {
if (st != null) st.close();
if (con != null) con.close();
returnString = e.toString();
}
// send objects back to applet
ObjectOutputStream outputToApplet = new
ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(returnString); // sql-message
outputToApplet.writeObject(headers); // fieldnames
outputToApplet.writeObject(rows); // result-vector
outputToApplet.flush();
outputToApplet.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}