Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NewConnection extends Thread
- {
- Socket s;
- int num;
- NewConnection(int num, Socket s)
- {
- this.num = num;
- this.s = s;
- setDaemon(true);
- setPriority(NORM_PRIORITY);
- start();
- System.out.println("Got client");
- }
- public void run()
- {
- try
- {
- InputStream sin = s.getInputStream();
- OutputStream sout = s.getOutputStream();
- // Конвертируем Stream'ы в другой тип, чтоб легче обрабатывать
- // текстовые сообщения.
- DataInputStream in = new DataInputStream(sin);
- DataOutputStream out = new DataOutputStream(sout);
- String url = null;
- // Ожидаем, пока клиент пришлёт строку текста.
- while (true)
- {
- url = "jdbc:mysql://localhost:3306/Clients";
- System.out.println("sending url");
- out.flush(); // Заставляем Stream закончить передачу данных.
- System.out.println("Waiting for the next line...");
- System.out.println();
- }
- }
- catch (Exception x)
- {
- x.printStackTrace();
- }
- }
- }
- connect.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- thread = new Thread(new Runnable()
- {
- public void run()
- {
- int serverPort = 6666;
- String address = "127.0.0.1";
- try
- {
- InetAddress ipAddress = InetAddress.getByName(address);
- socket = new Socket(ipAddress, serverPort);
- InputStream sin = socket.getInputStream();
- OutputStream sout = socket.getOutputStream();
- // Конвертируем потоки в другой тип, чтоб легче
- // обрабатывать текстовые сообщения.
- DataInputStream in = new DataInputStream(sin);
- DataOutputStream out = new DataOutputStream(sout);
- System.out.println("You are connected to " + address
- + " and port " + serverPort);
- connected = true;
- }
- catch (Exception x)
- {
- // errorFrameFunc();
- new Error("Connection Error", "Server is down");
- }
- // // Создаем поток для чтения с клавиатуры.
- // BufferedReader keyboard = new BufferedReader(new
- // InputStreamReader(System.in));
- // String line = null;
- // System.out.println("Type in something and press
- // enter. Will send it to the server and tell ya what it
- // thinks.");
- // System.out.println();
- // while (true) {
- // /*line = keyboard.readLine(); // ждем пока
- // пользователь введет что-то и нажмет кнопку Enter.
- // System.out.println("Sending this line to the
- // server...");
- // out.writeUTF(line); // отсылаем введенную строку
- // текста серверу.*/
- //// out.flush(); // заставляем поток закончить передачу
- // данных.
- //// line = in.readUTF(); // ждем пока сервер отошлет
- // строку текста.
- // }
- }
- });
- thread.setDaemon(true);
- thread.start();
- }
- });
- oK.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- if (groupTextField.getText().equals("") || nameTextField.getText().equals("")
- || surnameTextField.getText().equals("") || passwordTextField.getPassword().length == 0)
- new Error("Empty error", "One or all fields is empty,please enter the information");
- else
- {
- if (!Arrays.equals(passwordTextField.getPassword(), passwordConTextField.getPassword()))
- new Error("Password Error", "Passwords do not match, enter the same password");
- else
- {
- String queryCheck = "select Groups,Name,Surname from clients";
- String query = "INSERT INTO Clients.clients(Groups,Name,Surname,Password) " + " VALUES ('"
- + groupTextField.getText() + "','" + nameTextField.getText() + "','"
- + surnameTextField.getText() + "','" + new String(passwordTextField.getPassword())
- + "');";
- try
- {
- // opening database connection to MySQL server
- con = DriverManager.getConnection(url, user, passwordForRoot);
- // getting Statement object to execute query
- stmt = con.createStatement();
- // executing SELECT query
- rs = stmt.executeQuery(queryCheck);
- while (rs.next())
- {
- if (rs.getString(1).equals(groupTextField.getText())
- && rs.getString(2).equals(nameTextField.getText())
- && rs.getString(3).equals(surnameTextField.getText()))
- {
- new Error("Row error", "The same account is already exist");
- break;
- }
- }
- stmt.executeUpdate(query);
- }
- catch (SQLException sqlEx)
- {
- sqlEx.printStackTrace();
- }
- finally
- {
- // close connection ,stmt and resultset here
- try
- {
- con.close();
- }
- catch (SQLException se)
- {
- /* can't do anything */ }
- try
- {
- stmt.close();
- }
- catch (SQLException se)
- {
- /* can't do anything */ }
- try
- {
- rs.close();
- }
- catch (SQLException se)
- {
- /* can't do anything */ }
- }
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement