Code example for PreparedStatement

Methods: close, execute, setDate, setString

0
	 
	public void adiciona(Contato contato){
		String sql = "INSERT INTO contatos (nome, email, endereco, dataNascimento) values (?, ?, ?, ?)";
		try{ 
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			stmt.setString(1, contato.getNome());
			stmt.setString(2, contato.getEmail());
			stmt.setString(3, contato.getEndereco());
			stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
			stmt.execute();
			stmt.close();
		} catch (SQLException e){
			throw new AdicionaContatoException("Falha ao adicionar contato!"); 
		} 
	} 
	 
	public List<Contato> getLista() {
		try { 
			List<Contato> contatos = new ArrayList();
			PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM contatos");
			ResultSet rs = stmt.executeQuery();