import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public final class testMatricula extends HttpServlet
{
Connection conexion=null;
Statement sentencia=null;
ResultSet tabla= null;
public void Show(PrintWriter out) throws Exception
{ conexion=getMySqlConnection();
sentencia=conexion.createStatement();
boolean isResult=sentencia.execute("SELECT Alumnos.appaterno, apmaterno, nombres, dia, mes, anio, depart, "+
"provinc, distrito, estcivil, dni, nacion, gradinst, ocupac, direcact, distact, "+
"Carrera.nomcarrera, Turno.nomturno FROM Alumnos, Carrera, Turno "+
"WHERE (Alumnos.idcarrera=Carrera.idcarrera) and (Alumnos.idturno=Turno.idturno) "+
"and (Alumnos.idalumno='"+codigo+"')");
if(isResult)
{ ResultSet r=sentencia.getResultSet();
if (r!=null)
{ ResultSetMetaData md=r.getMetaData();
int fCount = md.getColumnCount();
out.println("<table border=\"1\">");
out.println("<tr>");
for(int k=1;k<=fCount;k++)
out.println("<td>"+md.getColumnName(k)+"</td>");
out.println("</tr>");
while(r.next())
{ out.println("<tr>");
for(int k=1;k<=fCount;k++)
out.println("<td>"+setMask(r.getString(k))+"</td>");
out.println("</tr>");
}
out.println("</table>");
}
conexion.close();
}
}
public String setMask(String value)
{ if (value.indexOf("á")>1) value=value.replaceFirst("á","á");
if (value.indexOf("é")>1) value=value.replaceFirst("é","é");
if (value.indexOf("í")>1) value=value.replaceFirst("í","í");
if (value.indexOf("ó")>1) value=value.replaceFirst("ó","ó");
if (value.indexOf("ú")>1) value=value.replaceFirst("ú","ú");
return value;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ response.setContentType("text/html");
PrintWriter out = response.getWriter(); //Dispositivo de salida
out.println("<html>");
out.println("<head>");
out.println("<title>Ficha de Alumno</title>");
//out.println("<link REL='stylesheet' TYPE='text/css' HREF='../../style.css' TITLE='formal'>");
out.println("</head>");
out.println("<body>");
out.println("<b>Ficha de Alumno</b>");
out.println("<p>");
try
{ Show(out);
} catch(Exception a)
{ out.println(setMask("Error de conexión...!"));
}
out.println("</body>");
out.println("</html>");
}
public static Connection getMySqlConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/instituto";
Connection conn = DriverManager.getConnection(url, "root", "");
return conn;
}
}