<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>

<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>


<%

		Connection conn = null;

           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost/band";
               Class.forName ("com.mysql.jdbc.Driver").newInstance();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           
			String like = "";
			String andor="";
			String tags="";
			
            like = request.getParameter("like")	;
            System.out.println("like -> "+like);
            if (request.getParameter("andor").equals("") || request.getParameter("tags").equals("")) {
            System.out.println("empty.."); 
            return;
            
        } else {
            
           
           andor=request.getParameter("andor");
           tags=request.getParameter("tags");
        
           }
           
                      
              Statement s = conn.createStatement ();
			String query= "Select * from tags where name like '%"+like+"%' limit 15";
			System.out.println(query);
			s.executeQuery (query);
			ResultSet rs = s.getResultSet ();
              
              
              ArrayList ar = new ArrayList();
             int count=0;              
		while (rs.next ())
		{
			int idVal = rs.getInt ("id");
			String tagname = rs.getString ("name");
			System.out.println(idVal+" "+tagname);
			ar.add(new ajax.Tag(idVal,tagname));									
			count++;
		}
		rs.close ();
		s.close ();
           String foo="";
           for (int i=0; i<ar.size();i++) {
           String the_name=(((ajax.Tag)(ar.get(i))).getTagName());
           foo=(the_name+"<br>");
           String escTag = tags.replaceAll(" ","+");
           //System.out.println(andor);
           out.println("<div class=\"suggest_link\" onmouseover=\"javascript:suggestOver(this);\" onmouseout=\"javascript:suggestOut(this);\" onclick=\"javascript:getAjax('getBandsFromTags.jsp?andor="+andor+"&tag="+tags+"|"+the_name+"|','main_window');\" >"+foo+"</div>");
          //  out.println("asd");
           }
           if (count==15) {
           out.println("<div class=\"suggest_link\" onmouseover=\"javascript:suggestOver(this);\" onmouseout=\"javascript:suggestOut(this);\"  >"+"..."+"</div>");
           }
           
           }
           
	
           
                      catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
           }
           
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
           

%>