Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. private void asignarCodigo() throws ClassNotFoundException, SQLException {
  2.  
  3. //Cargo driver
  4. Class.forName("com.mysql.jdbc.Driver");
  5. //Creo conexión
  6. Connection cnx = DriverManager.getConnection(ruta, usuario, pass);
  7. //Creo sentencia
  8. Statement sentencia = cnx.createStatement();
  9.  
  10. //Ejecuto sentencia sobre un resultset
  11. ResultSet resultado = sentencia.executeQuery("SELECT * FROM tabla;");
  12. //Variable para saber e ltamaño de ResultSet
  13. int ultimo = 0;
  14. //Recorro el bucle para saber el último código de departamento
  15. while (resultado.next()) {
  16. ultimo = resultado.getInt(1);
  17. }
  18. // Sumo uno al último código de departamento
  19. ultimo++;
  20. //Asigno el nuevo id al campo de texto
  21. jTextFieldCodigo.setText(String.valueOf(ultimo));
  22. //Libero recursos
  23. ////resultado.close();
  24. sentencia.close();
  25. cnx.close();
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement