Multiple-field sql search in java

2

How to do a sql search in java that involves multiple arguments? Example: I want to research all people with Angolan nationality, Computer Science course and two or more years of professional experience.

I'm using this way: select * from candidato where nome like ? , but when I increase more fields it returns error.

This is my search method:

public List<Dados_Cadastro> getLista(String nome) throws SQLException{
     String sql="select * from candidato where nome like ?";
 PreparedStatement stm = this.conexao.prepareStatement(sql);
 stm.setString(1, nome);
 ResultSet rs = stm.executeQuery();

 List<Dados_Cadastro>nova_lista = new ArrayList<Dados_Cadastro>();
 while(rs.next()){
  Dados_Cadastro cadastro =new Dados_Cadastro();

 cadastro.setNome_cadastro(rs.getString("nome"));
 cadastro.setGenero_cadastro(rs.getString("genero"));
 cadastro.setData_nasc_cadastro(rs.getString("nasc"));
 cadastro.setCbox_nacion_cadastro(rs.getString("nacionalidade"));
 cadastro.setCbox_nivel_cadastro(rs.getString("n_academico"));
 cadastro.setCurso_cadastro(rs.getString("curso"));
 cadastro.setEmpresa_cadastro(rs.getString("empresa"));
 cadastro.setCbox_exper_cadastro(rs.getString("experiencia"));
 cadastro.setIdent(Long.valueOf(rs.getString("ident")));
nova_lista.add(cadastro);
 }
 rs.close();
 stm.close();
 return nova_lista;
 }
    
asked by anonymous 22.04.2016 / 17:33

2 answers

2

You can number the parameters, for example:

select * from candidato where nome like ?1 AND genero = ?2

and then enter the parameters:

stm.setString(1, nome);
stm.setString(2, genero);
    
22.04.2016 / 20:55
1

SELECT * FROM candidato WHERE nome LIKE '%'+getNome_cadastrado+'%';

The % sign is used to set wildcards before and after the default.

    
22.04.2016 / 17:59