Home > Archive > Java Help > January 2006 > store object in database
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
store object in database
|
|
| sarath 2006-01-24, 7:06 pm |
| hi.
how will i be able to store objects(say a hashtable) in the
database ?
i have tried putting it in mysql db using the following code., but
while retrieving, i am getting a class cast exception when i am trying
to cast the object that i got from db to the hashtable. Please help..
the code goes as,
import java.sql.*;
import java.util.Hashtable;
import java.io.*;
public class HashStoreTest {
Connection con ;
/** Creates a new instance of HashStoreTest */
public HashStoreTest() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con =
DriverManager.getConnection("jdbc:mysql://192.168.1.6:3306/Test","test","test");
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (java.lang.ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public static void main(String args[]){
HashStoreTest hs = new HashStoreTest();
Hashtable hash = new Hashtable();
hash.put("key1","value1");
hash.put("key2","value2");
hash.put("key3","value3");
hash.put("key4","value4");
try {
//stores the element in DB
PreparedStatement stmt = hs.con.prepareStatement("INSERT
INTO HashTest values(?,?)");
stmt.setString(1, "hash");
stmt.setObject(2,hash);
//stmt.setBinaryStream(2,bais,bytes.length);
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
//trying to retrieve
byte [] bf = new byte[1000];
byte[] buff = new byte[1000];
ByteArrayOutputStream sout = new
ByteArrayOutputStream(1000);
Statement st = hs.con.createStatement();
ResultSet rs = st.executeQuery("select * from
HashTest");
rs.next();
// in next line, i am getting a class cast exception
Hashtable hhh = (Hashtable)rs.getObject(2);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
| |
| Roedy Green 2006-01-24, 7:06 pm |
| On 24 Jan 2006 09:07:15 -0800, "sarath" <sarath.post@gmail.com> wrote,
quoted or indirectly quoted someone who said :
> HashStoreTest hs = new HashStoreTest();
> Hashtable hash = new Hashtable();
> hash.put("key1","value1");
> hash.put("key2","value2");
> hash.put("key3","value3");
> hash.put("key4","value4");
a lazier way to do it is to write a byte array object stream and store
that whole thing as a blob.
see http://mindprod.com/applets/fileio.html for how.
The Hashtable is then a single BLOB byte[] object as far the database
is concerned.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| Oliver Wong 2006-01-24, 7:06 pm |
|
"zero" <zero@this.hi> wrote in message
news:Xns9755BA9C6CF2Ezerothishi@195.130.132.70...
> "sarath" <sarath.post@gmail.com> wrote in
> news:1138122435.729542.136630@f14g2000cwb.googlegroups.com:
>
>
> <snip>
>
> As far as I know, mysql is not an OO database, so you can't store objects
> in it directly. What you can do is store each of the object's fields if
> they are primitives. You can also store a canonical representation of the
> object. As for a hashtable, I believe you'd be better off saving the
> individual keys and elements, and put them back in a (new) hashtable when
> retrieving them.
The OP may wish to look into ORMs, or Object-Relational Mappings. An ORM
basically provides the facility to store objects in a relational database. A
popular one for Java, which is freely available, is Hibernate:
http://www.hibernate.org/
If you just want to store this Hashtable, using Hibernate is probably
overkill, and you should use a BLOB as Roedy mentions elsewhere in this
thread. However, if you're going to be storing lots of different objects in
your DB, Hibernate might make your life easier.
- Oliver
| |
| sarath 2006-01-25, 4:14 am |
| As per ur suggestion, i had tried the following code, but still it
gives me the errors..
i am able to retrieve the object in a byte array.. but how will i be
able to conver the same to an object and to hashtable ?
import java.sql.*;
import java.util.Hashtable;
import java.io.*;
public class HashStoreTest {
Connection con ;
/** Creates a new instance of HashStoreTest */
public HashStoreTest() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con =
DriverManager.getConnection("jdbc:mysql://192.168.1.6:3306/Test","test","test");
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (java.lang.ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public static void main(String args[]){
HashStoreTest hs = new HashStoreTest();
Hashtable hash = new Hashtable();
hash.put("key1","value1");
hash.put("key2","value2");
hash.put("key3","value3");
hash.put("key4","value4");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(out);
oos.writeObject(hash);
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
PreparedStatement stmt = hs.con.prepareStatement("INSERT
INTO HashTest values(?,?)");
stmt.setString(1, "hash");
//stmt.setObject(2,hash);
stmt.setBinaryStream(2,bais,bytes.length);
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
byte [] bf = new byte[1000];
byte[] buff = new byte[1000];
ByteArrayOutputStream sout = new
ByteArrayOutputStream(1000);
Statement st = hs.con.createStatement();
ResultSet rs = st.executeQuery("select * from
HashTest");
rs.next();
//Hashtable hhh = (Hashtable)rs.getObject(2);
InputStream sin = rs.getBinaryStream( 2 ) ;
if ( sin != null ) {
for (;;) {
int size = sin.read( buff ) ;
if ( size == -1 ) {
break ;
}
sout.write( buff, 0, size ) ;
}
}
/*I think i have got the object in the byte array with the above code
*but how can i get that into object and finally to hashtable so that
*i will be able to use them ??
*/
} catch (SQLException ex) {
ex.printStackTrace();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
| |
| Roedy Green 2006-01-25, 4:14 am |
| On 24 Jan 2006 21:34:29 -0800, "sarath" <sarath.post@gmail.com> wrote,
quoted or indirectly quoted someone who said :
> stmt.setBinaryStream(2,bais,bytes.length);
there is no need for a stream here. You should be able to write the
byte[] directly with PreparedStatement.setBytes
On the SQL side you need a BLOB field to accept that.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| Roedy Green 2006-01-25, 4:14 am |
| On 24 Jan 2006 21:34:29 -0800, "sarath" <sarath.post@gmail.com> wrote,
quoted or indirectly quoted someone who said :
>/*I think i have got the object in the byte array with the above code
>*but how can i get that into object and finally to hashtable so that
>*i will be able to use them ??
>*/
you get the byte array back out of the database. Then you turn that
into a ByteArrayInputStream with ReadObjectStream tacked on top.
see http://mindprod.com/applets/fileio.html
and tell it you want to read an object from a byte[].
you read the object and cast it back to HashMap and voila, everything
in the HashMap is restored along with it.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| sarath 2006-01-27, 3:59 am |
| Thank u all for the reply.. i was able to do it with ur suggestions..
and those links that u gave solved much more problems for me.....
regards
sarath
Roedy Green wrote:
> On 24 Jan 2006 21:34:29 -0800, "sarath" <sarath.post@gmail.com> wrote,
> quoted or indirectly quoted someone who said :
>
>
> you get the byte array back out of the database. Then you turn that
> into a ByteArrayInputStream with ReadObjectStream tacked on top.
>
> see http://mindprod.com/applets/fileio.html
> and tell it you want to read an object from a byte[].
>
> you read the object and cast it back to HashMap and voila, everything
> in the HashMap is restored along with it.
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|