Guest User

Untitled

a guest
Aug 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. Java - Unable to get the values from retrieved serialized object
  2. public static boolean storeInfo(Reservation rsv) throws
  3. IOException, SQLException{
  4. try {
  5. //Connection to DB
  6. Connection con = null;
  7. Class.forName("oracle.jdbc.driver.OracleDriver");
  8. con=DriverManager.getConnection(
  9. "jdbc:oracle:thin:@odsdsd",
  10. "SMBDB",
  11. "hpdbsmb");
  12. con.setAutoCommit(false);
  13. ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
  14. ObjectOutputStream out = new ObjectOutputStream(bos);
  15. out.writeObject(rsv);
  16. out.flush();
  17. out.close();
  18. byte[] buf = bos.toByteArray();
  19. //inserting into database
  20. PreparedStatement prepareStatement = con.prepareStatement("INSERT INTO
  21. SMD_RESERVATION_INSTANCES VALUES(?,?,?)");
  22. localIDTest = getUniqueID();
  23. System.out.println("ID: "+localIDTest);
  24. prepareStatement.setString(1, localIDTest);
  25. prepareStatement.setBytes(2, buf);
  26. prepareStatement.setString(3, "Pending");
  27. prepareStatement.executeUpdate();
  28. prepareStatement.close();
  29. con.commit();
  30. }catch(SQLException sqle){
  31. System.err.print(sqle);
  32. }
  33. catch(ClassNotFoundException cnfe){
  34. System.err.print(cnfe);
  35. }
  36. return false;
  37. }
  38.  
  39.  
  40. public static Reservation retrieveReservation()throws IOException,
  41. SQLException{
  42. Reservation testRsv = new Reservation();
  43. try {
  44. Connection con = null;
  45.  
  46. Class.forName("oracle.jdbc.driver.OracleDriver");
  47. con=DriverManager.getConnection(
  48. "jdbc:oracle:thin:@dsdsds",
  49. "SMBDB",
  50. "hpdbsmb");
  51. con.setAutoCommit(false);
  52. try {
  53.  
  54. PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM
  55. SMD_RESERVATION_INSTANCES WHERE id = ?");
  56. prepareStatement.setString(1, localIDTest);
  57. ResultSet rset = prepareStatement.executeQuery();
  58. prepareStatement.executeUpdate();
  59. //prepareStatement.close();
  60. con.commit();
  61. if(rset.next()){
  62. retrievedID = rset.getString("ID");
  63. Blob blob = rset.getBlob("RESERVATIONINST");
  64. status = rset.getString("STATUS");
  65. long blobLength = blob.length();
  66. int pos = 1; // position is 1-based
  67. //int len = 10;
  68. byte[] bytes = blob.getBytes(pos,(int) blobLength);
  69. InputStream is = blob.getBinaryStream();
  70. ObjectInputStream ois = new ObjectInputStream(is);
  71. testRsv=(Reservation)ois.readObject();
  72. }
  73.  
  74.  
  75. rset.close();
  76. con.close();
  77. }catch(IOException ioe){
  78. System.err.print(ioe);
  79. }
  80. }catch(ClassNotFoundException cnfe){
  81. System.err.print(cnfe);
  82. }
  83. return testRsv;
  84.  
  85. }
  86.  
  87. public static void displayRsvContent(){
  88.  
  89. try{
  90. Reservation rsvtester = new Reservation();
  91. rsvtester.badgeNo = 750752;
  92. rsvtester.networkID = "GHAMKS1C";
  93. storeInfo(rsvtester);
  94. rsvReturned = retrieveReservation();
  95. if(rsvReturned == null){
  96. System.out.println("Null Reservation!");
  97. }else{
  98. System.out.println("Badge: " + rsvReturned.badgeNo);
  99. System.out.println("Network: " + rsvReturned.networkID);
  100. }
  101.  
  102. }catch(SQLException sqle){
  103. System.err.print(sqle);
  104. }catch(IOException ioe){
  105. System.err.print(ioe);
  106. }
  107. }
  108.  
  109.  
  110. public static void main(String[]args){
  111. displayRsvContent();
  112. }
  113.  
  114. ID: ec561507-7138-4468-98a3-7756219f216e
  115. Badge: 0
  116. Network: null
  117.  
  118. public class Reservation implements
  119. java.io.Serializable{
  120.  
  121. String eventTitle;
  122. public Date startDate;
  123. public Date endDate;
  124. String requestType;
  125. public int terminals;
  126. String lastName;
  127. String firstName;
  128. String middleInitials;
  129. public transient int badgeNo;
  130. public transient String networkID;
  131. String telephoneNo;
  132. String orgCode;
  133. String orgName;
  134. String justification;
  135. String insideCheckRange;
  136. int mapSize;
  137.  
  138.  
  139.  
  140. MapStorage mapStorage = new MapStorage();
  141.  
  142.  
  143. public Reservation(int badgeNo, String networkID) {
  144. this.badgeNo = badgeNo;
  145. this.networkID = networkID;
  146. }
  147.  
  148.  
  149. public Reservation(){
  150.  
  151. }
  152.  
  153. public Reservation(String eventTitle, Date startDate, Date endDate, String requestType, int terminals, String lastName, String firstName, String middleInitials, int badgeNo, String networkID, String telephoneNo, String orgCode, String justification) {
  154. //create in here
  155.  
  156. this.eventTitle = eventTitle;
  157. this.startDate = startDate;
  158. this.endDate = endDate;
  159. this.requestType = requestType;
  160. this.terminals = terminals;
  161. this.lastName = lastName;
  162. this.firstName = firstName;
  163. this.middleInitials = middleInitials;
  164. this.badgeNo = badgeNo;
  165. this.networkID = networkID;
  166. this.telephoneNo = telephoneNo;
  167. this.orgCode = orgCode;
  168. this.justification = justification;
  169. }
  170.  
  171.  
  172.  
  173. public boolean checkRange() {
  174.  
  175.  
  176.  
  177. DateTime startx = new DateTime(startDate.getTime());
  178. DateTime endx = new DateTime(endDate.getTime());
  179.  
  180. //booking status
  181. boolean possible = false;
  182.  
  183. //Booking type: 1 = Project, 2 = Training
  184.  
  185. /*
  186. if(requestType.equals("Project")){
  187. bookingType = 1;
  188. }else if(requestType.equals("Training")){
  189. bookingType = 2;
  190.  
  191. }
  192. */
  193. //produces submap
  194. //mapSize = bookingType;
  195. TreeMap<DateTime, Integer> mapLoaded = null;
  196. try{
  197. mapLoaded = mapStorage.RetrieveMap();
  198. }catch(IOException ioe)
  199. {
  200. System.err.print(ioe);
  201. }
  202. if(requestType.equals("Project"))
  203. {
  204. //Project
  205. //insideCheckRange = "In first for loop";
  206. //fetch all values for keys in the map between start and end
  207. for (Integer capacity : mapLoaded.subMap(startx, endx).values()) {
  208.  
  209.  
  210. if(capacity >= terminals)
  211. //yes then its possible, set to true
  212. possible = true;
  213. else if(capacity < terminals)
  214. //not then set it to false
  215. possible = false;
  216.  
  217. }
  218.  
  219. if(possible == true)
  220. {
  221.  
  222. //if it is possible to accomodate request
  223. for (DateTime x : mapLoaded.subMap(startx, endx).keySet()) {
  224. {
  225. //for dates n, update value for next operation
  226. mapLoaded.put(x, mapLoaded.get(x) - terminals);
  227. }
  228. }
  229. }else{
  230. //nothing now
  231. }
  232. }else if(requestType.equals("Training")){
  233. //Training
  234. for (Integer capacity : mapLoaded.subMap(startx, endx).values()) {
  235. //Provides an insight into capacity accomodation possibility
  236. //testValue++;
  237. terminals = 1;
  238. if(capacity >= terminals)
  239. possible = true;
  240. else if(capacity < terminals)
  241. possible = false;
  242.  
  243. }
  244.  
  245.  
  246. if(possible == true)
  247. {
  248. for (DateTime x : mapLoaded.subMap(startx, endx).keySet()) {
  249. {
  250. //46 so that all seats are reserved
  251. mapLoaded.put(x, mapLoaded.get(x) - 46);
  252.  
  253. }
  254. }
  255. }else{
  256. //nothing now
  257. }
  258.  
  259. }
  260.  
  261.  
  262. return possible;
  263. }
  264.  
  265. }
  266.  
  267. PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM
  268. SMD_RESERVATION_INSTANCES WHERE id = ?");
  269. prepareStatement.setString(1, localIDTest);
  270. ResultSet rset = prepareStatement.executeQuery();
  271. prepareStatement.executeUpdate();
  272. //prepareStatement.close();
  273. con.commit();
  274.  
  275. public transient int badgeNo;
  276. public transient String networkID;
Add Comment
Please, Sign In to add comment