Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12.  
  13. public class ClientA2 extends JFrame {
  14. /** The name of the MySQL account to use (or empty for anonymous) */
  15. private final String userName = "root";
  16.  
  17. /** The password for the MySQL account (or empty for anonymous) */
  18. private final String password = "";
  19.  
  20. /** The name of the computer running MySQL */
  21. private final String serverName = "localhost";
  22.  
  23. /** The port of the MySQL server (default is 3306) */
  24. private final int portNumber = 3306;
  25.  
  26. /** The name of the database we are testing with (this default is installed with MySQL) */
  27. private final String dbName = "areadatabase";
  28.  
  29. /** The name of the table we are testing with */
  30. private final String tableName = "registeredapplicants";
  31.  
  32. int accountNumber;
  33.  
  34. Statement s = null;
  35. ResultSet rs = null;
  36.  
  37. // Text field for receiving radius
  38. private JTextField jtf = new JTextField();
  39.  
  40. // Text area to display contents
  41. private JTextArea jta = new JTextArea();
  42.  
  43. // Send Button
  44. private JButton send = new JButton("SEND");
  45.  
  46. //Exit Button
  47. private JButton exit = new JButton("EXIT");
  48.  
  49. // Last sent item
  50. private String textField = "Client";
  51.  
  52. // IO streams
  53. private DataOutputStream toServer;
  54. private DataInputStream fromServer;
  55.  
  56. public static void main(String[] args) {
  57. new ClientA2();
  58. }
  59.  
  60. @SuppressWarnings("resource")
  61. public ClientA2() {
  62. // Panel p to hold the label and text field
  63. JPanel p = new JPanel();
  64. p.setLayout(new BorderLayout());
  65. p.add(jtf, BorderLayout.CENTER);
  66. jtf.setHorizontalAlignment(JTextField.RIGHT);
  67. setLayout(new BorderLayout());
  68. add(p, BorderLayout.SOUTH);
  69. add(new JScrollPane(jta), BorderLayout.CENTER);
  70. add(new JLabel(textField), BorderLayout.NORTH);
  71. add(send, BorderLayout.WEST);
  72. add(exit, BorderLayout.EAST);
  73. send.addActionListener(new ActionListener() {
  74. public void actionPerformed(ActionEvent e) {
  75.  
  76. }
  77. });
  78. exit.addActionListener(new ActionListener() {
  79. public void actionPerformed(ActionEvent e) {
  80. System.exit(0);
  81. }
  82. });
  83.  
  84. jtf.addActionListener(new Listener()); // Register listener
  85. setTitle("Client");
  86. setSize(500, 300);
  87. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88. setVisible(true); // It is necessary to show the frame here!
  89.  
  90. try {
  91. // Create a socket to connect to the server
  92. Socket socket = new Socket("localhost", 8000);
  93.  
  94. // Create an input stream to receive data from the server
  95. fromServer = new DataInputStream(socket.getInputStream());
  96.  
  97. // Create an output stream to send data to the server
  98. toServer = new DataOutputStream(socket.getOutputStream());
  99. }
  100. catch (IOException ex) {
  101. jta.append(ex.toString() + '\n');
  102. }
  103. }
  104. /**
  105. * Get a new database connection
  106. *
  107. * @return
  108. * @throws SQLException
  109. */
  110. public Connection getConnection() throws SQLException {
  111. Connection conn = null;
  112. Properties connectionProps = new Properties();
  113. connectionProps.put("user", this.userName);
  114. connectionProps.put("password", this.password);
  115.  
  116. conn = DriverManager.getConnection("jdbc:mysql://" + this.serverName + ":" + this.portNumber + "/" + this.dbName, connectionProps);
  117.  
  118. return conn;
  119. }
  120.  
  121. // Run Method
  122. public void run() throws SQLException, ClassNotFoundException {
  123.  
  124. // Connect to MySQL
  125. try
  126. {
  127. Class.forName("com.mysql.jdbc.Driver");
  128. Connection conn = DriverManager.getConnection("jdbc:mysql://" + serverName + ":" + portNumber + "/" + dbName, userName, password);
  129. System.out.println("Connected to database");
  130. // Initialize ResultSet
  131. s = conn.createStatement();
  132. s.executeQuery ("SELECT * FROM " + tableName + " where accountNumber = " + accountNumber);
  133. rs = s.getResultSet();
  134. int accountNumber = 0;
  135. String firstName = "", lastName ="";
  136. if(rs.next())
  137. {
  138. accountNumber = rs.getInt("accountNumber");
  139. firstName = rs.getString("firstName");
  140. lastName = rs.getString("lastName");
  141. }
  142. }
  143. catch (SQLException e)
  144. {
  145. System.out.println("ERROR: Could not connect to the database");
  146. e.printStackTrace();
  147. return;
  148. }
  149. }
  150.  
  151. private class Listener implements ActionListener {
  152. @Override
  153. public void actionPerformed(ActionEvent e) {
  154. try {
  155. // Get the text from the text field
  156. double radius = Double.parseDouble(jtf.getText().trim());
  157.  
  158. // Send the radius to the server
  159. toServer.writeDouble(radius);
  160. toServer.flush();
  161.  
  162. // Get area from the server
  163. double area = fromServer.readDouble();
  164.  
  165. // Display to the text area
  166. jta.append("Radius is " + radius + "\n");
  167. jta.append("Area received from the server is " + area + "\n");
  168. }
  169. catch (IOException ex) {
  170. System.err.println(ex);
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement