Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. try {
  2.            
  3.             Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/vehiculos", "root", "");
  4.             Statement s = conexion.createStatement();
  5.             if (!marcas.equals("Todos") && emisiones.equals("Todos")) {
  6.                 ResultSet rs = s.executeQuery("select *from vehiculos where marca ='" + marcas.getSelectedItem() + "';");
  7.            
  8.             DefaultTableModel modelo = new DefaultTableModel();
  9.             ResultSetMetaData rsMd = rs.getMetaData();
  10.             int cantidadColumnas = rsMd.getColumnCount();
  11.             for (int i = 1; i <= cantidadColumnas; i++) modelo.addColumn(rsMd.getColumnLabel(i));
  12.             while (rs.next()) {
  13.                 Object[] fila = new Object[cantidadColumnas];
  14.                 for (int i = 0; i < cantidadColumnas; i++) fila[i] = rs.getObject(i + 1);
  15.                 modelo.addRow(fila);
  16.                 rs.close();
  17.                
  18.                 jtQuery.setModel(modelo);
  19.             }
  20.             }else if(marcas.equals("Todos") && !emisiones.equals("Todos")){
  21.                 ResultSet rs = s.executeQuery("select *from vehiculos where emisiones " + emisiones.getSelectedItem() + ";");
  22.            
  23.                 DefaultTableModel modelo = new DefaultTableModel();
  24.             ResultSetMetaData rsMd = rs.getMetaData();
  25.             int cantidadColumnas = rsMd.getColumnCount();
  26.             for (int i = 1; i <= cantidadColumnas; i++) modelo.addColumn(rsMd.getColumnLabel(i));
  27.             while (rs.next()) {
  28.                 Object[] fila = new Object[cantidadColumnas];
  29.                 for (int i = 0; i < cantidadColumnas; i++) fila[i] = rs.getObject(i + 1);
  30.                 modelo.addRow(fila);
  31.                 rs.close();
  32.                
  33.                 jtQuery.setModel(modelo);
  34.             }
  35.             }else if (!marcas.equals("Todos") && !emisiones.equals("Todos")) {
  36.                 ResultSet rs = s.executeQuery("select *from vehiculos where marca ='" + marcas.getSelectedItem() + "and emisiones " + emisiones.getSelectedItem() + "';");
  37.            
  38.                 DefaultTableModel modelo = new DefaultTableModel();
  39.             ResultSetMetaData rsMd = rs.getMetaData();
  40.             int cantidadColumnas = rsMd.getColumnCount();
  41.             for (int i = 1; i <= cantidadColumnas; i++) modelo.addColumn(rsMd.getColumnLabel(i));
  42.             while (rs.next()) {
  43.                 Object[] fila = new Object[cantidadColumnas];
  44.                 for (int i = 0; i < cantidadColumnas; i++) fila[i] = rs.getObject(i + 1);
  45.                 modelo.addRow(fila);
  46.                 rs.close();
  47.                
  48.                 jtQuery.setModel(modelo);
  49.             }
  50.             }
  51.            
  52.             conexion.close();
  53.            
  54.            
  55.  
  56.         } catch (Exception ex) {ex.printStackTrace();}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement