Sie sind auf Seite 1von 8

Consultas en JSP MYSQL Ejemplo # 01

<%@page import="java.sql.*" %> <%@page pageEncoding="UTF-8"%>

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Consultar</title> </head> <body background="C:\Users\Gregorio\Desktop\Norbert Wiener 2013 - II/Fondoweb.jpg"> <form name="consultar" action="" method="">

<center> <h1>LISTADO DE Empleados - JSP</h1> <p></p> <table border="1">

<tr align="center"> <td><strong>Nonbre a Buscar</strong></td> <td colspan="3"><strong><input type="text" name="txtbuscar" value="" /></strong></td> <td><strong><input type="submit" value="Consultar" name="btnconsultar" /></strong></td> </tr> <tr> <td><strong>Codigo</strong></td> <td><strong>Nombres</strong></td> <td ><strong>Distrito</strong></td> <td ><strong>Sexo</strong></td> <td ><strong>Edad</strong></td>

</tr>

<% String nombre; nombre=request.getParameter("txtbuscar");

try { Class.forName("com.mysql.jdbc.Driver"); Connection cn=DriverManager.getConnection("jdbc:mysql://localhost/norberwiener","root","123"); out.println("Conexin realizada con xito a: "+cn.getCatalog());

String acceso="select*from empleado where Nom_Emp LIKE'"+nombre+"%'"; Statement sentencia=cn.createStatement(); ResultSet rs=sentencia.executeQuery(acceso); rs.beforeFirst(); while(rs.next())

{%> <tr> <td align="center"><%out.print(rs.getString(1));%></td> <td align="center"><%out.print(rs.getString(2));%></td> <td align="center"><%out.print(rs.getString(3));%></td> <td align="center"><%out.print(rs.getString(4));%></td> <td align="center"><%out.print(rs.getString(5));%></td> </tr> <%}%> <%}

catch(SQLException ex) { out.print("Se produjo un error durante la conexion:"+ex); } catch(Exception ex) { out.print("se produjo una exception:"+ex);

}%> </table> </center>

</form>

</body> </html>

Ejemplo # 02

<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Facultad de Ingeniera de Sistemas</title> </head> <body background="C:\Users\Gregorio\Desktop\Norbert Wiener 2013 - II/Fondoweb.jpg"> <h1>Bienvenidos al Sistema</h1> </body> </html>

Ejemplo # 03

Formulario Login

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body background="C:\Users\Gregorio\Desktop\Norbert Wiener 2013 - II/Fondoweb.jpg"> <form name="datos" action="validar_usuario.jsp" method="POST"> <table border="1" align="center"> <tr> <td colspan="2" align="center"> Control de Acceso</td> </tr>

<tr> <td align="right">Usuario:</td> <td align="center"><input type="text" name="txtnombre" value="" /></td> </tr> <tr> <td align="right">Password:</td> <td align="center"><input type="password" name="txtpassword" value=""/> </td> </tr> <tr> <td colspan="2" align="center"><input type="reset" value="Nuevo" name="btnnuevo" /> <input type="submit" value="Ingresar" name="btningresar" /></td>

</tr> </table>

</form </body> </html>

Emjemplo # 04

Cdigo para validar el login del usuario

<%@page import="java.sql.*" %> <%@page pageEncoding="UTF-8"%>

<html>

<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body background="C:\Users\Gregorio\Desktop\Norbert Wiener 2013 - II/Fondoweb.jpg"> <% String nombre; String password; nombre=request.getParameter("txtnombre"); password=request.getParameter("txtpassword"); try { Class.forName("com.mysql.jdbc.Driver"); Connection cn=DriverManager.getConnection("jdbc:mysql://localhost/norberwiener","root","123"); out.println("Conexin realizada con xito a: "+cn.getCatalog()); String acceso="select*from usuario where Nom_Usua like'"+nombre+"' && Pass_Usua like '"+password+"'";

Statement sentencia=cn.createStatement(); ResultSet rs=sentencia.executeQuery(acceso); if(rs.first()) { response.sendRedirect("Menu.jsp"); } else { response.sendRedirect("login.jsp"); out.println("Datos Incorrectos");

}catch(SQLException ex){ out.print("Se produjo un error durante la conexion:"+ex); }catch(Exception error){ out.print("se produjo una exception:"+error);

}%>

</body> </html>

Das könnte Ihnen auch gefallen