Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package assignment2;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.*;
  10. import java.awt.*;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13.  
  14. import javax.swing.*;
  15.  
  16. import java.util.Date;
  17. public class MultiThreadedServerA2 extends JFrame {
  18. private JTextArea jta = new JTextArea();
  19.  
  20. public static void main(String[] args) {
  21. new MultiThreadedServerA2();
  22. }
  23. public MultiThreadedServerA2() {
  24. // Place text area on the frame
  25. setLayout(new BorderLayout());
  26. add(new JScrollPane(jta), BorderLayout.CENTER);
  27. setTitle("Server");
  28. setSize(500, 300);
  29. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. setVisible(true);
  31. //setupGui();// It is necessary to show the frame here!
  32. try {
  33. // Create a server socket
  34. ServerSocket serverSocket = new ServerSocket(8000);
  35. jta.append("Server started at " + new Date() + '\n');
  36.  
  37.  
  38. /*Socket socket = serverSocket.accept();
  39. DataInputStream inputFromClient = new DataInputStream(
  40. socket.getInputStream());
  41. DataOutputStream outputToClient = new DataOutputStream(
  42. socket.getOutputStream());*/
  43.  
  44. while (true) {
  45. /*int accountNum = inputFromClient.readInt();
  46. try {
  47. Connection conn = null;
  48. conn = getConnection();
  49. Statement st;
  50. st = conn.createStatement();
  51. ResultSet rs=st.executeQuery("SELECT COUNT(1) FROM TABLE WHERE KEY = " + accountNum);
  52. jta.append(rs.toString());*/
  53. //Socket s1=serverSocket.accept();
  54. Socket socket = serverSocket.accept();
  55. DataInputStream inputFromClient = new DataInputStream(
  56. socket.getInputStream());
  57. DataOutputStream outputToClient = new DataOutputStream(
  58. socket.getOutputStream());
  59. ClientA2 c = new ClientA2(socket);
  60. //c.start();
  61. }
  62. }
  63. catch (IOException e1) {
  64. e1.printStackTrace();
  65. }
  66. }
  67. public void setupGui() {
  68. JFrame f=new JFrame();
  69. JLabel labelAccountNum=new JLabel("AccountNum: ");
  70. JLabel labelRadius=new JLabel("Radius: ");
  71. JButton submit = new JButton("submit");
  72. submit.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. Connection conn = null;
  75. conn = getConnection();
  76. if (conn != null){
  77. String confirmString = "SELECT COUNT(1) FROM TABLE WHERE KEY = " + 1001;
  78. try {
  79. executeUpdate(conn, confirmString);
  80. System.out.println("Success");
  81. }
  82. catch (SQLException a) {
  83. System.out.println(a);
  84. }
  85. }
  86. }
  87. });
  88. final JTextField textAccountNum=new JTextField(20);
  89. final JTextField textRadius=new JTextField(20);
  90. final JTextField result=new JTextField(20);
  91. final JTextField welcome=new JTextField(20);
  92. JPanel p=new JPanel(new GridLayout(5,5));
  93. p.add(labelAccountNum);
  94. p.add(textAccountNum);
  95. p.add(labelRadius);
  96. p.add(textRadius);
  97. p.add(result);
  98. p.add(welcome);
  99. p.add(submit);
  100. f.add(p);
  101. f.setVisible(true);
  102. f.pack();
  103. }
  104. public static Connection getConnection() {
  105. try{
  106. Class.forName("com.mysql.jdbc.Driver").newInstance();
  107. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/areadatabase",
  108. "root", "");
  109. if (!con.isClosed()){
  110. System.out.println("Successfully connected to MySQL server...");
  111. }
  112. return con;
  113. }
  114. catch(Exception e){
  115. System.out.println(e);
  116. return null;
  117. }
  118. }
  119. public static boolean executeUpdate(Connection conn, String command)throws SQLException {
  120. Statement stmt = null;
  121. try {
  122. stmt = conn.createStatement();
  123. stmt.executeUpdate(command);
  124. return true;
  125. }
  126. finally {
  127. if (stmt != null) {
  128. stmt.close();
  129. }
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement