Andy Beanstalk "Jack" is first (and only) seen as a prisoner of San Ricardo prison in Puss in Boots. He is an old man and tells Puss in Boots his name is Andy Beanstalk, but his friends call him Jack, thus his name, Jack Andy Beanstalk, is a parody of Jack and the Beanstalk, whom (it is supposed, by his words) he is the character from.
All that is known about his early life is what he tells Puss, that he is the Jack that gained the magic beans, went up the beanstalk and (presumably) killed the giant, yet still retained some magic beans on him.
It wasn't his family cow he traded for the beans, though, but rather "some other family's family cow" and this crime of theft got him eight to ten years sentence in the San Ricardo prison.
As it is presumed he was a youth at this time, Jack has been in prison well over his sentencing and then some, giving his old age, but he has either not noticed this or has little control over his own fate. He doesn't realize he can slip his hands free of his shackles and regularly slips them out to gesture, but then will set them right back in.
His cell mate history is probably varied but unknown except that he counts Humpty Alexander Dumpty as his least favorite cell mate. Much as he did with Puss, he told the story of how he ended up in jail to Humpty and then the egg at some point stole his magic beans off of him, perhaps during one of his many fits of falling asleep.
It is presumed he was awake and aware of the theft at some point before Humpty's escape, though, as he tells Puss he tried to tell Humpty not to take the Golden Goose as it would cause trouble when it's momma the "Great Terror" came after it.
In the same way he talked much of what he had done to Humpty, the egg told him enough of his plans of revenge that Jack knew getting back at a cat named Puss in Boots was what the egg was after. Once Puss is thrown in jail, Jack thus concludes that "the egg got what he wanted".
He proceeds to tell Puss the same story and also of what Humpty did, thus enlightening the cat to the plight of San Ricardo and undoing Jack's own folly in telling Humpty enough to spur majority of the bad events of the movie, though unintentionally.
The two, Jack and Puss, are freed when Kitty Softpaws knocks out a guard. She apologizes to Puss for betraying him, and Jack cluelessly states that he forgives her before rushing off to at least freedom from the cell. It is never told whether or not he escaped the prison still full of guards though.