'java.sql.SQLSyntaxErrorException: Unknown column 'id' in 'where clause'
excuse me, this is the first time I have asked a question on this platform.
I am generating a code in Java to be able to make a query in a database, but when I send a call with select * from where I get the following error:
java.sql.SQLSyntaxErrorException: Unknown column 'id' in 'where clause'
I don't know what the correct syntax must be so that it doesn't generate that error. It should be noted that I am using MySQL version 8.0.2, in case that is important. I attach the part of the code where it is giving me problems. Thank you very much, I hope you can help me.
public Cliente buscarCliente(int id) {
Cliente c = new Cliente();
String sql = "select * from Cliente where id = ? limit 1";
PreparedStatement stm = null;
Connection con = Conexion.init();
ResultSet rs = null;
try {
stm = con.prepareStatement(sql);
stm.setInt(1, id);
rs = stm.executeQuery();
while(rs.next()) {
c.setClienteID(rs.getInt("ClienteID"));
c.setClienteNombre(rs.getString("ClienteNombre"));
c.setClienteDirección(rs.getString("ClienteDireccion"));
c.setClienteRFC(rs.getString("ClienteRFC"));
c.getFechaNac(rs.getDate("FechaNac"));
}
rs.close();
stm.close();
} catch (SQLException ex) {
Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return c;
}
Solution 1:[1]
Kindly request to check tables columns by using desc Cliente to verify column names. As per the sql exception the column id is not present in Cliente table.
Solution 2:[2]
I came across a same exception. I got it to work thought. Here's how I did it.
You should set the datatype of id
in the table Cliente
to INT
because apparently in Java, the condition in where
clause is compared when both are of integer datatypes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Banupriya velusamy |
Solution 2 | Reznov |