package mytags;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;
import java.sql.*;  

public class GetStudent extends TagSupport {
    private String name;  
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/cs4010";
    static final String USER = "cs4010";
    static final String PASS = "cs4010";

    public void setName(String name) {  //A convention for naming the setter.
        this.name = name;  
    }  

    public int doStartTag() throws JspException {  
        JspWriter out=pageContext.getOut();  
        try {
            out.print(TheStudent(name));  
        } catch (Exception e) {
            e.printStackTrace();
        }

        return SKIP_BODY;  
    } 
        public String TheStudent(String who){
        Connection conn = null;
        Statement stmt = null;
        String theReturn="";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String this_query="SELECT name, year, test FROM student where name=\"";
            this_query=this_query+who+"\";";
            ResultSet rs = stmt.executeQuery(this_query);
            while (rs.next()) {
                String the_name  = rs.getString("name");               
                String the_year = rs.getString("year");
                int the_grade = rs.getInt("test");
                theReturn="Name: " + the_name + " Year: "+the_year+" Grade: "+the_grade;
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theReturn;
    } 
}