Guest User

Untitled

a guest
Jul 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import java.rmi.*;
  2.  
  3. public interface KMeans extends Remote {
  4.  
  5. VectorSerializable[] getClusters() throws RemoteException;
  6.  
  7. }
  8.  
  9. ----------------------
  10.  
  11. import java.io.Serializable;
  12. import java.rmi.*;
  13. import java.rmi.server.*;
  14. import java.sql.*;
  15.  
  16. import dbkmeans.*;
  17.  
  18. public class KMeansImpl extends UnicastRemoteObject implements KMeans, Serializable {
  19.  
  20. private static final long serialVersionUID = 1L;
  21. private String nome;
  22.  
  23. public KMeansImpl(String n) throws RemoteException {
  24. nome = n;
  25. }
  26.  
  27. public VectorSerializable[] getClusters() throws RemoteException {
  28. VectorSerializable dataPoints = new VectorSerializable();
  29. Connection con = null;
  30. Statement stmt = null;
  31. ResultSet rs = null;
  32. VectorSerializable[] v = null;
  33.  
  34. try{
  35. Class.forName("com.mysql.jdbc.Driver").newInstance();
  36. con = DriverManager.getConnection("jdbc:mysql://localhost/SEI?user=root&password=d4t4b4s3");
  37. System.out.println("Connessione al DB SEI Riuscita");
  38. stmt = con.createStatement();
  39. rs = stmt.executeQuery("select * from giocatori");
  40.  
  41. while (rs.next()) {
  42. String nome = rs.getString("Nome_giocatore");
  43. int prezzo = rs.getInt("Prezzo");
  44. dataPoints.add(new DataPoint(0, prezzo, nome));
  45. }
  46.  
  47. System.out.println("Sono arrivato fin qui.");
  48. JCA jca = new JCA(3, 1000, dataPoints);
  49. jca.startAnalysis();
  50.  
  51. v = jca.getClusterOutput();
  52. System.out.println("Ora fin arrivato fin qui.");
  53. System.out.println("Taglia: "+v.length);
  54. }
  55. catch(Exception e){
  56. System.out.println("Connessione al DB SEI Rifiutata");
  57. System.exit(0);
  58. }
  59. return v;
  60. }
  61.  
  62. public static void main(String[] args) {
  63. try {
  64. KMeansImpl km1 = new KMeansImpl("KMeans1");
  65. Naming.rebind("KMeans1", km1);
  66. System.out.println("Attesa Client...");
  67. }
  68. catch(Exception e) { e.printStackTrace(); }
  69. }
  70.  
  71. }
  72.  
  73. ----------------------
  74.  
  75. import java.rmi.*;
  76. import java.util.Iterator;
  77. import dbkmeans.DataPoint;
  78.  
  79. public class Client {
  80.  
  81. public static void main(String[] args) {
  82.  
  83. try {
  84. System.out.println("Provo la connessione\n");
  85. KMeans km1 = (KMeans) Naming.lookup("rmi://localhost/KMeans1");
  86. System.out.println("Chiedo il vector\n");
  87. VectorSerializable[] v = km1.getClusters();
  88. System.out.println("Ho ricevuto il vector\n");
  89. for (int i = 0; i < v.length; i++) {
  90. VectorSerializable tempV = v[i];
  91. System.out.println("-----------Cluster" + i + "-----------");
  92. Iterator iter = tempV.iterator();
  93. while (iter.hasNext()) {
  94. DataPoint dpTemp = (DataPoint) iter.next();
  95. System.out.println(dpTemp.getObjName() + "[" + dpTemp.getX() + "," + dpTemp.getY() + "]");
  96. }
  97. }
  98. } catch(Exception e) { e.printStackTrace(); }
  99. }
  100. }
  101.  
  102. ----------------------
  103.  
  104. import java.io.Serializable;
  105. import java.util.Vector;
  106.  
  107. public class VectorSerializable extends Vector implements Serializable {
  108.  
  109. }
Add Comment
Please, Sign In to add comment