Created
December 16, 2014 02:40
-
-
Save samuelsonbrito/af4816160f247f115713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Usuario buscaUsuario(int id) throws SQLException { | |
Connection conexaoComBanco = null; | |
PreparedStatement preparedStatement = null; | |
Usuario usuarioEncontrado = null; | |
String consulta = "SELECT EMAIL, NOME WHERE ID = ?"; | |
try { | |
// busca a conexão de algum lugar válido | |
conexaoComBanco = getConexaoComBanco(); | |
// prepara o objeto da conexão | |
preparedStatement = conexaoComBanco.prepareStatement(consulta); | |
preparedStatement.setInt(1, id); | |
// executa a consulta | |
ResultSet rs = preparedStatement.executeQuery(); | |
usuarioEncontrado = new Usuario(); | |
while (rs.next()) { | |
String email = rs.getString("EMAIL"); | |
String nome = rs.getString("NOME"); | |
usuarioEncontrado.setNome(nome); | |
usuarioEncontrado.setId(id); | |
usuarioEncontrado.setEmail(email); | |
} | |
} finally { | |
if (preparedStatement != null) { | |
preparedStatement.close(); | |
} | |
if (conexaoComBanco != null) { | |
conexaoComBanco.close(); | |
} | |
} | |
return usuarioEncontrado; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment