Olá eu tenho um combo numa pagina em jsp, por exemplo municipio e sendo q tenho uma tabela no banco de dados municipio que é padrão onde tem todos os municipios, como faço para cadastrar e consultar esses dados, quando eu pesquisar um ALUNO “X” venha todas as informações dele e no combobox carregue o municipio q foi cadastradado pra ele e como faço para cadastrar isso no banco?
Atenciosamente,
Diego
Na tua tabela ALUNO, crie uma coluna chamada MUNICIPIO_CADASTRO_ID (ou MUNICIPIO_ID) daí você sabe que aquele aluno pertence ao município X, para consultar, é só fazer um join entre a tabela aluno e município (algo do tipo select * from aluno a, municipio m where a.municipio_id = m.municipio_id )
isso aí tudo bem… mas como vou passar o ID para o combo?
Assim:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Class1 extends JFrame {
JButton button = new JButton("Exibir Aluno");
JComboBox combo;
public Class1() {
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Aluno a = (Aluno)combo.getSelectedItem();
JOptionPane.showMessageDialog(null, a.getId() + " - "+ a.getNome());
}
});
List<Aluno> listaAluno = new ArrayList<Aluno>();
listaAluno.add(new Aluno(1,"José"));
listaAluno.add(new Aluno(2,"Maria"));
listaAluno.add(new Aluno(3,"João"));
listaAluno.add(new Aluno(4,"Joana"));
combo = new JComboBox(listaAluno.toArray());
this.add(combo);
this.add(button);
this.setVisible(true);
this.pack();
}
public static void main(String[] args) {
Class1 c = new Class1();
}
}
class Aluno {
private int id;
private String nome;
public Aluno(int id, String nome) {
this.id = id;
this.nome = nome;
}
public int getId() {
return id;
}
public String getNome() {
return nome;
}
public String toString() {
return nome;
}
}