In SQL, I have a table called COMPANIAS
with id_compania
and nombre
.
Given the name, in a variable, I want to find the id_compania
that has exactly that name.
Method to obtain the ID:
public int companiaNombreToInt(String nombre) {
int id = 0;
sSQL = "SELECT id_compania FROM companias WHERE nombre = '" + nombre + "'";
// Java 7 try-with-resources
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sSQL)) {
id = rs.getInt("id_compania");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "SQLException:\n" + e, "Error: companiaNombreToInt()", JOptionPane.ERROR_MESSAGE);
}
return id;
}
Error:
The result set has not current row.
I tried:
- name = '% name%'
- name = '% + name +%'
- name = '+ name +'
What is the correct way ?, or is my mistake another?.
Thanks in advance.