UNIDAD 8: JAVA JSP INTRODUCION A LAS BASES DE DATOS

TEMA 12: BAJA O ELIMINACION SQL DELETE

Eliminación es otro proceso simple y comun con las bases de datos el modelo que estamos usando hace este tipo de operaciones muy faciles:

La instrucción sql a usar es: DELETE FROM TABLA WHERE CONDICION

Y RESULTSET.EXECUTEUPDATE()

Prog46.jsp


<%@ page import="java.io.*,java.util.*,java.net.*,java.sql.*" %>

<%

// declarando y creando objetos globales

Connection canal = null;

ResultSet tabla= null;

Statement instruccion=null;

String strcon = "jdbc:mysql://localhost/mibase?user=lauro&password=laurosoto";

if(request.getParameter("OK") != null)

{

// abriendo canal o enlace en su propio try-catch

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();

canal=DriverManager.getConnection(strcon);

instruccion = canal.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

} catch(java.lang.ClassNotFoundException e){} catch(SQLException e) {};

// preparando condicion de eliminacion

int clave = Integer.parseInt(request.getParameter("CLAVEB"));

// construyendo select con condicion eliminacion SQL DELETE

String q="delete from mitabla where clave = "+clave;

// mandando SQL a tabla en disco

try { instruccion.executeUpdate(q);

// avisando

out.println("registro eliminado");

} //fin try no usar ; al final de dos o mas catchs

catch(SQLException e) {}

catch(java.lang.NullPointerException e){};

try {

// no ocupa cerrar tabla(), no se leyo (select) un resultset

// la eliminacion fue directa en disco

instruccion.close();canal.close();} catch(SQLException e) {};

};

// construyendo forma dinamica

out.println("<FORM ACTION=prog46.jsp METHOD=post>");

out.println("CLAVE ELIMINAR:<INPUT TYPE=TEXT NAME=CLAVEB><BR>");

out.println("<INPUT TYPE=SUBMIT NAME=OK VALUE=ELIMINAR><BR>");

out.println("</FORM>");

%>

Corridas :

Prog ←-tabla original

image89.jpg

pro46.jsp

image90.jpg

prog41.jsp

image91.jpg

TAREAS PROGRAMACION JAVA JSP

1.- construir este proceso para las tablas y bases de datos que tengan construidas.

 




Google
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Politica de Privacidad