utlizacion combobox with database

0

I have to fill a combox with a single data from the database and I can not do it please someone to help me thanks this is the image of my database this is the code I am using and I need to show it in this part where it says search by date I will thank you a thousand thanks, this is to finalize my thesis and I am a beginner in this language, all in MYSQL

    
asked by Carlos Ariza 11.12.2018 в 14:00
source

2 answers

0

I hope you will use my bro code!

import javax.swing.*;

import java.sql.*;

class LlenaCombo extends JFrame{

 private JComboBox miCombo;

 private Connection conexion;

 private Statement sentencia;

 private ResultSet resultado;

 private String sql;

 public LlenaCombo(){

    conexion = DriverManager.getConnection(AQUI PONES TU RUTA);

    sql = "SELECT DISTINCTROW DATO FROM TU BD"; 

    sentencia = conexion.createStatement(sql);

    resultado = sentencia.executeQuery();

    miCombo = new JComboBox("DATO");

    while(resultado.next()){

     miCombo.addItem(resultado.getString(1));

   }

 }
}

Important:

Distinctrow: adds a single column in specific == > the data in that column.

In the loop: the combobox is filled every loop loop with each row of the column.

    
answered by 11.12.2018 в 17:33
-1

// About the class

DefaultComboBoxModel modelCombo = new DefaultComboBoxModel (); // I create the model to load items DefaultComboBoxModel modelCombo2 = new DefaultComboBoxModel (); // I create the model to load items

// In the constructor

jComboBox5.removeAllItems (); // Delete current items jComboBox1.removeAllItems (); // Delete current items fill_combo (1); // Fill the group combo fill_combo (2);

// The method

public void fill_combo (int selec) {         String informationTable="";         String informacionCampo="";

    switch(selec){

        case 1:
            informacionTabla = "tabla_grupo";
            informacionCampo = "nombre_encargado";
        break;
        case 2:
            informacionTabla = "tabla_trabajo";
            informacionCampo = "nombre_trabajo";
        default:
            System.out.println("No existe opcion para el llenado del combo.");
        break;

    }

    try {
        lb = new LibreriaHerramientas();
        String Sql = "SELECT "+ informacionCampo + " as Name "
                + " FROM "
                + informacionTabla; // Escribo la sentencia !!
        PreparedStatement us = lb.conex().prepareStatement(Sql); //
        ResultSet res = us.executeQuery(); //

        if(selec == 1){
            modeloCombo.addElement("Seleccione un Campo");//es el primer registro q mostrara el combo
            jComboBox5.setModel(modeloCombo);//con esto lo agregamos al objeto al jcombobox
        }
        if(selec == 2){
            modeloCombo2.addElement("Seleccione un Campo");//es el primer registro q mostrara el combo
            jComboBox1.setModel(modeloCombo2);//con esto lo agregamos al objeto al jcombobox
        }
        while (res.next()) {

            //jComboBox5.setModel(modeloCombo);
            if(selec == 1){    
                modeloCombo.addElement(res.getObject("Name"));
                jComboBox5.setModel(modeloCombo);//con esto lo agregamos al objeto al jcombobox
            }
            if(selec == 2){
                modeloCombo2.addElement(res.getObject("Name"));
                jComboBox1.setModel(modeloCombo2);//con esto lo agregamos al objeto al jcombobox
            }

        }

        res.close();
    } catch(SQLException ex){

    }
}
    
answered by 11.12.2018 в 18:16