Reputation: 884
I am trying to print out the contents of row set to a file but I am getting java.io.NotSerializableException:
Here is my program..
package k564;
import java.io.*;
import javax.sql.RowSet;
import com.sun.rowset.JdbcRowSetImpl;
public class Lab15a {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
RowSet rs= new JdbcRowSetImpl();
rs.setUrl("jdbc:mysql://localhost:3306/jdbc");
rs.setUsername("root");
rs.setPassword("123");
rs.setCommand("select cid,cname,cemail,cphone,camount from customer");
rs.execute();
while(rs.next()){
System.out.println(rs.getInt("cid")+"\t"+rs.getString("cname")+"\t"+rs.getString("cemail")+"\t"+rs.getInt("cphone")+rs.getInt("camount"));
}
rs.beforeFirst();
System.out.println("Serialixzing Rowset");
FileOutputStream fos = new FileOutputStream("d:\\rowset.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(rs);
System.out.println("serialized");
}catch(Exception e){
e.printStackTrace();
}
}
}
Error that I am getting.
Error:
java.io.NotSerializableException: java.lang.Object
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at k564.Lab15a.main(Lab15a.java:33)
I am not able to figure out what is wrong here?
Upvotes: 0
Views: 1000
Reputation: 5259
According to doc,
Disconnected RowSet objects have all the capabilities of connected RowSet objects plus they have the additional capabilities available only to disconnected RowSet objects. For example, not having to maintain a connection to a data source makes disconnected RowSet objects far more lightweight than a JdbcRowSet object or a ResultSet object. Disconnected RowSet objects are also serializable, and the combination of being both serializable and lightweight makes them ideal for sending data over a network.
I recommend using a CachedRowSet
which according to doc may be more suitable for serializing
You can also think of creating a Customer class which implements Serializable
with fields like cid,cname,cemail,cphone,camount
to represent the values read from the database. Then map the values from Resultset to the values of the column using setters for each field and then you can serialize the Customer
class to the output file.
Note: If you want to serialize an Object, the class must implement Serializable and you are strongly recommended to also add a serialVersionUID to represent the version number
Upvotes: 0
Reputation: 1366
Java's ObjectOuputStream
can only serialize objects which implement the java.io.Serializable
interface. JdbcRowSet
class does not implement this interface.
To print out the contents, try to use getRow()
, toString()
or something similar to achieve your goal.
Upvotes: 1