You have to be the owner of record before the ex-dividend date, which is not the same day as the date the dividend is paid. This also implies that if you sell on or after the ex-dividend date, you'll still get the dividend, even if you no longer own the stock.
Keep in mind, also, that the quoted price of the stock (and on any open orders that are not specifically marked as "do not reduce") on its ex-dividend date is dropped by the amount of the dividend, first thing in the morning before trading starts. If you happen to be the first order of the day, before market forces cause the price to move, you'll end up with zero gain, since the dividend is built into the price, and you got the same value out of it -- the dividend in cash, and the remaining value in stock. As pointed out in the comments (Thanks @Brick), you'll still get a market price for your trade, but the price reduction will have had some impact on the first trade of the day.
Source: NYSE Rule 118.30
Also, remember that the dividend yield is expressed in annualized terms. So a 3% yield can only be fully realized by receiving all of the dividend payments made by the company for the year. You can, of course, forget about individual companies and just look for dividends to create your own effective yield over time. But, see the final point...
Finally, if you keep buying and selling just to play games with the dividends, you're going to pay far more in transaction fees than you will earn in dividends. And, depending on your individual circumstances, you may end up paying more in capital gains taxes.