Use ObjectOutputStream to insert into a MySQL database

Go To StackoverFlow.com

0

I'm making a multiplayer game server at the moment. Currently it saves each player as a serialized file, I'm looking to convert it to saving everything via MySQL so I can access it all on my website as well.

What I'm wondering, is to write to the serialized file, I use ObjectOutputStream. Below is my method for saving the file.

public static final void storeSerializableClass(Serializable o, File f)
        throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
    out.writeObject(o);
    out.close();
}

So what I'm wondering, is how would I be able to accomplish that, but saving to SQL, and not serialized? I've been playing with several different things, but I can't seem to find a solid way to do it.

Also I don't need to know how to use MySQL in Java, I know that, I just need to know how I can use ObjectOutputStream with it.

2012-04-05 01:03
by Darren
You would have to deserialize it at the database side - Hovercraft Full Of Eels 2012-04-05 01:04
So I could use ObjectOutputStream to save to an SQL database? I would just have to deserialize via PHP - Darren 2012-04-05 01:06
Ah, no. I see what you're saying, no that won't do - Hovercraft Full Of Eels 2012-04-05 01:07
Yeah do you know of any way to just take an ObjectOutputStream(like with serialized files), and insert into SQL? Because I haven't been able to find anything - Darren 2012-04-05 01:08
The only way is if you have Java on the server side, deserialized it in the server Java code, and then inserted information into the database. Serialization is a Java-only thing - Hovercraft Full Of Eels 2012-04-05 01:09
But I'm going to delete my comments since I am not very strong at client-server database stuff - Hovercraft Full Of Eels 2012-04-05 01:10
If I were to do away with serialization, is there anyway, to like, I don't know the correct words for it. Transfer a class to SQL? Like accomplish what ObjectOutputStream does, but without serialization, and storing to an SQL database? Sorry if that's hard to understand, I can't think of how to say it - Darren 2012-04-05 01:11
Again, I don't see any problem with using serialization as long as your server Java code deserializes it. You're not going to directly expose the database itself to the internet and will need some program between the net and the database. But again, let's see what more experienced hands have to say.. - Hovercraft Full Of Eels 2012-04-05 01:19
I'm wondering if you want to insert your object in a database in a structured format? Are you inserting just one kind of objects in database? If that's the case you could use an ORM (such as Hibernate) and persist your object to database - Amir Pashazadeh 2012-04-05 01:43
@AmirPashazadeh Well it's a full game, so there's a lot of stuff that needs put in. ObjectOutputStream takes the entire Player class, and puts it into a seperate file for each player. Basically I want it to do that, but insert into an sql table, rather than a seperate file - Darren 2012-04-05 01:56


0

You can store it as a Blob and then deserialize it. There is an example here: http://www.mindfiresolutions.com/Store-Java-Class-Object-in-Database-754.php

Another possibility is to simply store the file as text. It all depends on the requirements.

2014-12-11 23:33
by dan b
Ads