How to set a jcombobox in a Preparedstatement?

Asked

Viewed 134 times

0

How do I make a jcombobox in this method? The Combobox would be the type the user is supposed to choose, I tried to make one but it didn’t work out very well. And this method is in my main class and how do I set in a Preparedstatement who is in a Dao class?

My method that is in the Main class

 public static void inserirEnderecoTeste(){
        Bean viagem = new Bean();

        Object[] opcoes = {"Um","Dois","Tres","Quatro"};  
        viagem.setTipo(JOptionPane.showInputDialog(null, "Escolha um item" , "Selecao de itens" ,  
         JOptionPane.PLAIN_MESSAGE , null ,opcoes,""));


        viagem.setDataInicio(DataHelper.StringToCalendar("dd/MM/yyyy", 
                JOptionPane.showInputDialog("Insira a data de inicio:\n(Deve ser no formato  DD/MM/YYYY)", "30/05/2015")));
        viagem.setDataFim(DataHelper.StringToCalendar("dd/MM/yyyy", 
                JOptionPane.showInputDialog("Insira a data de fim:\n(Deve ser no formato  DD/MM/YYYY)", "01/05/2015")));
        viagem.setCidade(JOptionPane.showInputDialog("Insira a cidade:"));
        viagem.setUf(JOptionPane.showInputDialog("Insira o bairro:"));
        viagem.setValorDiaria(new Double(JOptionPane.showInputDialog("Valor da diária:")));
        viagem.setColaborador(JOptionPane.showInputDialog("Insira um colaborador"));
        viagem.setCliente(JOptionPane.showInputDialog("Insira um cliente"));
        //vb.setCidade(JOptionPane.showInputDialog("Insira a cidade Destino:", "Angra dos Reis"));



        if (Dao.inserir(viagem)){
            JOptionPane.showMessageDialog(null, "Inseriu!");
        } else {
            JOptionPane.showMessageDialog(null, "Não Inseriu!");
        }
    }
 }

This is the class Dao where I’m supposed to set the jcombobox

public static boolean inserir(Bean endereco) {
        boolean executou = false;
        if (ConexaoMySQL.conectar()) {
            try {
                Connection con = ConexaoMySQL.getConexao();
                String sql = "Insert into viagem values (0,?,?,?,?,?,?,?,?)";
                PreparedStatement pstm = con.prepareStatement(sql);

        >>>>>>>     pstm.setString(1,endereco.getTipo()); <<<<<<<<<<<

inserir a descrição da imagem aqui

  • http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#getSelectedItem%28%29

1 answer

2


Apparently the following works (although the question appears in the title):

public static void main(String[] args) {
    System.out.println(JOptionPane.showInputDialog("Qual é o seu nome?"));
    JComboBox<String> options = new JComboBox<String>(new String[] {
            "Desenvolvedor", "Colaborador", "Visitante"
    });
    JOptionPane.showMessageDialog(null, options, "Qual é o seu título?", JOptionPane.QUESTION_MESSAGE);
    System.out.println(options.getSelectedItem());
}

The secret was to use Joptionpane.showMessageDialog (see documentation here).

Also worth taking a look at this response in Stackoverflow.


Editing:

In his Bean, leave the attribute tipo as String, instead of JComboBox.

  • Dude I did it this way: Object[] options = {"One","Two","Three","Four"}; trip.setType(Joptionpane.showInputDialog(null, "Choose an item" , "Select items" , Joptionpane.PLAIN_MESSAGE , null ,options"); My problem is how I’m going to set this in my preparestately: >>>>>>>> pstm.setString(1,address. getType()); <<<<<<<<<<<

  • @Jarwin What’s the matter with endereco.getTipo()?

  • I just don’t know how to return this in pstm.setString();

  • For example for city I do so:pstm.setString(4,address. getCity());

  • 1

    I’ll update the question Main class for you to see how I’m creating my Jcombobox blz?

  • No, but the pstm.setString(1, endereco.getTipo()) should work, once you are setting the attribute tipo of Bean via viagem.setTipo(...). What’s the problem? Not compiling? Are you making a mistake? Not saving with the right value?

  • I took a print of the error ok on the question says that setString(int, String) in preparestatement is not applied to the arguments(in,Jcombobox)

  • Aaaaa is because it is no longer string in my Bean but rather an Object so it is giving it but the GRA I switched to pstm.setObject(1,addressType()); and it is giving another error '-'

  • Just leave as String in the Bean, and everything will solve: both the viagem.setTipo(...) as to the pstm.setString(1, endereco.getTipo())!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.