Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/06/2010, 09:32
Sebast20
 
Fecha de Ingreso: mayo-2010
Mensajes: 57
Antigüedad: 13 años, 11 meses
Puntos: 1
Exclamación Jtable con Result set

bueno mi consulta es la siguiente necesito crear una clase que me ayude a ahorrar codigo
porque tengo 2 funciones los cuales me rellena y dibujan un Jtable cn un result set pero es demaciado codigo
Código Javascript:
Ver original
  1. private void dibujarcom(){
  2.         try{
  3.             sentencia = con.createStatement();
  4.             rs = sentencia.executeQuery("select count(0) as Cantidad from comunas");
  5.             int numero = 0;
  6.             if(rs.next()){
  7.                 numero =rs.getInt("Cantidad");
  8.             }
  9.             sentencia.close();
  10.   grillacom.setModel(new javax.swing.table.DefaultTableModel(
  11.                     new Object[numero][4],
  12.                     new String[]{
  13.                     "Codigo","Nombre","Region"
  14.                     }
  15.                     ));
  16.             llenarcom();
  17.         }catch(SQLException e1){
  18.          javax.swing.JOptionPane.showMessageDialog(this, "Error en Dibujar la Grilla SQL", "ERROR", 0);
  19.  
  20.         }catch(Exception e2){
  21.             javax.swing.JOptionPane.showMessageDialog(this, "Error al dibujar", "ERROR", 0);
  22.  
  23.         }
  24.  
  25.  
  26.     }
  27.  
  28.  
  29.  
  30. private void llenarcom(){
  31.     String sel;
  32.     int x;
  33.     try{
  34.         sel="SELECT comunas.cod AS cod, comunas.nombre AS nombre, regiones.nombre AS region FROM comunas, regiones WHERE comunas.region = regiones.cod ";
  35.         sentencia=con.createStatement();
  36.         rs =sentencia.executeQuery(sel);
  37.         x=0;
  38.         while(rs.next()){
  39.             grillacom.setValueAt(rs.getString("cod"), x, 0);
  40.             grillacom.setValueAt(rs.getString("nombre"), x, 1);
  41.             grillacom.setValueAt(rs.getString("region"), x, 2);
  42.  
  43.                     x++;
  44.             }
  45.     sentencia.close();
  46.     }catch(SQLException e){
  47.         JOptionPane.showMessageDialog(this, "Error [MySQLException] al listar datos: " + e.getMessage(),"Error",0);
  48.     }catch(Exception e){
  49.         JOptionPane.showMessageDialog(this, "Error [Exception] al listar datos: " + e.getMessage(),"Error",0);
  50.     }
  51. }