<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>
<%
 
  String band_id="-1";  
  String bandname= "";  
  if (request.getParameter("data")==null) 
   {
   bandname = request.getParameter("bandname");
   band_id = request.getParameter("band_id");
   out.println("<div class=\"invis\" id=\"band_id\">"+band_id+"</div>");  
   out.println("<div class=\"invis\" id=\"bandname\">"+bandname+"</div>");  
 %>
<i>Add new Tag:</i>
<br><br>

New Tag:    <br><input id="newTagField" type="text" size="25" maxlength="40"><br>
<br>
<button onclick="javascript:addTag()">add</button>
<div id="isPost"></div>
<br>      
<br>      

<% }

else{


String data = (request.getParameter("data")); 


System.out.println(request.getCharacterEncoding());
System.out.println(request.getContentLength());
System.out.println(request.getContentType());
System.out.println(request.getParameterNames());

//data= URLDecoder.decode(data,"UTF-8");

int delimiter = data.indexOf("||");



band_id= data.substring(0,delimiter);
data = data.substring(delimiter+2);

System.out.println(band_id);
System.out.println(data);


		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");
               
               Statement s;
               String insert_statement="";
               String query="";
               ResultSet rs=null;
               //s.executeQuery rs = s.getResultSet()
               
               query =("select 1 from tags where name='"+data+"'");
               
               System.out.println(query);
               s = conn.createStatement();
               s.executeQuery(query);               
               rs = s.getResultSet();
               
               boolean newTag = true;
               
               while(rs.next())
               newTag=false;
               
               System.out.println(newTag);
               
               if (newTag) {
               s = conn.createStatement();
               insert_statement = ("insert into tags(name) VALUES ('"+data+"')");
               System.out.println(insert_statement);
               s.executeUpdate(insert_statement);
               
               }
               
               
               query =("select id from tags where name='"+data+"'");
               
               System.out.println(query);               
               s = conn.createStatement();
               s.executeQuery(query);
               rs = s.getResultSet();
               
               int tag_id=-1;
               
               while(rs.next()){
               tag_id = rs.getInt("id");
               }
               
               
               
               
               int m;
               s = conn.createStatement();
               insert_statement = ("insert into tagged(tag_id, band_id) VALUES ('"+tag_id+"','"+band_id+"');");
               System.out.println(insert_statement);
               m = s.executeUpdate(insert_statement);
               
               if (m==1) out.println("Added!");
               else out.println("NOT Added!");
               
               
               
          }        
          
          
           catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
               out.println("Error");
               return;
           }
           
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
           
    




}
%>













<%
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");
    
      Statement s = conn.createStatement ();
      
		String	query= ("select name from tags where id in (select tag_id from tagged where band_id = "+band_id+")");
			System.out.println(query);			
			s.executeQuery (query);
			ResultSet rs = s.getResultSet ();
      
      String tagname="";
      
      
      out.println("<div class=\"bandtags\">");
      out.println("<br><b>Artist tags:</b><br><br>");
		while (rs.next ())
		{
			tagname = rs.getString ("name");
			out.println("<div class=\"suggest_link\"onmouseover=\"javascript:suggestOver(this);\" onmouseout=\"javascript:suggestOut(this);\" onclick=\"javascript:getBandsFromTag('"+tagname+"','main_window')\"   >");
      out.println(tagname+"<br>");      
      out.println("</div>");      
		}
		
		out.println("</div>");
		
		rs.close ();
		s.close ();
		}      
		
		          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 */ }
               }
           }
   
 %>
