Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ClientA2 extends JFrame {
- /** The name of the MySQL account to use (or empty for anonymous) */
- private final String userName = "root";
- /** The password for the MySQL account (or empty for anonymous) */
- private final String password = "";
- /** The name of the computer running MySQL */
- private final String serverName = "localhost";
- /** The port of the MySQL server (default is 3306) */
- private final int portNumber = 3306;
- /** The name of the database we are testing with (this default is installed with MySQL) */
- private final String dbName = "areadatabase";
- /** The name of the table we are testing with */
- private final String tableName = "registeredapplicants";
- int accountNumber;
- Statement s = null;
- ResultSet rs = null;
- // Text field for receiving radius
- private JTextField jtf = new JTextField();
- // Text area to display contents
- private JTextArea jta = new JTextArea();
- // Send Button
- private JButton send = new JButton("SEND");
- //Exit Button
- private JButton exit = new JButton("EXIT");
- // Last sent item
- private String textField = "Client";
- // IO streams
- private DataOutputStream toServer;
- private DataInputStream fromServer;
- public static void main(String[] args) {
- new ClientA2();
- }
- @SuppressWarnings("resource")
- public ClientA2() {
- // Panel p to hold the label and text field
- JPanel p = new JPanel();
- p.setLayout(new BorderLayout());
- p.add(jtf, BorderLayout.CENTER);
- jtf.setHorizontalAlignment(JTextField.RIGHT);
- setLayout(new BorderLayout());
- add(p, BorderLayout.SOUTH);
- add(new JScrollPane(jta), BorderLayout.CENTER);
- add(new JLabel(textField), BorderLayout.NORTH);
- add(send, BorderLayout.WEST);
- add(exit, BorderLayout.EAST);
- send.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- exit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- jtf.addActionListener(new Listener()); // Register listener
- setTitle("Client");
- setSize(500, 300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true); // It is necessary to show the frame here!
- try {
- // Create a socket to connect to the server
- Socket socket = new Socket("localhost", 8000);
- // Create an input stream to receive data from the server
- fromServer = new DataInputStream(socket.getInputStream());
- // Create an output stream to send data to the server
- toServer = new DataOutputStream(socket.getOutputStream());
- }
- catch (IOException ex) {
- jta.append(ex.toString() + '\n');
- }
- }
- /**
- * Get a new database connection
- *
- * @return
- * @throws SQLException
- */
- public Connection getConnection() throws SQLException {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", this.userName);
- connectionProps.put("password", this.password);
- conn = DriverManager.getConnection("jdbc:mysql://" + this.serverName + ":" + this.portNumber + "/" + this.dbName, connectionProps);
- return conn;
- }
- // Run Method
- public void run() throws SQLException, ClassNotFoundException {
- // Connect to MySQL
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://" + serverName + ":" + portNumber + "/" + dbName, userName, password);
- System.out.println("Connected to database");
- // Initialize ResultSet
- s = conn.createStatement();
- s.executeQuery ("SELECT * FROM " + tableName + " where accountNumber = " + accountNumber);
- rs = s.getResultSet();
- int accountNumber = 0;
- String firstName = "", lastName ="";
- if(rs.next())
- {
- accountNumber = rs.getInt("accountNumber");
- firstName = rs.getString("firstName");
- lastName = rs.getString("lastName");
- }
- }
- catch (SQLException e)
- {
- System.out.println("ERROR: Could not connect to the database");
- e.printStackTrace();
- return;
- }
- }
- private class Listener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- // Get the text from the text field
- double radius = Double.parseDouble(jtf.getText().trim());
- // Send the radius to the server
- toServer.writeDouble(radius);
- toServer.flush();
- // Get area from the server
- double area = fromServer.readDouble();
- // Display to the text area
- jta.append("Radius is " + radius + "\n");
- jta.append("Area received from the server is " + area + "\n");
- }
- catch (IOException ex) {
- System.err.println(ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement