Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stroing a Vector in MySQL

 
james wilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does MySQL support BLOBs ?
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Pho Tek, it does.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic