1
I have a question that is next, when I make a bank consultation he can return me more than two names, and I can only get one. The idea is to filter the names of doctors by the type of doctor(Neurosurgeon, among others) and then take these names and put in a Combobox.
Here is the code but returns only one name.
I created a function for that.
public String tipoMedico(String tipo)
{
try{
Statement comando = cone.createStatement();
ResultSet rs = comando.executeQuery("SELECT Nome,Tipo FROM `medicos`
WHERE Tipo='"+tipo+"';");//Se tiver dois médicos do mesmo tipo, era
para retornar mais de um nome, quero saber como pegar todos os nomes
que retornar.
while(rs.next()) {
String Tipo = rs.getString("Tipo");
String Nome1 = rs.getString("Nome");
if(tipo.equals(Tipo)){
return Nome1;
}
}}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Erro na Conexão com o Banco de
Dados");
}
return "Não encontrado";
}
Then call Names in a Combobox.
private void cbTipoActionPerformed(java.awt.event.ActionEvent evt) {
String nome =
dado.tipoMedico(cbTipo.getItemAt(cbTipo.getSelectedIndex()));
String tipo = cbTipo.getItemAt(cbTipo.getSelectedIndex());
switch(tipo)
{
case "Neurocirurgião": cbDoutor.removeAllItems();
cbDoutor.addItem(nome);//Aqui eu adiciono o nome retornado do banco
na comboBox
break;
case "Clinico Geral": cbDoutor.removeAllItems();
cbDoutor.addItem(nome);
break;
}}
Dude, the problem is in your Turn. You’re familiar with the structure
LIst
?– Sorack
You are returning a simple string from the database. A combobox expects a combomodel or string array.
– user28595
Another thing, you should not mix the layers, wherever it is to search in the bank, it should be only for consultation and bank functions, optionpane in the bank method and with defined return is a bad practice.
– user28595