Hello there. I am not a parent but I would like to offer my suggestions for you & your son.
I can see your worry in having your child attend school when his classmates might make fun of him and give him trouble for the rest of his school years. I'm sure your son will eventually grow out of this problem, but if you are worried it may impact his social life even when he is a teenager, you may want to be extra careful about how he does his business at school.
My first suggestion is that you talk with his teacher. You do not necessarily have to tell the teacher the exact problem, just let her know that your son has bathroom troubles. I think the best idea is try to avoid him using the restroom while he is at school. There is really nothing you can do to STOP him from going (if he gotta go, he gotta go). But just let the teacher know there is a problem. That way she will not be taken off guard if something happens and will be able to spare your child some embarrasment if there is an incident. If your child is only 5, the teacher will have probably dealt with children who have bathroom issues before. It shouldn't be anything new to her.
Some suggestions that you might want to put on the table for her is to make sure she asks your son "is it an emergency?" before he goes to the bathroom every time. I am not sure this will work because he may just say "yes" anyways, but it might do a little to discourage him from going to the restroom just so he can stimulate himself. Also, see if she has any concrete plan on when the children can go to the bathroom. If you are lucky, she may have certain mapped out times during the day when she lets all the children go to the bathroom at once (some of my elementary teachers did this). The good thing about this is that if your son sees there are other people waiting to use the toilet, he might just hurry himself up and not stimulate himself at all.
Make sure your son uses the bathroom before he goes to school and a lot when he is at home, so maybe he will not have to go during school at all. The urination cycle is a little harder to regulate because it will be difficult to only allow your son to drink at certain times, but it may be a good step to try to get him on a disbowelment cycle. This way you can get him on a schedule to where he will not have to excrete during school. You may need help from a doctor or dietician for more information on this, but I think it is a good idea once your son starts school. It will involve something like only allowing him to eat at certain times (breakfast, lunch, dinner, occasional snacks at certain times every day). It is not a hard process and as long as you are sure your son is getting full, balanced meals, it will not have an effect on his health. This way you can plan on feeding him at times that will not require him to use the restroom while he is at school. If your child uses the bathroom less/not at all during school, he will have less of a chance of embarrasing himself in front of classmates.
One last suggestion is that you may want to get your child involved in some other activites that can get him the attention he might be craving. If you think he is doing this just for attention, it might be a cry that he wants more attention from you and your wife. It would probably help to encourage him more and make sure you praise him for the good things he does more than you scold him for the bad things. Try not to scold him for this problem too much. Simply clean up his hands and let him be. If you have told him that what he is doing is wrong and he still does it anyways, there is not much more you can do to stop him.
My best advice is to get him involved in a sport or after-school activities. Spend a lot of time with him outside of the house. Not only will these things give him attention in other areas, but they may take his mind off of the other thing he is doing. Giving him a lot of fun things to do with you and his mother will hopefully make him forget about stimulating himself.
I hope this advice works and best wishes for you and your family!