Edición de datos en SQL desde Java

Publicado por Yanina Castillo
hace 4 años

<p>Buenas Tardes,</p><p>Necesitaría que si pudieran ayudarme con una aplicación de Java y Sql. Tengo que realizar un ABM, la cuestión es que estoy trabajando con un JList, allí tengo almacenados los datos, ya puedo insertar y eliminar, me faltaría modificar, el tema es que necesitaría editar los datos del List a los JText y allí modificarlos, pero no se como hacerlos, agradeciera que me ayuden por favor. </p><p>este es mi codigo: </p><pre class="prettyprint prettyprinted"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">Modificar_Gestor</span><span class="pun">(</span><span class="typ">Gestor_entity</span><span class="pln"> gestor</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">throws</span><span class="pln"> </span><span class="typ">Exceptions</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">    </span><span class="typ">Connection</span><span class="pln"> conn </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><br /><span class="pln">    </span><span class="typ">String</span><span class="pln"> nombre</span><span class="pun">,</span><span class="pln"> apellido</span><span class="pun">,</span><span class="pln">direccion</span><span class="pun">;</span><br /><span class="pln">    </span><span class="typ">String</span><span class="pln">  dni</span><span class="pun">,</span><span class="pln"> telefono</span><span class="pun">;</span><br /><span class="pln">    </span><span class="kwd">long</span><span class="pln"> id</span><span class="pun">;</span><br /><span class="pln">    </span><span class="kwd">int</span><span class="pln"> edad</span><span class="pun">;</span><br /><span class="pln">    nombre</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getNombre</span><span class="pun">();</span><br /><span class="pln">    apellido</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getApellido</span><span class="pun">();</span><br /><span class="pln">    direccion</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getDireccion</span><span class="pun">();</span><br /><span class="pln">    edad</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getEdad</span><span class="pun">();</span><br /><span class="pln">    dni</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getDni</span><span class="pun">();</span><br /><span class="pln">    telefono</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getTelefono</span><span class="pun">();</span><br /><span class="pln">    id</span><span class="pun">=</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getId_gestor</span><span class="pun">();</span><br /><span class="pln">    </span><br /><span class="pln">    </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">        conn </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Conectar</span><span class="pun">.</span><span class="pln">getConnection</span><span class="pun">();</span><br /><span class="pln">        </span><span class="typ">PreparedStatement</span><span class="pln"> psmt </span><span class="pun">=</span><span class="pln"> conn</span><span class="pun">.</span><span class="pln">prepareStatement</span><span class="pun">(</span><span class="str">"UPDATE gestor"</span><span class="pln"> </span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">"SET id_gestor= ?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">" SET nombre =?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">"SET apellido =?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">" SET dni =?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">" SET telefono =?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">" SET direccion =?"</span><span class="pun">+</span><br /><span class="pln">                                                       </span><span class="str">" SET edad =?"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><br /><span class="pln">                                                       </span><span class="str">" WHERE id_gestor =?"</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setLong</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> id</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> nombre</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> apellido</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> dni</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span><span class="pln"> telefono</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">6</span><span class="pun">,</span><span class="pln"> direccion</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">setLong</span><span class="pun">(</span><span class="lit">7</span><span class="pun">,</span><span class="pln"> edad</span><span class="pun">);</span><br /><span class="pln">        psmt</span><span class="pun">.</span><span class="pln">executeUpdate</span><span class="pun">();</span><br /><span class="pln">        </span><br /><span class="pln">        </span><br /><span class="pln">        </span><br /><span class="pln">    </span><span class="pun">}</span><span class="kwd">catch</span><span class="pun">(</span><span class="typ">Exception</span><span class="pln"> ex</span><span class="pun">){</span><br /><span class="pln">        </span><span class="kwd">throw</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Exceptions</span><span class="pun">(</span><span class="str">"Error al actualizar los datos"</span><span class="pun">,</span><span class="pln">ex</span><span class="pun">);</span><span class="pln"> </span><br /><span class="pln">    </span><span class="pun">}</span><br /><span class="pun">}</span><span class="pln"> </span><br /><br /><br /><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">Editar_Gestor</span><span class="pun">(</span><span class="typ">Gestor_entity</span><span class="pln"> gestor</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">throws</span><span class="pln"> </span><span class="typ">Exceptions</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">    </span><span class="typ">Connection</span><span class="pln"> conn</span><span class="pun">;</span><br /><span class="pln">        gestor</span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><br /><span class="pln">        </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">           conn </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Conectar</span><span class="pun">.</span><span class="pln">getConnection</span><span class="pun">();</span><br /><span class="pln">           </span><span class="com">//Statement stmt = conn.createStatement();</span><br /><span class="pln">           </span><span class="com">//ResultSet rs1= stmt.executeQuery("select from gestor");</span><br /><span class="pln">           </span><br /><span class="pln">           </span><span class="typ">PreparedStatement</span><span class="pln"> psmt </span><span class="pun">=</span><span class="pln"> conn</span><span class="pun">.</span><span class="pln">prepareStatement</span><span class="pun">(</span><span class="str">"select from gestor"</span><span class="pun">);</span><br /><span class="pln">           </span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setLong</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getId_gestor</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln">gestor</span><span class="pun">.</span><span class="pln">getNombre</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getApellido</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getTelefono</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setString</span><span class="pun">(</span><span class="lit">6</span><span class="pun">,</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getDireccion</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">setLong</span><span class="pun">(</span><span class="lit">7</span><span class="pun">,</span><span class="pln"> gestor</span><span class="pun">.</span><span class="pln">getEdad</span><span class="pun">());</span><br /><span class="pln">            psmt</span><span class="pun">.</span><span class="pln">executeUpdate</span><span class="pun">();</span><br /><span class="pln">           conn</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><br /><span class="pln">           </span><br /><span class="pln">           </span><br /><span class="pln">        </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Exception</span><span class="pln"> ex</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">           </span><span class="kwd">throw</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Exceptions</span><span class="pun">(</span><span class="str">"Error al editar los datos de la tabla"</span><span class="pun">,</span><span class="pln">ex</span><span class="pun">);</span><br /><span class="pln">        </span><span class="pun">}</span><br /><span class="pln">        </span><br /><span class="pln"> </span><span class="pun">}</span><br /><span class="pln"> </span><br /><span class="pun">}</span></pre><div><br /></div><div>Por Favor, necesito que me ayuden</div><div><br /></div>

Respuesta de Agustín Gonzalez
hace 4 años

<p>Hola! Primero que nada debes tener más cuidado en la forma en que nombras las funciones: "Modificar_Gestor" y "Editar_Gestor" suenan muy parecidas en cuanto funcionalidad, lo cual puede llevar a confusiones. Segundo, en vez de "<span>nombre= gestor.getNombre(); psmt.setString(2, nombre);", es más simple poner "psmt.setString(2, gestor.getNombre());". Te </span>ahorras<span> líneas de código, siendo este más legible.</span></p><p><span>Más allá de esos comentarios, el "pasar los datos al modo edición" es más que simple. Cuando se presione el botón Editar (es decir, se produzca el evento), deberás traer la entidad y asignar cada propiedad a las cajas de textos (con el setText()). Muchísimo más simple de lo que parecía.</span></p>

Para poder participar del foro necesitas iniciar sesión con tu cuenta. Si no tienes una puedes registrarte utilizando tu email!

Iniciar sesión Quiero registrarme