Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- ss = new ServerSocket(Integer.valueOf(port));
- Thread t1 = new Thread()
- {
- @Override
- public void run()
- {
- BufferedReader in = null;
- PrintWriter out = null;
- Connection con = null;
- PreparedStatement stmt = null;
- Socket s = null;
- while(true)
- {
- try
- {
- System.out.println("poczatek");
- if(!ss.isBound())
- {
- s = ss.accept();
- }
- else
- {
- Thread t2 = new Thread()
- {
- Socket innerS = null;
- @Override
- public void run()
- {
- try
- {
- innerS = ss.accept();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- };
- t2.start();
- }
- System.out.println("polaczylem sie z klientem");
- in = new BufferedReader(new InputStreamReader(s.getInputStream())); //wejsciowy
- out = new PrintWriter(s.getOutputStream(), true); //wyjsciowy
- String line = in.readLine();
- if(line.matches("\\d.*")) // sygnal bazodanowy
- {
- System.out.println("Odebralem działanie: "+line);
- if(line.startsWith("1"))
- {
- line = line.substring(1,line.length());
- String login = line.split(" ")[0];
- String haslo = line.split(" ")[1];
- System.out.println("login: "+ login +" haslo: " + haslo);
- try
- {
- System.out.println("lalala");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/komunikator","root","local");
- stmt = con.prepareStatement("INSERT INTO user(login,password) VALUES (?,?)");
- stmt.setString(1, login);
- stmt.setString(2, haslo);
- stmt.executeUpdate();
- out.println("1created");
- }
- catch (SQLException ex)
- {
- if (ex.getSQLState().startsWith("23"))
- {
- out.println("1error1");
- }
- else
- {
- out.println("1error2");
- }
- ex.printStackTrace();
- }
- finally
- { // zwalnianie zasobow
- if (stmt != null)
- {
- try
- {
- stmt.close();
- }
- catch (SQLException sqlEx) { }
- stmt = null;
- }
- if(con != null)
- {
- try
- {
- con.close();
- }
- catch (SQLException sqlEx) { }
- con = null;
- }
- }
- }
- else if(line.startsWith("2"))
- {
- }
- }
- else
- { out.println("Serwer nie obsluguje "+line);
- System.out.println("Dostalem: "+line +" i to nie jest obsługiwane dzialanie");
- }
- }
- catch(Exception e)
- {
- System.out.println("Problem z połączeniem TCP");
- e.printStackTrace();
- System.exit(1);
- }
- }
- }
- };
- t1.start();
- }
- catch(Exception e)
- {
- System.out.println("Problem z socketem serwera");
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement