private Double toDouble(ResultSet rs, String column) throws SQLException {
   Object obj = rs.getObject(column);
   if (obj instanceof Double) {
     return (Double) obj;
   }
   return null;
 }
Exemplo n.º 2
0
  private void jTbdescripcionKeyPressed(
      java.awt.event.KeyEvent evt) { // GEN-FIRST:event_jTbdescripcionKeyPressed

    try {
      // se comienza la conexion con la base de datos
      try {
        con = new Conexion();

      } catch (ClassNotFoundException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
      }

      String nom = jTbdescripcion.getText();
      String sql = "SELECT * FROM productos WHERE nombre_producto LIKE '" + nom + "%'";
      rs = con.Consulta(sql);

      if (rs == null)
        JOptionPane.showMessageDialog(
            null, "No se encontro: " + jTbdescripcion.getText() + " en la base de datos.");

      // Para establecer el modelo al JTable

      DefaultTableModel buscar =
          new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int rowIndex, int vColIndex) {
              return false;
            }
          };
      this.jTbuscar.setModel(buscar);

      // Obteniendo la informacion de las columnas que estan siendo consultadas
      ResultSetMetaData rsMd = rs.getMetaData();
      // La cantidad de columnas que tiene la consulta
      int cantidadColumnas = rsMd.getColumnCount();
      // Establecer como cabezeras el nombre de las colimnas
      for (int i = 1; i <= cantidadColumnas; i++) {
        buscar.addColumn(rsMd.getColumnLabel(i));
      }

      while (rs.next()) {
        Object[] fila = new Object[cantidadColumnas];

        for (int i = 0; i < cantidadColumnas; i++) {

          fila[i] = rs.getObject(i + 1);
        }
        buscar.addRow(fila);
      }

    } catch (SQLException ex) {
      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
    }
  } // GEN-LAST:event_jTbdescripcionKeyPressed