<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>
<%
 /*
 try {
 String a = request.getParameter("data");
 System.out.println("!!!try");
 }
 catch(Exception e){
 System.out.println("!!!");
 System.out.println(e);
 System.out.println("!!!");
 }
 */
  if (request.getParameter("data")==null) 
  {
   System.out.println("data is null");
 %>
<i>Add new Band:</i>
<br><br>

Name:       <br><input id="bandnameField" type="text" size="25" maxlength="40"><br>
Picture Url:<br><input id="picField" type="text" size="25" maxlength="256"><br>
Description:<br><textarea cols="60" rows="6" id="descField">
</textarea>
<br>
<button onclick="javascript:addBand()">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");


System.out.println(data);


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


String name = data.substring(0,delimiter);
String picurl = data.substring(delimiter+2,delimiter2);
String desc = data.substring(delimiter2+3);


desc = desc.replaceAll("\n","<br>");
desc = desc.replaceAll("'","\\\\'");
desc = desc.replaceAll("\"","\\\\''");
System.out.println(name);
System.out.println(picurl);
System.out.println(desc);


		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();
               
               
               
               
               int m;
               
               String insert_statement = ("insert into bands(name, picurl, description) VALUES ('"+name+"','"+picurl+"','"+desc+"');");
               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 */ }
               }
           }
           
    




}
%>