Advertisement
Metziop

Untitled

May 31st, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1.  //---------------- llenando la tabla de empleados--------------//
  2.     private void llenarTabla() {
  3.  
  4.         try {
  5.             DefaultTableModel model = new DefaultTableModel();
  6.  
  7.             jTEmpleados.setModel(model);
  8.             jTEmpleados.setAutoCreateRowSorter(true);
  9.             PreparedStatement ps = null;
  10.             ResultSet rs = null;
  11.  
  12.             Connection conn = MySQLConection.getConnection();
  13.             String sql = "SELECT numEmpleado,user FROM empleados where puesto='diseñador'";
  14.             ps = conn.prepareStatement(sql);
  15.             rs = ps.executeQuery();
  16.             ResultSetMetaData rsMeta = rs.getMetaData();
  17.             int columnas = rsMeta.getColumnCount();
  18.             model.addColumn("ID");
  19.             model.addColumn("Usuario");
  20.  
  21.             int[] anchos = {3, 50};
  22.             for (int x = 0; x < columnas; x++) {
  23.                 jTEmpleados.getColumnModel().getColumn(x).setPreferredWidth(anchos[x]);
  24.             }
  25.             while (rs.next()) {
  26.                 Object[] filas = new Object[columnas];
  27.                 for (int i = 0; i < columnas; i++) {
  28.                     filas[i] = rs.getObject(i + 1);
  29.                 }
  30.                 model.addRow(filas);
  31.             }
  32.         } catch (Exception e) {
  33.             JOptionPane.showMessageDialog(null, "Error al actualizar" + e);
  34.         }
  35.  
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement