Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment2;
- 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.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import java.util.Date;
- public class MultiThreadedServerA2 extends JFrame {
- private JTextArea jta = new JTextArea();
- public static void main(String[] args) {
- new MultiThreadedServerA2();
- }
- public MultiThreadedServerA2() {
- // Place text area on the frame
- setLayout(new BorderLayout());
- add(new JScrollPane(jta), BorderLayout.CENTER);
- setTitle("Server");
- setSize(500, 300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- //setupGui();// It is necessary to show the frame here!
- try {
- // Create a server socket
- ServerSocket serverSocket = new ServerSocket(8000);
- jta.append("Server started at " + new Date() + '\n');
- /*Socket socket = serverSocket.accept();
- DataInputStream inputFromClient = new DataInputStream(
- socket.getInputStream());
- DataOutputStream outputToClient = new DataOutputStream(
- socket.getOutputStream());*/
- while (true) {
- /*int accountNum = inputFromClient.readInt();
- try {
- Connection conn = null;
- conn = getConnection();
- Statement st;
- st = conn.createStatement();
- ResultSet rs=st.executeQuery("SELECT COUNT(1) FROM TABLE WHERE KEY = " + accountNum);
- jta.append(rs.toString());*/
- //Socket s1=serverSocket.accept();
- Socket socket = serverSocket.accept();
- DataInputStream inputFromClient = new DataInputStream(
- socket.getInputStream());
- DataOutputStream outputToClient = new DataOutputStream(
- socket.getOutputStream());
- ClientA2 c = new ClientA2(socket);
- //c.start();
- }
- }
- catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- public void setupGui() {
- JFrame f=new JFrame();
- JLabel labelAccountNum=new JLabel("AccountNum: ");
- JLabel labelRadius=new JLabel("Radius: ");
- JButton submit = new JButton("submit");
- submit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Connection conn = null;
- conn = getConnection();
- if (conn != null){
- String confirmString = "SELECT COUNT(1) FROM TABLE WHERE KEY = " + 1001;
- try {
- executeUpdate(conn, confirmString);
- System.out.println("Success");
- }
- catch (SQLException a) {
- System.out.println(a);
- }
- }
- }
- });
- final JTextField textAccountNum=new JTextField(20);
- final JTextField textRadius=new JTextField(20);
- final JTextField result=new JTextField(20);
- final JTextField welcome=new JTextField(20);
- JPanel p=new JPanel(new GridLayout(5,5));
- p.add(labelAccountNum);
- p.add(textAccountNum);
- p.add(labelRadius);
- p.add(textRadius);
- p.add(result);
- p.add(welcome);
- p.add(submit);
- f.add(p);
- f.setVisible(true);
- f.pack();
- }
- public static Connection getConnection() {
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/areadatabase",
- "root", "");
- if (!con.isClosed()){
- System.out.println("Successfully connected to MySQL server...");
- }
- return con;
- }
- catch(Exception e){
- System.out.println(e);
- return null;
- }
- }
- public static boolean executeUpdate(Connection conn, String command)throws SQLException {
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- stmt.executeUpdate(command);
- return true;
- }
- finally {
- if (stmt != null) {
- stmt.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement