2
I have a question about using Preparedstatement with INNER JOIN, here’s my code:
public List<Emprestimo> pesquisar() throws SQLException {
List<Emprestimo> listaEmprestimo = new ArrayList<Emprestimo>();
String sql = "SELECT emp.cod_obra AS id_obra, ob.titulo, emp.dispo, emp.dataDevolu "
+ "FROM emprestimo emp "
+ "INNER JOIN obra ob "
+ "ON ob.cod_obra = emp.cod_obra"
+ "ORDER BY id_obra";
PreparedStatement ps = c.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
listaEmprestimo = new ArrayList<Emprestimo>();
while (rs.next()) {
Emprestimo emp = new Emprestimo();
emp.getObra().setCodObra(rs.getInt("id"));
emp.getObra().setTitulo(rs.getString("nome"));
emp.setDisp(rs.getInt("status"));
emp.setDataDevolucao(rs.getDate("dataDev"));
listaEmprestimo.add(emp);
}
rs.close();
ps.close();
return listaEmprestimo;
}
However, when I compile the code this error is returned:
But when I do the query, it works. What goes wrong?
You are not making space between things. Your query is being built like this:
... ON ob.cod_obra = emp.cod_obraORDER BY id_obra ...
. Missed space at the end of"ON ob.cod_obra = emp.cod_obra"
. - is the suggestion to print SQL on the screen when doing the debug of this kind of problem.– Bacco