Categoria: Java para WEB
|
Publicado em 28 de Novembro de 2010
|
- SELECT - Query parametrizada
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
try {
// Query to select users that matches the informed username and password
String query = "SELECT * FROM users WHERE username = ? AND password = ?";
// Prepares the query
PreparedStatement ps = conn.prepareStatement( query );
// Passes the parameters values
ps.setString(1, "usuario");
ps.setString(2, "senha");
// Executes the query and stores it's result set
ResultSet rs = ps.executeQuery();
if ( rs.first() ) {
// One or more records found
// Reading the username field from the first row
rs.getString("username");
} else {
// No record was found
}
} catch (SQLException e) {
System.out.println("Não foi possível executar a Query SQL");
}
|
|