File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stroing a Vector in MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stroing a Vector in MySQL" Watch "Stroing a Vector in MySQL" New topic
Author

Stroing a Vector in MySQL

james wilson
Greenhorn

Joined: Nov 17, 2001
Posts: 15
To Anybody that can help!
I'm trying to store a vector in a mysql database but when i read it back I get a EOFException. I don't even know if i've stored the vector correctly and its a read problem or if i'm not writing the vector correctly! Anywho here's the code
To Write;<pre>
public void writeDb()
{
try
{
Class.forName(dbDriver);
Connection myConnection = DriverManager.getConnection(dbId, dbUser, dbPassword);

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);

out.writeObject(newtime);

String vectordata = bytes.toString();
Statement myStatement = myConnection.createStatement();
String sqlQuery="UPDATE user SET dbTime = ('"+vectordata+"') WHERE dbUserId = ('"+userid+"')";
myStatement.executeUpdate(sqlQuery);
System.out.print("Success");

out.flush();
bytes.close();
}
catch (Exception e)
{
System.out.println("FAILURE " +e);
}
}
To Read;
public boolean timeDb()
{
try
{
Class.forName(dbDriver);
Connection myConnection = DriverManager.getConnection(dbId, dbUser, dbPassword);

Statement myStatement = myConnection.createStatement();
String sqlQuery="SELECT dbTime FROM user WHERE dbUserId = '"+userid+"'";
ResultSet myResultSet = myStatement.executeQuery(sqlQuery);

if(myResultSet.next())
{
String vectdata = myResultSet.getString("dbTime");
byte bytedata[] = vectdata.getBytes();

ByteArrayInputStream inbytes = new ByteArrayInputStream(bytedata);

ObjectInputStream in = new ObjectInputStream(inbytes);

time = (Vector)in.readObject();

Enumeration e = time.elements();
e.nextElement();
while(e.hasMoreElements())
{
String extract = e.nextElement().toString();

StringTokenizer token = new StringTokenizer(extract,"/");
token.nextToken();
newtime = newtime+Integer.parseInt(token.nextToken());
}

flag=true;
}
else
flag=false;
}
catch (Exception e)
{
System.out.println("FAILURE " +e);
}

return flag;

}
</pre>
Any help would be greatly appreciated
James Wilson
[This message has been edited by Marilyn deQueiroz (edited December 05, 2001).]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Does MySQL support BLOBs ?


Regards,

Pho
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Yes, Pho Tek, it does.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stroing a Vector in MySQL