I said to myself, "Come now, I will make a test of pleasure; enjoy yourself." But again, this also was vanity. I said of laughter, "It is mad," and of pleasure, "What use is it?" I searched with my mind how to cheer my body with wine—my mind still guiding me with wisdom—and how to lay hold on folly, until I might see what was good for mortals to do under heaven during the few days of their life. (NRSV 2:1-3)
I said in mine heart, Go to now, I will prove thee with mirth, therefore enjoy pleasure: and, behold, this also is vanity. I said of laughter, It is mad: and of mirth, What doeth it? I sought in mine heart to give myself unto wine, yet acquainting mine heart with wisdom; and to lay hold on folly, till I might see what was that good for the sons of men, which they should do under the heaven all the days of their life. (KJV 2:1-3)
Here, the Teacher investigates pleasure and folly and then finds out that they're madness (sheer madness we tell you!). But it's a necessary part of his attempt to discover what wisdom is and what's good for human beings.
So I turned to consider wisdom and madness and folly; for what can the one do who comes after the king? Only what has already been done. Then I saw that wisdom excels folly as light excels darkness. The wise have eyes in their head, but fools walk in darkness. Yet I perceived that the same fate befalls all of them. Then I said to myself, "What happens to the fool will happen to me also; why then have I been so very wise?" And I said to myself that this also is vanity. For there is no enduring remembrance of the wise or of fools, seeing that in the days to come all will have been long forgotten. How can the wise die just like fools? So I hated life, because what is done under the sun was grievous to me; for all is vanity and a chasing after wind. (NRSV 2:12-17)
And I turned myself to behold wisdom, and madness, and folly: for what can the man do that cometh after the king? even that which hath been already done. Then I saw that wisdom excelleth folly, as far as light excelleth darkness. The wise man's eyes are in his head; but the fool walketh in darkness: and I myself perceived also that one event happeneth to them all. Then said I in my heart, As it happeneth to the fool, so it happeneth even to me; and why was I then more wise? Then I said in my heart, that this also is vanity. For there is no remembrance of the wise more than of the fool for ever; seeing that which now is in the days to come shall all be forgotten. And how dieth the wise man? as the fool. Therefore I hated life; because the work that is wrought under the sun is grievous unto me: for all is vanity and vexation of spirit. (KJV 2:12-17)
Now, Ecclesiastes starts bashing folly. He was interested in it at first, but now he's saying that wisdom excels folly, because it helps you see what life really is. Yet, then he goes on to say that, "in the long run, we're all dead" (to quote John Maynard Keynes, a famous economist)—the fools and the wise men together.
Then I saw that all toil and all skill in work come from one person's envy of another. This also is vanity and a chasing after wind. Fools fold their hands and consume their own flesh. Better is a handful with quiet than two handfuls with toil, and a chasing after wind. (NRSV 4:4-6)
Again, I considered all travail, and every right work, that for this a man is envied of his neighbour. This is also vanity and vexation of spirit. The fool foldeth his hands together, and eateth his own flesh. Better is an handful with quietness, than both the hands full with travail and vexation of spirit. (KJV 4:4-6)
It's bad to work only to try to beat other people out of envy. Real creative stuff can't come out of this. You need to have passion for the work itself. But it's even worse to just be idle—another fool gnawing on his own flesh (metaphorically—we hope).
Never be rash with your mouth, nor let your heart be quick to utter a word before God, for God is in heaven, and you upon earth; therefore let your words be few. For dreams come with many cares, and a fool's voice with many words. When you make a vow to God, do not delay fulfilling it; for he has no pleasure in fools. Fulfill what you vow. (NRSV 5:2-4)
Be not rash with thy mouth, and let not thine heart be hasty to utter any thing before God: for God is in heaven, and thou upon earth: therefore let thy words be few. For a dream cometh through the multitude of business; and a fool's voice is known by multitude of words. When thou vowest a vow unto God, defer not to pay it; for he hath no pleasure in fools: pay that which thou hast vowed. (KJV 5:2-4)
Even though Ecclesiastes seems to have a pretty distant idea of God, he takes God very seriously—only fools treat the vows they make to God with carelessness.
It is better to go to the house of mourning than to go to the house of feasting; for this is the end of everyone, and the living will lay it to heart. Sorrow is better than laughter, for by sadness of countenance the heart is made glad. The heart of the wise is in the house of mourning; but the heart of fools is in the house of mirth. It is better to hear the rebuke of the wise than to hear the song of fools. For like the crackling of thorns under a pot, so is the laughter of fools; this also is vanity. (NRSV 7:2-6)
It is better to go to the house of mourning, than to go to the house of feasting: for that is the end of all men; and the living will lay it to his heart. Sorrow is better than laughter: for by the sadness of the countenance the heart is made better. The heart of the wise is in the house of mourning; but the heart of fools is in the house of mirth. It is better to hear the rebuke of the wise, than for a man to hear the song of fools. For as the crackling of thorns under a pot, so is the laughter of the fool: this also is vanity. (KJV 7:2-6)
This is a pretty subtle and delicate point: the wise go to learn about sorrow and suffering, but fools only want to learn about what's fun and enjoyable. But because the wise face suffering directly, they're able to enjoy life as it really is, whereas the fool has a fake enjoyment. A fool takes delight in something that isn't really life—his laughter is just like the futile sound of crackling tinder.
Dead flies make the perfumer's ointment give off a foul odor; so a little folly outweighs wisdom and honor. The heart of the wise inclines to the right, but the heart of a fool to the left. Even when fools walk on the road, they lack sense, and show to everyone that they are fools. (NRSV 10:1-3)
Dead flies cause the ointment of the apothecary to send forth a stinking savour: so doth a little folly him that is in reputation for wisdom and honour. A wise man's heart is at his right hand; but a fool's heart at his left. Yea also, when he that is a fool walketh by the way, his wisdom faileth him, and he saith to every one that he is a fool. (KJV 10:1-3)
If you're going to have wisdom, you need to go all in. It's not that you need to stop enjoying life—far from it (as Ecclesiastes keeps saying). But you can't let a foolish and deluded attitude pollute your attempts to enjoy life. Otherwise the whole thing will fall apart.
There is an evil that I have seen under the sun, as great an error as if it proceeded from the ruler: folly is set in many high places, and the rich sit in a low place. I have seen slaves on horseback, and princes walking on foot like slaves. (NRSV 10:5-7)
There is an evil which I have seen under the sun, as an error which proceedeth from the ruler: Folly is set in great dignity, and the rich sit in low place. I have seen servants upon horses, and princes walking as servants upon the earth. (KJV 10:5-7)
Yet, even though folly ruins your ability to enjoy life, there are still fools everywhere. They get rich and get into positions of power, even though they don't deserve it and are incapable of enjoying it. And wise people are stuck in lowly places. It's just the way things are.
Words spoken by the wise bring them favor, but the lips of fools consume them. The words of their mouths begin in foolishness, and their talk ends in wicked madness; yet fools talk on and on. No one knows what is to happen, and who can tell anyone what the future holds? The toil of fools wears them out, for they do not even know the way to town. (NRSV 10:12-15)
The words of a wise man's mouth are gracious; but the lips of a fool will swallow up himself. The beginning of the words of his mouth is foolishness: and the end of his talk is mischievous madness. A fool also is full of words: a man cannot tell what shall be; and what shall be after him, who can tell him? The labour of the foolish wearieth every one of them, because he knoweth not how to go to the city. (KJV 10:12-15)
This is just another attack on fools, pointing out how they talk too much. Too much talking will lead to madness, apparently. Ecclesiastes is really concerned that we get this point.