Java Reference
In-Depth Information
185
// Do NOT use with JDK 1.2.2 using JDBC-ODBC Bridge as
186
// SQL NULL data value is not handled correctly.
187
buf = rs.getBytes ( "pswd" ) ;
188
if ( buf != null )
189
{
190
System .out.println ( "Password Object = "
191
+ ( pswdFromDB= ( Password ) deserializeObj ( buf ))) ;
192
System .out.println ( " AutoExpires
= " + pswdFromDB.getAutoExpires ()) ;
193
System .out.println ( " Expiring now
= " + pswdFromDB.isExpiring ()) ;
194
System .out.println ( " Remaining uses = "
195
+ pswdFromDB.getRemainingUses () + "\n" ) ;
196
}
197
else
198
System .out.println ( "Password Object = NULL!" ) ;
199
}
200
201
rs = stmt.executeQuery ( "SELECT * FROM Stocks" ) ;
202
if ( !rs.next ())
203
System .out.println ( "Stocks table contains no records." ) ;
204
else
205
System .out.println ( "Stocks table still contains records!" ) ;
206
207
rs = stmt.executeQuery ( "SELECT * FROM UserStocks" ) ;
208
if ( !rs.next ())
209
System .out.println ( "UserStocks table contains no records." ) ;
210
else
211
System .out.println ( "UserStocks table still contains records!" ) ;
212
213
stmt.close () ; // closing Statement also closes ResultSet
214
215
} // end of main()
216
217
// Method to write object to byte array and then insert into prepared statement
218
public static byte [] serializeObj ( Object obj )
219
throws IOException
220
{
221
ByteArrayOutputStream baOStream = new ByteArrayOutputStream () ;
222
ObjectOutputStream objOStream = new ObjectOutputStream ( baOStream ) ;
223
224
objOStream.writeObject ( obj ) ; // object must be Serializable
225
objOStream.flush () ;
226
objOStream.close () ;
227
return baOStream.toByteArray () ; // returns stream as byte array
228
}
229
230
// Method to read bytes from result set into a byte array and then
231
// create an input stream and read the data into an object
232
public static Object deserializeObj ( byte [] buf )
233
throws IOException , ClassNotFoundException
234
{
235
Object obj = null ;
236
237
if ( buf != null )
238
{
239
ObjectInputStream objIStream =
240
new ObjectInputStream ( new ByteArrayInputStream ( buf )) ;
241
242
obj = objIStream.readObject () ; // throws IOException, ClassNotFoundException
243
}
244
return obj;
245
}
246
} // end of class
FIGURE 11-17
Search WWH ::




Custom Search