Download Ejemplo de Servlet con connexion a base de datos MySql

Document related concepts
no text concepts found
Transcript
<!-- Ejemplo de Servlet con connexion a base de datos MySql el nombre del
archivo es index.html -->
<html>
<head>
<title>Mostrar Datos</title>
</head>
<body>
<form method="get" action="Test">
<input type="submit">
</form>
</body>
</html>
/* Servlet que se conecta a la base de datos y crea una base de datos para mostrar
los resultados, el nombre del archivo es Test.java para generar el Test.class
*/
/*
* OJO, COLOCAR LA LIBRERIA DEL DRIVER
* mysql-connector-java-3.1.12-bin.jar EN
* C:\Program Files\Java\jre1.6.0_02\lib\ext\
* Y LAS LIBRERIAS jsp-api.jar y servlets-api.jar AGREGARLAS AL JDK EN
* configure / options / jdk profiles / edit / add / archives
* Base de Datos "prueba"
* tabla clientes
* campos id int, nombre varchar(20), apellido varchar(20)
*/
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 Test extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
try {
response.setContentType("text/html");
PrintWriter sal = response.getWriter();
sal.println("<html><body>");
/* Driver */
String driver = "com.mysql.jdbc.Driver";
sal.println( "=> Cargando el Driver:" );
Class.forName(driver);
sal.println("OK");
/* Definiendo Base de Datos */
String url = "jdbc:mysql://localhost/prueba" ;
String user = "root";
String pass = "";
/* Conectando */
sal.println( "=> conectando:" );
Connection con = DriverManager.getConnection( url , user , pass );
sal.println("OK");
/* Mostrando Resultados */
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM clientes");
sal.println("<table border = \"1\" >");
sal.println("<tr bgcolor = \" #FFFF00 \"><th>Codigo</th><th>Nombre</th></tr>");
while( rs.next() )
{
sal.println("<tr>");
sal.println("<td>" + rs.getInt(1) + "</td>" );
sal.println("<td>" + rs.getString(2) + "</td>" );
sal.println("</tr>");
}
sal.println("</table>");
sal.println("</body></html>");
}
catch( Exception x ) {
x.printStackTrace();
}
}
}
<!-- El archivo web.xml -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Servlet Muestra Datos de Base de Datos</display-name>
<description>
Mostrar Datos
</description>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
La base de datos
-- phpMyAdmin SQL Dump
-- version 2.11.4
-- http://www.phpmyadmin.net
--- Servidor: localhost
-- Tiempo de generación: 26-11-2008 a las 19:23:52
-- Versión del servidor: 5.0.51
-- Versión de PHP: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--- Base de datos: `prueba`
-CREATE DATABASE `prueba` DEFAULT CHARACTER SET latin1 COLLATE
latin1_swedish_ci;
USE `prueba`;
-- ---------------------------------------------------------- Estructura de tabla para la tabla `clientes`
-CREATE TABLE IF NOT EXISTS `clientes` (
`id` int(11) NOT NULL,
`nombre` varchar(20) NOT NULL,
`apellido` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--- Volcar la base de datos para la tabla `clientes`
-INSERT INTO `clientes` (`id`, `nombre`, `apellido`) VALUES
(21, 'Jorge', 'Aguilar'),
(436, 'Mario', 'Perez');