3
Ways to get values from the Java database (JDBC).
public static Connection getConnection(){
/* 1. Faz a conexao com a base de dados */
/* 2. Retorna qual banco ira trabalhar */
Connection connection=null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/base", "postgres", "postgres");
System.out.print("Conexao com o banco de dados efetuada com sucesso!");
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
System.out.print("Erro durante a conexao com o banco de dados!");
}
return connection;
}
Map<String,String> fields_values = new HashMap<String,String>(); // Chave: Valor
String sql = "SELECT id, nome, idade, endereco FROM PESSOA";
Connection conexao = getConnection(); // Chama método para retornar conexão com o banco
prepared = conexao.prepareStatement(sql); // Prepara o SQL para ser executado no banco
ResultSet result = prepared.executeQuery(sql); // Retorno do banco de dados
I believe there must be another way to take the values from the database without having to use methods such as result.getInt(), result.()getString, result.getFloat()
.
- I could get the values from the database another way?
- I can obtain the values in a more simplified way to store in the variable
fields_values
?
Very good guy, I tested here worked right!
– Luiz Ricardo Cardoso