Blobs via servlet

I

Ike

I have a Blob field in a MySQL database, on a remote server wherein a
servlet (code below) communicates with the database, and transmits/receives
the SQL requests from an applet running on a local machine, using
ObjectInput/Output streams.

Things were fine until I have a blob field. Since blobs are NOT
serializeable, the ObjectInput/Output streams cant digest them.

Has anyone a solution to this?

Thanks, desperate, Ike
------------------------------------

import java.net.URL;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* applet-servlet-communication with streams
* send results of a query (from an applet) back to the applet
*/
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++) {
//newRow.addElement(rs.getString(i));
//newRow.addElement(rs.getObject(i));
if(((String)headers.get(i-1)).equals("picture"))
newRow.addElement(rs.getBlob(i));
else
newRow.addElement(rs.getObject(i));
}
// System.out.println("\n row:
"+(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();
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top