import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class chat_servlet extends HttpServlet
{
int total_regs;
entidade[] ents = new entidade[15000];	
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException 
{
	response.setContentType("text/html");
	final PrintWriter sos = response.getWriter();
 
	Thread th = new Thread()
	{
	   	public void run()
		{
			while(true)
			{
				
			try
				{
			   		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
					final Connection conn;
				   	conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://LINCOLN:1433;databasename=chat","sa","");
	
				 	Statement stmt = conn.createStatement();
	 				ResultSet rs = stmt.executeQuery("select * from chat where flag = 1"); 
					while(rs.next())
						{
							String texto = rs.getString("texto");
							sos.println(texto);
							Statement stmt2 = conn.createStatement();
							stmt2.executeUpdate("update chat set flag = 2 where id_chat = " + rs.getString("id_chat"));
							stmt.close();
							stmt2.close();
						}
					rs.close();
					sos.flush();
					try
					{
						Thread.sleep(5000);	
					}catch(InterruptedException e){System.out.println(e);}
		}
					catch(Exception e)
					{
						System.out.println(e);
					}			}
  		}
  	};
  	th.start();
}
}
