Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.rmi.*;
  2. import java.rmi.registry.LocateRegistry;
  3. import java.rmi.registry.*;
  4. import java.rmi.Naming;
  5. import java.util.*;
  6. import java.io.*;
  7. import java.net.InetAddress;
  8. import java.rmi.server.UnicastRemoteObject;
  9. import java.rmi.registry.Registry;
  10. import java.rmi.registry.LocateRegistry;
  11.  
  12. /* RMIServer:
  13. * Author: Dr. Nekrouf ZIANI
  14. * Date: November 2019
  15. * © Dr. Nekrouf ZIANI 2019
  16. */
  17. public class RMIServer
  18. {
  19. private static final String HOST = "localhost";
  20. //private static final String HOST = "CUD-M652/10.0.32.31";
  21. private static JDBC dbGUI;
  22. public JDBC getJDBC()
  23. {
  24. return dbGUI;
  25. }
  26. /* main method is called when starting
  27. * the server application */
  28. public static void main(String[] args) throws Exception
  29. {
  30. System.out.println("Start the server .....");
  31. System.out.println("First, server connects to Database through its Constructor() ...");
  32. // Database connection
  33. dbGUI = new JDBC();
  34. System.out.println("Connection DONE !");
  35. // Create 2 vectors: One Vector and a One Vector of Vector objects to
  36. // test retrieving data from the database*/
  37. Vector vectNum = new Vector();
  38. Vector<Vector<Object>> vectAllNum = new Vector();
  39. // Retrieve Data; One row and set of rows (whole database table)
  40. //through methods getOneRow() and getAlRows() of JDBC
  41. System.out.println();
  42. vectNum = dbGUI.getOneRow();
  43. vectAllNum = dbGUI.getAllRows();
  44. System.out.println("And here are the 2 vectors, One row (or record) and " + "Set of rows (whoe table):");
  45. System.out.println("JDBC getOneRow() method is returning: " + vectNum);
  46. System.out.println("and JDBC getAllRows() method is returning: " + vectAllNum);
  47. System.out.println();
  48. System.out.println("Create Object to publish in the naming service through the RemoteImpl constructor with 2 vector parameters");
  49. RemoteDBImpl obj = new RemoteDBImpl(vectNum, vectAllNum);
  50. System.out.println("DONE ...");
  51. System.out.println();
  52. /* Create the URL to publish the objects */
  53. String rmiRegName = "rmi://" + HOST + "/myDB";
  54.  
  55. // or String nekrouf = "rmi://localhost/myDB";
  56.  
  57. System.out.println("The server machine name and address is: " + rmiRegName);
  58. System.out.println();
  59. System.out.println("Let's test an RMI call and display ONE record from getRemoteRecord() : ");
  60. Vector a = obj.getRemoteRecord();
  61. System.out.println(a);
  62. System.out.println();
  63.  
  64. System.out.println("Let's test an RMI call and display ALL records from getAllRecord() : ");
  65. Vector<Vector<Object>> b = obj.getAllRecords();
  66.  
  67. System.out.println(b);
  68.  
  69. InetAddress i = InetAddress.getLocalHost();
  70.  
  71. System.out.println(i.toString());
  72.  
  73. //"rmi://CUD-M652/10.0.32.31/myDB";
  74. int port = 1099;
  75.  
  76. LocateRegistry.createRegistry(port);
  77.  
  78. String url_1 = "rmi://localhost/myDB";
  79. //String url_1 = "rmi://10.0.22.27:1099/myDB";
  80. /* Publish the object in the naming service */
  81. //Naming.rebind("rmi: //localhost/myDB", obj);
  82.  
  83. Naming.rebind(url_1, obj);
  84.  
  85. System.out.println("Object bound to this address: " + url_1);
  86. System.out.println("and the object Reference obj is: " + obj);
  87. System.out.println();
  88. System.out.println("Binding success !! and server ready for requets ......");
  89. } // end of main()
  90. } // end of class RMIServer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement