使用来自sql-java的数据向组合框添加数据

时间:2011-09-09 14:48:08

标签: java sql swing resultset jcombobox

我开始用Java编写Java程序。 我遇到的问题是,当我尝试用sql中的表中的数据填充组合框时,它不起作用,我不知道为什么。

这是我到目前为止所尝试的内容:

public class Proveedores extends javax.swing.JFrame
{

    Connection cnn;

    ConexionesBaseDeDatos conexiones = new ConexionesBaseDeDatos();


    public Proveedores() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNombreProveedor = new javax.swing.JTextField();
        txtDireccionProveedor = new javax.swing.JTextField();
        txtMailProveedor = new javax.swing.JTextField();
        cmbProvinciasProveedores = new javax.swing.JComboBox();
        btnCrearProveedor = new javax.swing.JButton();
        btnModificarProveedor = new javax.swing.JButton();
        btnDesabilitarProveedor = new javax.swing.JButton();
        txtTelefonoProveedor = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listProveedores = new javax.swing.JList();
        btnAceptar = new javax.swing.JButton();
        btnGuardarCambios = new javax.swing.JButton();
        btnDesabilitar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setText("Nombre");

        jLabel2.setText("Teléfono");

        jLabel3.setText("Dirección");

        jLabel4.setText("Mail");

        jLabel5.setText("Provincia");

        btnCrearProveedor.setText("Crear Proveedor");

        btnModificarProveedor.setText("Modificar Proveedor");

        btnDesabilitarProveedor.setText("Deshabilitar Proveedor");

        txtTelefonoProveedor.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        listProveedores.setEnabled(false);
        listProveedores.setVisibleRowCount(13);
        jScrollPane1.setViewportView(listProveedores);

        btnAceptar.setText("Aceptar");

        btnGuardarCambios.setText("Guardar Cambios");
        btnGuardarCambios.setEnabled(false);

        btnDesabilitar.setText("Desabilitar");
        btnDesabilitar.setEnabled(false);

        btnCancelar.setText("Cancelar");

        jMenu1.setText("File");

        jMenuItem1.setText("Proveedores");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Clientes");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Modelos");
        jMenu1.add(jMenuItem3);

        jMenuItem4.setText("Eventos");
        jMenu1.add(jMenuItem4);

        jMenuItem5.setText("Subastas");
        jMenu1.add(jMenuItem5);

        jMenuItem6.setText("Compras");
        jMenu1.add(jMenuItem6);

        jMenuItem7.setText("Ventas");
        jMenu1.add(jMenuItem7);

        jMenuItem10.setText("Colecciones");
        jMenu1.add(jMenuItem10);

        jMenuItem11.setText("Lineas");
        jMenu1.add(jMenuItem11);

        jMenu3.setText("Productos");

        jMenuItem9.setText("Tipos de productos");
        jMenu3.add(jMenuItem9);

        jMenu1.add(jMenu3);

        jMenu4.setText("Insumos");

        jMenuItem8.setText("Tipos de insumos");
        jMenu4.add(jMenuItem8);

        jMenu1.add(jMenu4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        jMenuItem12.setText("Acerca de...");
        jMenu2.add(jMenuItem12);

        jMenuBar1.add(jMenu2);

        jMenu5.setText("Salir");
        jMenuBar1.add(jMenu5);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(btnCrearProveedor)
                .addGap(97, 97, 97)
                .addComponent(btnModificarProveedor)
                .addGap(104, 104, 104)
                .addComponent(btnDesabilitarProveedor)
                .addContainerGap(64, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(btnAceptar)
                .addGap(52, 52, 52)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addComponent(btnGuardarCambios)
                .addGap(62, 62, 62)
                .addComponent(btnDesabilitar)
                .addGap(71, 71, 71))
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(cmbProvinciasProveedores, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(18, 18, 18)
                            .addComponent(txtTelefonoProveedor))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(42, 42, 42)
                            .addComponent(txtMailProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNombreProveedor)
                            .addComponent(txtDireccionProveedor, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCrearProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(btnDesabilitarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNombreProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(txtDireccionProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel5))
                            .addComponent(cmbProvinciasProveedores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTelefonoProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel4))
                            .addComponent(txtMailProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                        .addGap(27, 27, 27)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDesabilitar)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardarCambios))
                .addGap(64, 64, 64))
        );

        pack();
    }// </editor-fold>

    private void formWindowActivated(java.awt.event.WindowEvent evt) {
        deshabilitarCampos();

        if (conexiones.conectaralabase())
            cargarComboProvincias();

    }

    public static void main()
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Proveedores().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCrearProveedor;
    private javax.swing.JButton btnDesabilitar;
    private javax.swing.JButton btnDesabilitarProveedor;
    private javax.swing.JButton btnGuardarCambios;
    private javax.swing.JButton btnModificarProveedor;
    private javax.swing.JComboBox cmbProvinciasProveedores;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listProveedores;
    private javax.swing.JTextField txtDireccionProveedor;
    private javax.swing.JTextField txtMailProveedor;
    private javax.swing.JTextField txtNombreProveedor;
    private javax.swing.JFormattedTextField txtTelefonoProveedor;
    // End of variables declaration


    private void deshabilitarCampos()
    {
        txtNombreProveedor.setEnabled(false);
        txtDireccionProveedor.setEnabled(false);
        txtMailProveedor.setEnabled(false);
        btnAceptar.setEnabled(false);
        btnCancelar.setEnabled(false);
        btnDesabilitar.setEnabled(false);
        btnGuardarCambios.setEnabled(false);
        cmbProvinciasProveedores.setEnabled(false);
        listProveedores.setEnabled(false);
    }

    private void habilitarCamposBtnCrearProveedor()
    {
        btnAceptar.setEnabled(true);
        btnCancelar.setEnabled(true);
        cmbProvinciasProveedores.setEnabled(true);
        listProveedores.setEnabled(true);
        txtDireccionProveedor.setEnabled(true);
        txtMailProveedor.setEnabled(true);
        txtNombreProveedor.setEnabled(true);
    }

    private void habilitarCamposBtnModificarProveedor()
    {
        btnAceptar.setEnabled(false);
        btnCancelar.setEnabled(true);
        cmbProvinciasProveedores.setEnabled(true);
        listProveedores.setEnabled(true);
        txtDireccionProveedor.setEnabled(true);
        txtMailProveedor.setEnabled(true);
        txtNombreProveedor.setEnabled(true);
        btnGuardarCambios.setEnabled(true);
        btnDesabilitarProveedor.setEnabled(false);
        btnDesabilitar.setEnabled(false);
        btnCrearProveedor.setEnabled(false);
    }

    private void limpiarCampos()
    {
        cmbProvinciasProveedores.setSelectedIndex(-1);
        txtDireccionProveedor.setText("");
        txtMailProveedor.setText("");
        txtNombreProveedor.setText("");
    }


    public void habilitarbtnCrearProveedor()
    {
        btnAceptar.setEnabled(true);
        btnCancelar.setEnabled(true);
        cmbProvinciasProveedores.setEnabled(true);
        txtDireccionProveedor.setEnabled(true);
        txtMailProveedor.setEnabled(true);
        txtNombreProveedor.setEnabled(true);
        txtTelefonoProveedor.setEnabled(true);
    }


    private void cargarComboProvincias()
    {
       Statement sql;
       ResultSet datos;
       boolean seguir;
       Provincias provinciaActual;

       try
       {
           sql = cnn.createStatement();
           datos = sql.executeQuery("SELECT * FROM provincias WHERE id_provincia IS NOT NULL");
           seguir = datos.next();

           while (seguir)
           {
               provinciaActual = new Provincias();
               provinciaActual.setNombre_provincia(datos.getString("nombre_provincia"));

               provinciaActual.toString();
               cmbProvinciasProveedores.addItem(provinciaActual);
               seguir = datos.next();
          }
       }
       catch (Exception e)
       {
           JOptionPane.showMessageDialog(null,e.getMessage() + "error en cargarComboProvincias");
           e.printStackTrace();
       }
    }
}


and here is where i make the connection with the database:


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.JOptionPane;


public class ConexionesBaseDeDatos
{
    Connection cnn;

    public boolean conectaralabase()
    {
         String connString;

        try
        {
            Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connString = ("jdbc:sqlserver://GILLOPEZ:1433;database=DOLLS;Trusted_Connection=True");
            cnn = DriverManager.getConnection (connString,"sa","1q2w3e4r");
        }
        catch(Exception e)
        {
            System.out.println( e.getMessage() + "Error en conectaralabase()");
            return false;
        }
        return true;
    }
}

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:3)

你必须在Resulset循环

中进行三次选择

1)将数据放入String[]数组

`String[] items = {"test1", "test2", "test3"};
 JComboBox combo = new JComboBox(items);`

2)将数据放入Vector 1D Vector

 Vector<String> v1 = new Vector<String>();
 v1.add("item1");
 v1.add("item2");
 v1.add("item3");
 JComboBox combo = new JComboBox(v1);

3)将数据放入ComboBoxModel

 DefaultComboBoxModel model = new DefaultComboBoxModel();
 model.add("item1");
 model.add("item2");
 model.add("item3");
 JComboBox combo = new JComboBox();
 combo.setModel(model);

但请在阅读之前阅读