Java - validacion de un registro existente

 
Vista:
sin imagen de perfil

validacion de un registro existente

Publicado por Sara (1 intervención) el 08/05/2016 01:54:46
Hola, hago una aplicacion web en donde realizo la crud de tablas con conexion a mysql, quisiera validar si existe o no un registro a la hora de realizar una consulta, tengo el siguiente codigo, pero a la hora de ejecutar se queda pensando, agradeceria la respuesta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<%@page import="javax.swing.JOptionPane"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="Modelo.Ciudades"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="MenuPrincipal.jsp"%>
        <title>Resultado consulta</title>
    </head>
    <body>
        <%! ResultSet rs =  null;
            Connection cnx = null;
            Statement st = null;
            Ciudades Ciudad = new Ciudades();
 
            String url= "jdbc:mysql://localhost/Caso4";
            String us= "root";
            String ps= "";
 
            boolean existe = false;
        %>
        <%
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                cnx=DriverManager.getConnection(url,us,ps);
                if(cnx!=null)
                                 {
                         if(request.getParameter("btnConsultar")!=null){
                            Ciudad.setId_ciudad(Integer.parseInt(request.getParameter("idciu")));
                                     System.out.println("Conexion exitosa");
                                     st = cnx.createStatement();
                                     rs=st.executeQuery("select*from ciudad where id_ciudad ='"+Ciudad.getId_ciudad()+"'");
                         }
 
                                       else{
                                            System.out.println("Conexion fallida");
                                           }
                                  }
 
        %>
        <table class="table table-bordered">
            <caption class="text-center">El resultado de su búsqueda es: </caption>
            <tr>
                <th>Id ciudad</th>
                <th>Nombre ciudad</th>
            </tr>
                <%
                    while(rs.next()){
                        existe = true;
                        if(existe == true){
                %>
                <tr>
                <td><%rs.getInt("id_ciudad");%></td>
                <td><%rs.getString("Nombre_ciudad");%></td>
                </tr>
                <%                        }
                                    }
                if ( existe == false ) {
JOptionPane.showMessageDialog(null, "El registro NO está en la base de datos,\nvuelva a capturar el número o reportelo a TI", "Error de captura", JOptionPane.ERROR_MESSAGE);
 
                                       }
 }catch(SQLException e)
                {
                     System.out.println("Exception:: " +e);
                }catch(Exception e)
                {
                     e.printStackTrace();
                }
                %>
        </table>
 
    </body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder