Your code has various issues:
- You missed an
=
after and Permissao
- From your code it seems you are looking for a user filtering by the following fields: Username, Password and Permissao, so you should have 3 variables defined
- You are trying to access the ResultSet (using
rs.getXXX
) before selecting any rows. After the executeQuery
method you "fill" a ResultSet but his index is not pointing to any valid "database rows" so you need to call "rs.next()" in order to move the index to the first row. Consecutive calls move the index ahead of 1 position every time until the ResultSet finishes.
Having said so, you should:
Use a prepared statement that prevents sql injection and other typo/character errors as it automatically escapes parameter values.
In the prepared statement use ?
to define the parameters you'll need to set using s.set<TypeOfField>
Check if ResultSet
has rows before using rs.get
Close connection, statement, and result set in the finally clause, so the resources will be closed either if there is or there is not an exception. Doing so you will prevent memory leak due to opened resources that you are not using anymore.
You should have 3 variable to perform the select: (I suppose)
- Username of type String
- Password of type String
- Permissao of type int/Integer
Try using the following code, adapted to your needs.
Connection c = DB.dbConnect(null);
PreparedStatement s = null;
ResultSet rs = null;
try {
final String SQL = " Select * from utilizador where Nome=? and Password=? and Permissao = ? ";
s = c.prepareStatement(SQL);
int i = 1;
s.setString(i++, Username);
s.setString(i++, Password);
s.setInt(i++, Permissao);
rs = s.executeQuery();
if (rs.next()) {
int permissao = rs.getInt("Permissao");
String nome = rs.getString("Nome");
String password = rs.getString("Password");
}
} catch (SQLException e) {
// exception handler
} finally {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
}
try {
if (s != null)
s.close();
} catch (Exception e) {
}try {
if (c != null)
c.close();
} catch (Exception e) {
}
}