Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package networkclientserver;
  2.  
  3.  
  4. import java.net.*;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16.  
  17. class ConnectionCW implements Runnable
  18. {
  19. private Socket s;
  20. private ObjectOutputStream ooos;
  21. private ObjectInputStream oois;
  22. ResultSet rs;
  23.  
  24.  
  25. ConnectionCW(Socket s)
  26. {
  27. this.s = s;
  28. }
  29.  
  30.  
  31. public void run()
  32. {
  33. try
  34. {
  35. ObjectOutputStream ooos = new ObjectOutputStream(this.s.getOutputStream());
  36. ObjectInputStream oois = new ObjectInputStream(this.s.getInputStream());
  37.  
  38. this.getTopology();
  39. this.ooos.writeObject(getTopology());
  40. this.ooos.flush();
  41.  
  42.  
  43.  
  44. } catch (Exception ex)
  45. {
  46. Logger.getLogger(ConnectionCW.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48. } // end of method run*/
  49.  
  50.  
  51.  
  52.  
  53. }
  54.  
  55. public ArrayList<ReadNet> getTopology() throws Exception
  56. {
  57. ArrayList<ReadNet> getTopology2 = new ArrayList<ReadNet>();
  58.  
  59. Connection conn2 = getConnection();
  60.  
  61.  
  62. String query = "SELECT * FROM datatopology ";
  63. Statement st;
  64. st=conn2.createStatement();
  65. rs=st.executeQuery(query);
  66. ReadNet readNet;
  67. while(rs.next())
  68. {
  69. readNet = new ReadNet(rs.getInt("id"),rs.getInt("numberNodes"),rs.getInt("numberHubs"),rs.getInt("numberSwitches"),rs.getString("topologyStructure"),rs.getString("country"),rs.getString("status"));
  70. getTopology2.add(readNet);
  71. }
  72. return getTopology2;
  73. }
  74.  
  75.  
  76.  
  77. public Connection getConnection() throws Exception
  78. {
  79. try
  80. {
  81. String driver = "com.mysql.jdbc.Driver";
  82. String url = "jdbc:mysql://localhost:3306/networkstopology";
  83. String username = "user";
  84. String password = "pass";
  85. Class.forName(driver);
  86.  
  87.  
  88. Connection conn = DriverManager.getConnection(url, username, password);
  89. System.out.println("Connected to MySQL!");
  90. return conn;
  91.  
  92.  
  93. } catch (Exception e)
  94. {
  95. System.out.println(e);
  96. }
  97. return null;
  98. }
  99. }
  100.  
  101. private void buttonReadActionPerformed(java.awt.event.ActionEvent evt)
  102. {
  103.  
  104. try
  105. {
  106. ArrayList<ReadNet> setTopology = (ArrayList<ReadNet>) ios.readObject();
  107. DefaultTableModel model = (DefaultTableModel) topologyTable1.getModel();
  108. Object[] row = new Object[7];
  109. for (int i = 0; i < setTopology.size(); i++)
  110. {
  111. row[0] = setTopology.get(i).getId();
  112. row[1] = setTopology.get(i).getNNodes();
  113. row[2] = setTopology.get(i).getNHubs();
  114. row[3] = setTopology.get(i).getNSwitches();
  115. row[4] = setTopology.get(i).getCountry();
  116. row[5] = setTopology.get(i).getTopology();
  117. row[6] = setTopology.get(i).getStatus();
  118. model.addRow(row);
  119. }
  120. } catch (IOException ex)
  121. {
  122. Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
  123. } catch (ClassNotFoundException ex)
  124. {
  125. Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
  126. }
  127. }
  128.  
  129. package networkclientserver;
  130.  
  131. import java.io.Serializable;
  132.  
  133. class ReadNet implements Serializable
  134. {
  135. int id;
  136. int nnodes;
  137. int nhubs;
  138. int nswitch;
  139. String topology;
  140. String country;
  141. String status;
  142.  
  143.  
  144. ReadNet(int id,int nodes, int hubs, int switches, String countries, String topology, String status)
  145. {
  146. this.id=id;
  147. this.nnodes = nodes;
  148. this.nhubs = hubs;
  149. this.nswitch = switches;
  150. this.country = countries;
  151. this.topology = topology;
  152. this.status = status;
  153. }
  154.  
  155. public int getId()
  156. {
  157. return this.id;
  158. }
  159. public int getNNodes()
  160. {
  161. return this.nnodes;
  162. }
  163.  
  164. public int getNHubs()
  165. {
  166. return this.nhubs;
  167. }
  168.  
  169. public int getNSwitches()
  170. {
  171. return this.nswitch;
  172. }
  173.  
  174. String getTopology()
  175. {
  176. return this.topology;
  177. }
  178.  
  179. String getCountry()
  180. {
  181. return this.country;
  182. }
  183.  
  184. String getStatus()
  185. {
  186. return this.status;
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement