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();
- // Конвертируем потоки в другой тип, чтоб легче обрабатывать текстовые сообщения.
- 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(); // заставляем поток закончить передачу данных.
- 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) {
- });
- 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