Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: add: //control filtering for parameters //optimize UI //able to change port after server is closed //modifiable IP address //notify a successful SQL parsing //notify

JAVA:

add:

//control filtering for parameters //optimize UI //able to change port after server is closed //modifiable IP address //notify a successful SQL parsing //notify a failed SQL parsing

//----------------------------------------------------------------------------------------------

//make a server-socket app that reads json from socket and parses it into the console. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import org.json.*; public class Main extends JFrame{ private JTextArea jsonArea; private JTextField portField; private JButton startButton; private JLabel ipLabel; private Socket socket; ServerSocket server; public Main(){ super("JSON Server"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); jsonArea = new JTextArea(); jsonArea.setEditable(true); jsonArea.setBackground(Color.LIGHT_GRAY); JScrollPane scrollPane = new JScrollPane(jsonArea); add(scrollPane, BorderLayout.CENTER); String css = "JButton { background-color: #4CAF50; color: white; font-weight: bold; }"; startButton = new JButton("Start Server"); startButton.setFont(new Font("Tahoma", Font.PLAIN, 20)); startButton.setBounds(496, 505 , 200, 50); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int port = Integer.parseInt(portField.getText()); jsonArea.setText("Listening: "); jsonArea.repaint(); startServer(port); } }); JPanel portPanel = new JPanel(); portPanel.add(new JLabel("Port: ")); portField = new JTextField(20); portPanel.add(portField); add(portPanel, BorderLayout.WEST); add(startButton, BorderLayout.SOUTH); JButton stopButton = new JButton("Stop Server"); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stopServer(); } }); add(stopButton, BorderLayout.EAST); ipLabel = new JLabel(); try { InetAddress ip = InetAddress.getLocalHost(); ipLabel.setText("IP address:\t" + ip.getHostAddress()); } catch (UnknownHostException e) { ipLabel.setText("Error: Could not get IP address"); } add(ipLabel, BorderLayout.NORTH); setVisible(true); } public void startServer(int port) { jsonArea.setText("Listening: "); jsonArea.repaint(); try { server = new ServerSocket(port); jsonArea.append("Server started on port " + port + " "); while (true) { socket = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = in.readLine(); JSONObject json = new JSONObject(line); jsonArea.append(json.toString(4) + " "); DBClass db = new DBClass(); JSONObject jsonData = new JSONObject(); //iterate over the keys in the JSON object Iterator keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); //check if the key is one of the expected fields if (key.equals("Name") || key.equals("Surname") || key.equals("City") || key.equals("University")) { jsonData.put(key, json.get(key)); } else { jsonArea.append("Error: Unexpected field '" + key + "' in JSON "); } } //control filtering for parameters //optimize UI //able to change port after server is closed //modifiable IP address //notify a successful SQL parsing //notify a failed SQL parsing if (jsonData.length() == 4) { db.saveDataToDatabase(jsonData.getString("Name"), jsonData.getString("Surname"), jsonData.getString("City"), jsonData.getString("University")); jsonArea.append("Successfully parsed JSON and saved to database "); } else { jsonArea.append("Error: Missing required fields in JSON "); } } } catch (IOException e) { jsonArea.append("Error: " + e.getMessage() + " "); } } public void stopServer() { try { socket.close(); server.close(); jsonArea.append(" Connection Terminated "); } catch (IOException e) { jsonArea.append("Error: " + e.getMessage() + " "); } } public static void main(String[] args) { Main main = new Main(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago